当前位置:   article > 正文

SpringCloud --- Eureka 服务发现Discovery_discovery 获取client并执行

discovery 获取client并执行

对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息

(1)再服务提供者controller中加入

  1. @Autowired
  2. private DiscoveryClient client;
  3. @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
  4. public Object discovery()
  5. {
  6. //盘点eureka中的微服务
  7. List<String> list = client.getServices();
  8. System.out.println("**********" + list);
  9. //获取某个微服务
  10. List<ServiceInstance> srvList = client.getInstances("springcloud-demo");
  11. for (ServiceInstance element : srvList) {
  12. System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
  13. + element.getUri());
  14. }
  15. return this.client;
  16. }
  17. import org.springframework.cloud.client.discovery.DiscoveryClient;

(2)服务提供者启动类中加入注解   @EnableDiscoveryClient

(3)自测 

  • 先要启动EurekaServer
  • 再启动服务提供者启动类,需要稍等一会儿
  • http://localhost:8001/dept/descovery

                                  

(4)修改消费者controller

  1. private static final String REST_URL_PREFIX = "http://localhost:8001";
  2. // 测试@EnableDiscoveryClient,消费端可以调用服务发现
  3. @RequestMapping(value = "/consumer/dept/discovery")
  4. public Object discovery()
  5. {
  6. return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
  7. }

              测试:   localhost/consumer/dept/discovery

       

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1000299
推荐阅读
相关标签
  

闽ICP备14008679号