1、依赖
<parent><artifactId>spring-boot-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.3.12.RELEASE</version><relativePath/></parent>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- springcloud-alibaba需要的依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
2、服务提供者
2.1)、yml配置
server:port: 8011spring:application:name: ALIBABA-PUBLISHERcloud:nacos:discovery:server-addr: 162.14.66.60:8848 # 自己的服务器ip:8848management:endpoints:web:exposure:include: "*" #健康检查
2.2)、启动类编写
package cn.zixieqing;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @ClassName PublisherApplication* @Author ZiXieQing* @Date 2021/12/7* Version 1.0**/@SpringBootApplication@EnableDiscoveryClient // 开启nacos的客户端功能public class PublisherApplication {public static void main(String[] args) {SpringApplication.run(PublisherApplication.class, args);}}
2.3)、controller编写
package cn.zixieqing.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/provider")public class ProvideService {@GetMapping("/service")public String provideService() {return "this is my son";}}
3、Feign
3.1)、导入依赖
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
3.2)、编写yml
server:port: 8012spring:cloud:nacos:discovery:server-addr: 162.14.66.60:8848 # 自己的服务器ip:8848application:name: ALIBABA-OPENFEIGN
3.3)、接口编写
package cn.zixieqing;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(value = "ALIBABA-PUBLISHER") // publisher中注册进去的服务名public interface ProxyService {@RequestMapping(value = "/provider/service" , method = RequestMethod.GET)String provideService();}
3.4)、启动类编写
package cn.zixieqing;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class OpenFeignApplication {public static void main(String[] args) {SpringApplication.run(OpenFeignApplication.class, args);}}
3.5)、服务消费层编写
package cn.zixieqing.controller;import cn.zixieqing.ProxyService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @ClassName getService* @Author ZiXieQing* @Date 2021/12/7* Version 1.0**/@RestController@RequestMapping("/test")public class getService {@Autowiredprivate ProxyService proxyService;@GetMapping("/getService")public String getServer() {return proxyService.provideService();}}
4、启动publisher 和 Feign