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里面的一些信息