Microservices: Using Eureka as a service registry

This is the 2nd part of the Developing Microservices with Spring Boot series.

After having a configuration service and being able to create microservices that communicate between themselves, we are going to see next how to create a service registry.
The full code is available on github.

Overview

I have heard several times the assimilation of a service registry with an address phone book on the cloud.
As you incorporate more and more services, locating them becomes more and more difficult and unmaintainable (imagine 500 microservices for example, how would you keep track of what is going on?).

On top of that, microservices are pieces of business logic that can be up/down changing their ip/status/address over the network.
Dns is very capable of solving that but it normally requires it to wait some time until the new services are resolved. Changes on addresses on the microservices should be reflected immediately and a good dns normally caches the information (for performance purposes for example).

For our next example, we will create a service registry with Eureka and it will be showed also how to modify the microservices in order to be discovered.

The following diagram represents the architecture we are going to follow:

Microservices - Eureka service registry

Microservices – Eureka service registry

 

Hands on code

During this article, only the part related to configure a Eureka service registry will be explained.  If you miss the post about Centralized configuration service, I recommend to give it a look. As a reminder, the code is available on github.

Configuration microservice

Properties

On the configuration server, we will need to expose the properties for determining where the service registry is going to live. In our case, and following the previous post, we will create a new properties file under the config folder.

File: src/main/resources/config/eureka-service.properties

Eureka-server microservice

The eureka service will be in charge of providing a way to register services to the rest of the microservices. At the same time, it will be used for obtaining information about the microservices registered (ex: a microservice that needs to be aware of the services running at a certain time)

Application

The application will need to have the @EnableEurekaServer annotation. This could go on top of your configuration file.

Properties

As the eureka server is going to be configured by the configuration service, we would need to point to the configuration service and specify under which application name it is going to get the configuration.

File: src/main/resources/bootstrap.properties

Services to be registered: Service {A,B}

Each microservice needs to be able to register to the service registry. Easiest way is having a client that talks to the service registry.

Dependencies

On your pom, you will need to add the eureka artifact spring-cloud-starter-eureka.

Application

In order for the service to register to the Eureka, it will need to have the @EnableDiscoveryClient annotation.

Deployment

After having done the modifications on the configuration server, microservices to register and created the service registry, the deployment will consist for each of the microservices to run the following:

mvn spring-boot:run

The order for deployment is:

  • Configuration server
  • Eureka service
  • Service A and Service B

I normally use jps to check what is running.

Test

Based on our configuration, eureka server is listening on port 8761. To see the services registered at the moment, go to the url: http://localhost:8761/

Eureka service registry web interface

Eureka service registry web interface

On the web interface, you will be able to see all the services registered and the history of the registrations.

Summary

  • On these examples, we are configuring our services through a configuration microservice
  • When you have several microservices, management can become a bit unmaintainable. Having a registry can help locating each of the services. Dns is not an option for development and instant registration.
  • Eureka is one implementation of a service registry open sourced by Netflix OSS.
    • In order to register a service you need to enable client discovery on it.

18 thoughts on “Microservices: Using Eureka as a service registry

Leave a Reply

Your email address will not be published. Required fields are marked *