Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix

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:

Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix

Now, I will create another service with Eureka Client dependencies as follows:

Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix

Result:

Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix

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:

application.properties:

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:

Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix

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:

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:

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:

to get information about the host, port of this service.

You can also re-write as follows:

Result:

Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix

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.

Discovery services in Eureka Server using Discovery Client, with Spring Cloud Netflix
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
3Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.