nacos user服务消费者(openfeign)访问notice集群 作者:马育民 • 2020-06-23 09:45 • 阅读:10060 # 创建工程 创建 user_openfeign 工程 [![](https://www.malaoshi.top/upload/pic/docker/20200622221341.png)](https://www.malaoshi.top/upload/pic/docker/20200622221341.png) # 添加依赖pom.xml ``` org.springframework.cloud spring-cloud-starter-openfeign com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test ``` # application.yml配置文件 在`resources`文件夹中创建`application.yml`配置文件 ``` server: port: 9001 # 服务端口 spring: application: name: user-consumer # 注册服务名 cloud: nacos: discovery: server-addr: 106.13.165.100:8848 # nacos地址 # 配置服务提供者url,便于在类中调用 service-provider: notice-provider: name: notice-provider urls: notice: /notice/{id} getNoteById: /getNoteById/{id} # 负载均衡策略 notice-provider: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule ``` 负载均衡策略参见: https://www.malaoshi.top/show_1EF5iJxtARRK.html https://www.malaoshi.top/show_1EF5iInH93Gc.html # 主启动类 ``` package top.malaoshi; 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 @EnableFeignClients @EnableDiscoveryClient public class UserFeignMain { public static void main(String[] args) { SpringApplication.run(UserFeignMain.class, args); } } ``` # FeignClient接口 ``` package top.malaoshi.clients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Component @FeignClient(value="${service-provider.notice-provider.name}") //微服务名称 public interface NoticeClient { @GetMapping(value = "${service-provider.notice-provider.urls.notice}") public String notice(@PathVariable("id") String id); } ``` # controller ``` package top.malaoshi.controller; import lombok.extern.slf4j.Slf4j; import top.malaoshi.clients.NoticeClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class FollowCtrl { @Resource private NoticeClient noticeClient; @GetMapping(value="/follow/{id}") public String follow(@PathVariable("id") String id){ log.info("user_openfeign:id:"+id); return noticeClient.notice(id); } } ``` # 修改notice服务的NoticeCtrl 打印一段信息,区别user调用的是哪一个notice服务 ``` package top.malaoshi.controllers; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j //添加注解 public class NoticeCtrl { @GetMapping(value = "/notice/{id}") public String notice(@PathVariable String id) { log.info("id--",id); //直接使用 return "通知成功! " + id; } } ``` # 测试 ### 启动 启动nacos 启动2个notice 启动user_openfeign ### 测试服务 访问 http://localhost:9001/follow/1 能够随机调用1个notice 其中一个notice服务会打印信息 原文出处:http://malaoshi.top/show_1EF5lAmIO8bi.html