eureka之服务发现 DiscoveryClient

1:注入 DiscoveryClient

 

// import org.springframework.cloud.client.discovery.DiscoveryClient;    
@Autowired
    private DiscoveryClient discoveryClient;

 

2:对外提供方法

 

    @RequestMapping("/eureka/discovery")
    public BaseResult discovery() {
        String description = discoveryClient.description();
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            String instancesStr = JSONUtil.toJsonPrettyStr(instances);
            System.out.println("instancesStr== " + instancesStr);
        }
        String servicesStr = JSONUtil.toJsonPrettyStr(services);
        return sendSuccess("description:" + description + "    servicesStr:" + servicesStr + " :: serverPort=" + serverPort);
    }

 

 

调用该方法,会得到eureka里面的一些信息

 

posted @ 2020-04-16 16:37  Draymond  阅读(1584)  评论(0编辑  收藏  举报