1. 程式人生 > 其它 >6.Eureka-服務發現Discovery

6.Eureka-服務發現Discovery

如何獲取eureka的各項微服務資訊?

可以在eureka的任何客戶端程式碼中,控制層:
1.控制層新增方法
    @GetMapping("/getDiscovery")
    public Object getDiscovery(){
        //重點1:獲取eureka的微服務名稱
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("微服務名稱:"+service);
            //重點2:獲取微服務下的各項例項資訊
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            for (ServiceInstance instance : instances) {
                log.info("其下的例項:"+instance.getInstanceId()+" host:"+instance.getHost()
                +" port:"+instance.getPort()+" uri:"+instance.getUri()+" serviceID:"+instance.getServiceId()+
                " schema:"+instance.getScheme());
            }
        }
        return services;
    }
2.在springboot啟動類上加上:@EnableDiscoveryClient註解
    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    public class CustomerUserApplication {
        public static void main(String[] args) {
            SpringApplication.run(CustomerUserApplication.class, args);
        }
    }

3.測試:
    當訪問時,控制檯輸出:
         微服務名稱:producer-user-api
             其下的例項:userService8001 host:192.168.137.1 port:8001 uri:http://192.168.137.1:8001 serviceID:PRODUCER-USER-API schema:null
             其下的例項:userService8002 host:192.168.137.1 port:8002 uri:http://192.168.137.1:8002 serviceID:PRODUCER-USER-API schema:null
         微服務名稱:customer-user
             其下的例項:userConsumer80 host:192.168.137.1 port:80 uri:http://192.168.137.1:80 serviceID:CUSTOMER-USER schema:null