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: false
now 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.
}
This works for me, get all services registered on eureka and show iformation about each one
@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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With