Sunday, December 16, 2018

Eureka Service Registry Service Discovery

Eureka Serviced Registry and Service Discovery

Hello Everyone, in this blog i will be showing you how to setup Eureka Service Registry using spring-boot.

Eureka service registry and service discovery:

- Is a REST (Representational State Transfer) base service.
- This can used to connect Eureka Server and Eureka Client.
- Eureka can be used as a load balancer
- Being able to monitor your service that has been register

Eureka Architecture

Without Eureka

From the diagram above, this shows when the service is not being registered using Eureka. To obtain the information about the employee, the producer has to use HTTP.get in order to retrieve the information. If the Employee-consumer is down, then the producer would not be able to retrieve the information because the connection between the producer and the consumer has been cut.

With Eureka

From the diagram above, this shows when the services has been registered using Eureka Service Registry.
As you can observed that both services has been registered to Eureka service registry, by doing this method it will improve performance and also making less modification to your code. If for some reason that the Employee-Consumer has failed, then you still be able to retrieve the data because the service has been registry in Eureka Service registry and the Employee-produce still can obtain the data.

How to set up the Eureka Service Registry

For this example i will create 3 maven project:
- Employee Produce 
- Employee Consumer 
- Eureka Server

Each of the maven project, you will need to add these dependencies:

Also you will need to add this java annotation in your main class:

In your Employee-Producer and Employee-Consumer you have to add the following:

You must add a "properties" file in order to register the service to the Eureka Service Registry:

The port number must be the same as the Eureka server port in order to register the service.

And lastly, in your Employee producer and Employee discovery you must have a properties name called "" in order to name your service.

in your Eureka Server you need to add a properties file which contain the server port


Once you have those 3 maven project running, you can see the registered service is your Eureka server. The image above shows the registered service that i created which is "EMPLOYEE-CONSUMER" and "EMPLOYEE-PRODUCER"

The code above is how you retrieve the data from the employee-consumer to the employee-customer. Instead of writing the URL Path, you can create this object

List<ServiceInstance> instances=discoveryClient.getInstances("employee-producer");
ServiceInstance serviceInstance=instances.get(0);

String baseUrl=serviceInstance.getUri().toString();


and add the name of the service you have registered, so for this example i put in the "employee-produce" as the name of my service  and also add which method you wanted to call which is "employee" for this example.

This is the result that i tested using postman. In the employee consumer i made an employee object and added a couple of data. That data is then save in the Eureka Service registry waiting for the employee producer to consume.
Then using postman, i type in the address for the client server to retrieve all the employee data as shown and the retrieving method is by using Eureka Service Registry. 

No comments:

Post a Comment