构建步骤
    在服务控制器Controller中注入

    1. /**
    2. * 服务发现 获取服务信息
    3. */
    4. @Resource
    5. private DiscoveryClient discoveryClient;
    6. /**
    7. * 服务发现
    8. * @return
    9. */
    10. @GetMapping("payment/discovery")
    11. public Object discovery(){
    12. List<String> services=discoveryClient.getServices();
    13. for (String element :services){
    14. log.info("element:"+element);
    15. }
    16. //一个微服务下的全部实例
    17. List<ServiceInstance> serviceInstanceList=discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    18. for (ServiceInstance instance : serviceInstanceList){
    19. log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
    20. }
    21. return this.discoveryClient;

    最后在该服务启动类上添加@EnableDiscoveryClient注解即可