Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eureka Server - list all registered instances

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?

like image 849
Kihats Avatar asked Feb 23 '17 22:02

Kihats


3 Answers

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);
        });
    });
like image 182
Kihats Avatar answered Sep 22 '22 22:09

Kihats


If you want to get all registered applications.

  1. 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
    
  2. now we can get the registered applications, but the class must be put in eureka application's package. [As we need to autowire PeerAwareInstanceRegistry]

enter image description here

@Autowired
PeerAwareInstanceRegistry registry;

public void eurekaApplications() {
    Applications applications = registry.getApplications();
    //TODO add your code here.
}
like image 22
tyrantqiao Avatar answered Sep 20 '22 22:09

tyrantqiao


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;
}
like image 21
CATCODER DEV Avatar answered Sep 22 '22 22:09

CATCODER DEV