feign整合nacos
时间:2022-05-11 11:22
创建两个项目,服务提供者service-offer和服务消费者service-consumer
一、service-offer的pom文件
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-test test com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.2.5.RELEASE
启动类上添加两个注解 @EnableDiscoveryClient @EnableFeignClients
yml配置
server: port: 8183 spring: application: name: service-offer cloud: nacos: discovery: server-addr: localhost:8848
提供一个API
@RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello world"; } }
服务提供者完成
二、创建服务消费者
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.2.5.RELEASE org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.cloud spring-cloud-loadbalancer org.springframework.boot spring-boot-starter-test test
启动类:添加注解
@EnableDiscoveryClient
@EnableFeignClients
yml配置
spring: application: name: service-consumer cloud: nacos: discovery: server-addr: localhost:8848 server: port: 8184
创建feign接口
@FeignClient("service-offer")//填写配置的server name public interface ProvideClient { @RequestMapping("/hello") String hello(); }
并调用该接口
@RestController public class HelloController { @Autowired private ProvideClient provideClient; @RequestMapping("/helloWorld") public String hello(){ return provideClient.hello(); } }
调用成功,访问http://localhost:8848/nacos/index.html,用户名密码都是nacos,登录可查看服务状态
访问http://localhost:8184/helloWorld,访问成功