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() + ") ");
});
});
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 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.
}
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();
}
}
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());
}
© 2022 - 2024 — McMap. All rights reserved.