Service Discovery with Eureka


Table of Contents

  1. Introduction to Service Discovery
  2. Introduction to Eureka
  3. Setting Up Eureka Server
  4. Registering Microservices with Eureka
  5. Configuring Client-Side Service Discovery
  6. Load Balancing and Ribbon Integration
  7. Conclusion

1. Introduction to Service Discovery

In a microservices architecture, services need to discover and communicate with each other dynamically. Service discovery is the process of automatically detecting services in a network and allowing microservices to locate each other.

In traditional monolithic applications, service discovery is not necessary because all components are part of the same application. However, in a distributed system like microservices, services are deployed independently, and their locations (URLs and ports) can change frequently, making it difficult to hardcode addresses.

There are two main types of service discovery:

  • Client-side discovery: The client is responsible for querying the service registry to get the location of a service.
  • Server-side discovery: The client makes a request to a load balancer, which queries the service registry to find the service’s location.

For this module, we’ll be using Eureka, a popular service registry and discovery tool from the Spring Cloud ecosystem.


2. Introduction to Eureka

Eureka is a REST-based service registry for resilient load balancing and failover. It provides a central place for microservices to register and discover each other. Eureka works with both client-side and server-side service discovery mechanisms.

Eureka consists of two main components:

  • Eureka Server: Acts as the service registry where services register themselves. It allows microservices to discover the instances of other microservices.
  • Eureka Client: A Spring Boot application that registers itself with the Eureka server and can discover other registered services.

3. Setting Up Eureka Server

Step 1: Creating a Eureka Server

Start by creating a new Spring Boot application for the Eureka server. You can generate the project using Spring Initializr or manually set it up in your IDE.

Add the following dependencies:

  • Spring Web
  • Spring Cloud Eureka Server

In the pom.xml file, add the Eureka Server dependency:

xmlCopyEdit<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Step 2: Enable Eureka Server

In your EurekaServerApplication.java, enable Eureka Server by adding the @EnableEurekaServer annotation.

javaCopyEditpackage com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Step 3: Configure Eureka Server

In the application.properties (or application.yml), configure the Eureka server settings:

propertiesCopyEditserver.port=8761
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
  • server.port=8761: The port on which the Eureka server will run.
  • spring.application.name=eureka-server: The application name for Eureka.
  • eureka.client.register-with-eureka=false: Disables the registration of the server itself with Eureka.
  • eureka.client.fetch-registry=false: Disables fetching of the registry as the server does not need it.

4. Registering Microservices with Eureka

Step 1: Add Eureka Client to Microservices

In your microservice applications (e.g., microservice-demo and microservice-consumer), you need to add Eureka Client dependency.

In the pom.xml file, add the Eureka Client dependency:

xmlCopyEdit<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Step 2: Enable Eureka Client

In the application.properties of your microservice, configure it to register with the Eureka server. You also need to specify the Eureka server’s location.

propertiesCopyEditspring.application.name=microservice-demo
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
  • spring.application.name: The name of the microservice, which will be used for service discovery.
  • eureka.client.service-url.defaultZone: The Eureka server’s URL, where your microservices will register themselves.

Step 3: Enable Eureka Client in the Application

In your MicroserviceDemoApplication.java, add the @EnableEurekaClient annotation to enable the microservice to register with Eureka.

javaCopyEditpackage com.example.microservicedemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class MicroserviceDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceDemoApplication.class, args);
    }
}

5. Configuring Client-Side Service Discovery

Once your microservices are registered with Eureka, they can discover and communicate with each other dynamically. Eureka allows services to query the registry and obtain the addresses of other services.

Step 1: Use @LoadBalanced RestTemplate

In the microservice that will call another service, you need to create a RestTemplate bean with the @LoadBalanced annotation. This enables client-side load balancing using Eureka.

In the AppConfig.java of the calling microservice, define a RestTemplate bean:

javaCopyEditpackage com.example.microserviceconsumer;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}

Step 2: Access Service Using Service Name

Instead of using hardcoded URLs, use the service name registered with Eureka in the RestTemplate call. For example, if the service name is microservice-demo, use:

javaCopyEditString serviceUrl = "http://microservice-demo/hello";
return restTemplate.getForObject(serviceUrl, String.class);

This will allow Spring Cloud to use Eureka to resolve the service name to the correct IP and port.


6. Load Balancing and Ribbon Integration

Spring Cloud integrates with Ribbon for client-side load balancing. When you make requests to a service, Ribbon will automatically distribute the requests across the available instances of that service.

For example, if multiple instances of microservice-demo are running, Ribbon will round-robin the requests across those instances.

To enable Ribbon, you don’t need to add anything extra since the @LoadBalanced RestTemplate we defined earlier automatically integrates Ribbon for load balancing.


7. Conclusion

In this module, we’ve covered:

  • Eureka Server: How to set up Eureka as a service registry.
  • Eureka Client: How to register microservices with Eureka.
  • Service Discovery: How services can discover each other using Eureka and make REST calls using service names.
  • Load Balancing: Basic integration with Ribbon for load balancing across service instances.

Eureka and service discovery are essential parts of building robust, scalable, and resilient microservices architectures. They allow microservices to locate and communicate with each other dynamically without relying on hardcoded configurations.