Eureka Server - list all registered instances

三世轮回 提交于 2019-12-05 07:51:37

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.

  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]

@Autowired
PeerAwareInstanceRegistry registry;

public void eurekaApplications() {
    Applications applications = registry.getApplications();
    //TODO add your code here.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!