Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eureka Getting List of Services

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);
    }
}
like image 741
Rangan Roy Avatar asked Jun 21 '17 07:06

Rangan Roy


People also ask

How do I access Eureka service?

Visit the eureka-client in the browser, at http://localhost:8080/service-instances/a-bootiful-client . There, you should see the ServiceInstance for the eureka-client reflected in the response.

Is Netflix Eureka deprecated?

According to the Eureka wiki (https://github.com/Netflix/eureka/wiki), the Eureka 2.0 has been discontinued. The open source work on eureka 2.0 has been discontinued.

What is Eureka Service Discovery?

Eureka Server is an application that holds the information about all client-service applications. Every Micro service will register into the Eureka server and Eureka server knows all the client applications running on each port and IP address. Eureka Server is also known as Discovery Server.

What is Eureka client fetch registry?

A Eureka instance is also a Eureka client as it fetches the registry from Eureka server containing the details of other instances. In order to enable it, eureka. client. fetch-registry is set to true (by default, true). As soon as a service registers itself with the server, it fetches the registry and catches it.


2 Answers

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);
        }
    }
like image 106
cmptrwizard Avatar answered Dec 12 '22 06:12

cmptrwizard


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.

like image 43
Ola Aronsson Avatar answered Dec 12 '22 08:12

Ola Aronsson