构建步骤
在服务控制器Controller中注入
/**
* 服务发现 获取服务信息
*/
@Resource
private DiscoveryClient discoveryClient;
/**
* 服务发现
* @return
*/
@GetMapping("payment/discovery")
public Object discovery(){
List<String> services=discoveryClient.getServices();
for (String element :services){
log.info("element:"+element);
}
//一个微服务下的全部实例
List<ServiceInstance> serviceInstanceList=discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : serviceInstanceList){
log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
最后在该服务启动类上添加@EnableDiscoveryClient注解即可