Eureka Getting List of Services
Asked Answered
H

2

5

How can I fetch the already registered service from eureka?

The below code gives the details about a particular service. But I want the list of the registered services .

Code:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(
            @PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }
}
Haeres answered 21/6, 2017 at 7:34 Comment(1)
#42427992Frolick
S
9

Pretty simple really :)

        List<Application> applications = discoveryClient.getApplications().getRegisteredApplications();

    for (Application application : applications) {
        List<InstanceInfo> applicationsInstances = application.getInstances();
        for (InstanceInfo applicationsInstance : applicationsInstances) {

            String name = applicationsInstance.getAppName();
            String url = applicationsInstance.getHomePageUrl();
            System.out.println(name + ": " + url);
        }
    }
Snuffer answered 5/1, 2018 at 6:40 Comment(0)
P
7

Or, and if you like don't want to drag in the whole world of spring-boot/Eureka libs and prefer a "clean" thin client, you can just perform a simple GET towards

http://<eureka host>:<port>/eureka/apps

as is described here Using Eureka as a registry using REST APIs or, and as we do it, utilize springboot admin's API (described here https://codecentric.github.io/spring-boot-admin/1.5.7/ for instance) and just do a simple GET towards

http://<springbootadmin host>:<port>/api/applications

while providing login credentils using a Basic auth header, ie

"Basic", java.util.Base64.getEncoder().encodeToString(("<springboot admin user>" + ":" + "<pwd>").getBytes());

You'll then get a nice JSON response easily parsed into collections of java objects utilizing JSON-attributes

private String name; // service name
private String managementUrl; // an url from which host and port can be easily grabbed 
private StatusInfo statusInfo; // telling whether this service instance is up or down

for instance. Add proper toString()-based hashcode() and equals() and you have something rather proper to work with whether you want to work with sets of running services "on any instance" or unique instances.

Pinch answered 14/12, 2019 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.