In the previous tutorials, I introduced you to the Eureka Server for managing services in a Microservices system and how to register these services with Eureka Server using Spring Cloud Netflix. In this tutorial, I will guide you how a service can find information of other services in a Eureka Server.
As an example of this tutorial, I’m going to use the Eureka Server that I created using Spring Cloud Netflix in my previous post about the Eureka Server along with a service which I created in my tutorial about the Eureka Client. The current interface of Eureka Server is as follows:
Now, I will create another service with Eureka Client dependencies as follows:
Result:
In order to find information on other services in a Eureka Server, you first need to register this service with the Eureka Server. The steps I have guided in this tutorial, if you do not know then can refer. For the service which I created above, I will do the following:
SpringCloudDiscoveryApplication:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springcloud.discovery; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class SpringCloudDiscoveryApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudDiscoveryApplication.class, args); } } |
application.properties:
1 2 3 4 5 |
spring.application.name=Discovery Example eureka.client.service-url.default-zone=http://localhost:8761 server.port=8081 |
Because, the example in the tutorial Register services with Eureka Server using Eureka Client, with Spring Cloud Netflix running on port 8080, so you should, as you can see, change the default port of Tomcat using the property server.port of Spring Boot.
Now, if you run this example, the interface of Eureka Server will now look like this:
As you can see, we have registered two services in a Eureka Server. So how do we do from the service in this tutorial, we can search information of service “Eureka Client Example”, please read more!
Now, I will create a new Controller for easy illustration. This controller will have a request that will allow the user to obtain information about the host, port of the “Eureka Client Example” service from this “Discovery Example” service.
The initial content of this controller is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.springcloud.discovery; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DiscoveryController { @GetMapping("/discover") String discoverService() { String clientURI = null; return clientURI; } } |
To get information about the host, port of the “Eureka Client Example” service, we will use the org.springframework.cloud.client.discovery.DiscoveryClient interface with the getInstances() method. The parameter of this method is serviceId, which is the name of the service registered in the Eureka Server. The specific code at this time of DiscoveryController will be as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.springcloud.discovery; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DiscoveryController { @Autowired private DiscoveryClient discoveryClient; @GetMapping("/discover") String discoverService() { String clientURI = null; List<ServiceInstance> instances = discoveryClient.getInstances("Eureka Client Example"); return clientURI; } } |
with “Eureka Client Example” is the name of the service we are looking for. We can register multiple services of the same name so the return value will be a list, and the ServiceInstance class is the class that holds the information of these services.
Because I currently only register a service called “Eureka Client Example” I will code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.huongdanjava.springcloud.discovery; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DiscoveryController { @Autowired private DiscoveryClient discoveryClient; @GetMapping("/discover") String discoverService() { String clientURI = null; List<ServiceInstance> instances = discoveryClient.getInstances("Eureka Client Example"); clientURI = instances.get(0).getUri().toString(); return clientURI; } } |
to get information about the host, port of this service.
You can also re-write as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava.springcloud.discovery; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DiscoveryController { @Autowired private DiscoveryClient discoveryClient; @GetMapping("/discover") String discoverService() { List<ServiceInstance> instances = discoveryClient.getInstances("Eureka Client Example"); return instances.get(0).getUri().toString(); } } |
Result:
As you can see, information about the host port of the “Eureka Client Example” service, from this information you can request this service to fulfill your needs. Also, because the host port information is managed by Eureka Server, if later on, this service is deployed to another server, we do not need to update that information on our service. Everything will be updated in Eureka Server and we do not need to do anything.