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