[英]Eureka Server - list all registered instances
I have a Spring Boot application that is also a Eureka Server.我有一个 Spring Boot 应用程序,它也是一个 Eureka 服务器。 I want to list all instances that have been registered to this Eureka Server.
我想列出所有已经注册到这个 Eureka Server 的实例。 How do I do it?
我该怎么做?
Fetch the registry
using EurekaServerContextHolder.getInstance().getServerContext().getRegistry()
then use the registry
to list all Applications
使用
EurekaServerContextHolder.getInstance().getServerContext().getRegistry()
获取registry
然后使用registry
列出所有Applications
PeerAwareInstanceRegistry registry = EurekaServerContextHolder.getInstance().getServerContext().getRegistry();
Applications applications = registry.getApplications();
applications.getRegisteredApplications().forEach((registeredApplication) -> {
registeredApplication.getInstances().forEach((instance) -> {
System.out.println(instance.getAppName() + " (" + instance.getInstanceId() + ") : " + response);
});
});
If you want to get all registered applications.如果您想获得所有已注册的应用程序。
you need to turn on eureka's configuration.需要开启eureka的配置。
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #register eureka as application register-with-eureka: true #fetch all thing, we can get applications here fetch-registry: true #also you can specify the instance renewal time. server: enable-self-preservation: false
now we can get the registered applications, but the class must be put in eureka application's package.现在我们可以获取已注册的应用程序,但该类必须放在 eureka 应用程序的包中。 [As we need to autowire PeerAwareInstanceRegistry]
[因为我们需要自动装配 PeerAwareInstanceRegistry]
@Autowired
PeerAwareInstanceRegistry registry;
public void eurekaApplications() {
Applications applications = registry.getApplications();
//TODO add your code here.
}
This works for me, get all services registered on eureka and show iformation about each one这对我有用,在 eureka 上注册所有服务并显示每个服务的信息
@Autowired
private DiscoveryClient discoveryClient;
public List<ServiceInstance> getApplications() {
List<String> services = this.discoveryClient.getServices();
List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
services.forEach(serviceName -> {
this.discoveryClient.getInstances(serviceName).forEach(instance ->{
instances.add(instance);
});
});
return instances;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.