How to implement a Service Discovery with Spring Cloud Eureka

In this article I will implement a Service Discovery with Spring Cloud Eureka. For that, I will create a Spring Boot microservice with Eureka Server and the others will use Eureka Client and Feign Client.

Content:

  • The Service Discovery design pattern
  • The Service Discovery with Eureka Server
  • Configuring the Eureka Clients
  • The Feign Clients

For more details, watch this video.

All the code is available in this repository.

The Service Discovery design pattern

In the previous link, I have a Github project which simulates a bookstore with multiple microservices: the backend-user which is the public one; and the other are private and only accessible through backend-user. And each microservice is started multiple times, in multiple replicas. I will start each service in a different random port to have them in my localhost. So, what does Spring Cloud Netflix Eureka? What does a Service Discovery?

In a microservices architecture, each microservice will need at some point to communicate with the others. To communicate, i used to have a URL and a port to map to the destination service. But what happens when i have multiple instances of a single microservice? The best option is to keep one url but have a Load Balancer in front of the cluster of instances of the same microservice.

The problem here is that i need another service, a Load Balancer, which needs maintenance and knowledge to put it in place. Here comes Spring Cloud Netflix Eureka. This Service Discovery will first register all the available microservices in your architecture. Each microservice, individually will register into the Service Discovery just to tell he’s fine and what is his IP. So, when the microservice A needs to communicate with microservice B, it will first ask about B address at the Service Discovery, then the microservice A will use the address given by the Service Discovery to reach the microservice B.

The Service Discovery will check the health of each microservice frequently. This way, if a single microservice becomes unavailable, it won’t redirect the traffic to it. And when it receives the registration of multiple instances of the same microservice, it will return the address of a different one each time, to avoid saturating a single instance.

The Service Discovery with Eureka Server

Let’s now implement all of this. I will need a new microservice: the Service Discovery itself. I’m gonna build it directly from Spring Initializr. All the dependencies i need are Spring Boot Starter Web and Netflix Eureka Server.

Spring Initializr
Spring Initializr

Let’s unzip it and add now some basic configuration.

-- service-discovery/src/main/resources/application.yml

server.port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

This is to avoid the server registering to itself. I will also need to configure the default port of the Eureka server.

Configuring the Eureka Clients

Now that i have the server ready, let’s configure the clients. For that, i need two dependencies: Eureka Client and the Feign Client.

-- backend-user/pom.xml

        <dependencies>
                <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
			<version>2.2.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>1.4.7.RELEASE</version>
		</dependency>
                ...
        </dependencies>

The Eureka Client is the one which will register the microservice with Eureka Server. And the Feign Client is the one which will request the other microservices. The Eureka Client will request the other microservices network information and tell it to Feign Client. The Feign Client is basically Retrofit, but directly connected to Eureka Client. The configuration about the addresses, the IP of the other microservices is already set by the Eureka Client.

The Feign Clients

Let’s configure both the Eureka and Feign Clients.

-- backend-user/src/main/resources/application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

I also need those annotations.

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class BackendUserApplication {
     ...
}

And for the Eureka Server, i need the following annotation.

@SpringBootApplication
@EnableEurekaServer
public class ServiceDiscoveryApplication {
     ...
}

Let’s now configure the Feign Client, the Retrofit interfaces.

@FeignClient("service-books")
public interface ServiceBooks {

    @RequestMapping("/books/{bookId}")
    BookDto getBook(@PathVariable("bookId") long bookId);

}

To use these interface, I only need to inject it as a Spring service.

Now that i have the Service Discovery running, i can check in the Eureka Server dashboard (http://localhost:8761/) all the microservices registered.

Spring Eureka interface
Spring Eureka interface

Conclusion

  • I’ve created a dedicated Service Discovery with Eureka Service dependency.
  • Added the Eureka Client dependency to each microservice to register to the server with the configured address.
  • Added the Feign Client to read the Eureka Client information about the other microservices.
  • And use the Feign interfaces to communicate with the other microservices.
  • The Eureka Server will already distribute the requests across all the available similar microservices, to avoid requesting always the same one.

Repository

My New ebook, How to Master Git With 20 Commands, is available now.

2 responses to “How to implement a Service Discovery with Spring Cloud Eureka”

  1. […] a previous article i’ve implemented a backend-user, which was another microservice, but with public access. This […]

    Like

Leave a comment

A WordPress.com Website.