I have a Spring Boot application that is also a Eureka Server. I want to list all instances that have been registered to this Eureka Server. How do I do it?
Fetch the registry using EurekaServerContextHolder.getInstance().getServerContext().getRegistry() then use the registry to list all 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: 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: falsenow we can get the registered applications, but the class must be put in eureka application's package. [As we need to autowire PeerAwareInstanceRegistry]
@Autowired
PeerAwareInstanceRegistry registry;
public void eurekaApplications() {
Applications applications = registry.getApplications();
//TODO add your code here.
}
来源:https://stackoverflow.com/questions/42427492/eureka-server-list-all-registered-instances
