Search code examples
springspring-bootmicroservicesnetflix-eurekaspring-actuator

how can I get registered micro services by using netflix-eureka


I registered many microservices using Netflix's Eureka, and also I'm using Spring Cloud Config. However, when I updated the application.properties I need to restart the application for the new properties to be applied.

Restarting the application is kind of annoying, so googled it and found that get registered microservices by using Netflix Eureka and refresh it using spring boot actuator, but I have failed to get registered microservices.

In short: How can I get registered microservices using Netflix Eureka?


Solution

  • You can access Eureka's registry programmatically using the com.netflix.discovery.EurekaClient interface.

    For example, to send a refresh to all instances of some service:

    @RestController
    public class EurekaOperationsController {
      @Autowired
      private EurekaClient eurekaClient;
      @Autowired
      private RestTemplate restTemplate;
    
      @PostMapping("/{serviceName}")
      public void refreshAllInstancesOf(@RequestParam String serviceName) {
        Application application = eurekaClient.getApplications().getRegisteredApplications(serviceName);
        application.getInstances().forEach(instanceInfo -> {
          restTemplate.postForEntity(instanceInfo.getHomePageUrl() + "actuator/refresh", null, Void.class);
        });
      }
    }
    

    And this can be inside any Eureka client, including Eureka service itself since it should be self registered as a client.