Eureka Server - list all registered instances
Asked Answered
M

5

14

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?

Mellifluent answered 23/2, 2017 at 22:35 Comment(0)
M
11

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() + ") ");
        });
    });
Mellifluent answered 23/2, 2017 at 22:35 Comment(6)
Hello, does the EurekaServerContextHolder need inject? Using the same code, I can't get anything in my test class.Obstetric
@Obstetric did you manage to resolve this, I am facing the same challenge. The code provided by Kihats doesn't work for me.Ermine
@Ermine Sorry for so long to see your question, I'm using another way to get applications now. I would give my solution here.Obstetric
@tyrantqiao, you don't need to inject it. In fact this is the javadoc on that class 'A static holder for the server context for use in non-DI cases.'Mellifluent
what about the response variable inside the println?Fostoria
Does not seem to work, nowadays. For me this other answer worked: https://mcmap.net/q/817432/-eureka-server-list-all-registered-instancesTraditional
Y
2

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;
}
Yogurt answered 23/1, 2020 at 1:43 Comment(0)
O
1

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.
}
Obstetric answered 28/9, 2018 at 1:45 Comment(0)
H
1

To fetch the list of applications registered in the eureka server, you can use the DiscoveryClient interface from springframework.cloud.client.discovery package according to documentation docs.

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
public class DiscoveryClient{


  private final DiscoveryClient client;

  @GetMapping("/api/discovery/instance/info")
  public ResponseEntity<?> test() {
    return ResponseEntity.ok(getApplications());
  }


  public int getApplications() {
    List<ServiceInstance> instances = client.getInstances("YOUR-APPLICATION-NAME-WITHIN-EUREKA");
    log.info("instances {}", instances);
    return instances.size();
  }

}
Haldeman answered 1/11, 2022 at 8:26 Comment(0)
E
0

One can access the list of registered apps from the registry directly:

@Autowired
PeerAwareInstanceRegistry eureka_registry;

List<Application> apps = eureka_registry.getSortedApplications(); 
for(Application a : apps)
{
    System.out.println("  " + a.getName() + ": " + a.getInstancesAsIsFromEureka().get(0).getStatus().name());
}
Encouragement answered 17/3 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.