Skip to content

Microservices pattern : Self-Registration #3245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
84042e8
Services and information
naman-sriv Apr 5, 2025
6ad64ff
README.md updated
naman-sriv Apr 6, 2025
afc7862
unit tests added for controllers
naman-sriv Apr 7, 2025
b902012
Merge branch 'iluwatar:master' into master
naman-sriv Apr 7, 2025
13bc41f
pom.xml, clean up, java version update and slf4j logging added
naman-sriv Apr 9, 2025
796b421
Merge branch 'master' of https://github.com/naman-sriv/java-design-pa…
naman-sriv Apr 9, 2025
93c6653
Merge branch 'iluwatar:master' into master
naman-sriv Apr 9, 2025
030643b
Merge branch 'master' of https://github.com/naman-sriv/java-design-pa…
naman-sriv Apr 9, 2025
20d34ec
pom files updated
naman-sriv Apr 9, 2025
c2c13ae
Merge branch 'iluwatar:master' into master
naman-sriv Apr 10, 2025
2bd955d
Removed children maven wrappers
naman-sriv Apr 11, 2025
17d019f
Merge branch 'iluwatar:master' into master
naman-sriv Apr 11, 2025
8b08728
Microservice-self-registration added to pom file
naman-sriv Apr 11, 2025
142fe8b
Merge branch 'master' of https://github.com/naman-sriv/java-design-pa…
naman-sriv Apr 11, 2025
d06d61e
POM file updated for self-registration
naman-sriv Apr 11, 2025
aa788f0
pom file updated
naman-sriv Apr 11, 2025
b47a00e
Update pom.xml
naman-sriv Apr 11, 2025
6b1c680
SonarQube comments addressed
naman-sriv Apr 11, 2025
7d0152b
Merge branch 'master' of https://github.com/naman-sriv/java-design-pa…
naman-sriv Apr 11, 2025
ab2c8ac
sonarqube comments on coverage addressed
naman-sriv Apr 11, 2025
c4b33cd
Code coverage for custom health checks
naman-sriv Apr 11, 2025
edb73c1
changes for unit tests
naman-sriv Apr 11, 2025
84b6676
changes for unit tests
naman-sriv Apr 11, 2025
fcf605d
changes to for code coverage improvement
naman-sriv Apr 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 236 additions & 0 deletions microservices-self-registration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
---
title: "Microservices Self-Registration Pattern in Java with Spring Boot and Eureka"
shortTitle: Microservices Pattern - Self-Registration
description: "Dynamically register and discover Java microservices using Spring Boot and Eureka for resilient, scalable communication."
category: Service Discovery
language: en
tag:
- Microservices
- Self-Registration
- Service Discovery
- Eureka
- Spring Boot
- Spring Cloud
- Java
- Dynamic Configuration
- Resilience
---

## Intent of Microservices Self-Registration Pattern

The intent of the Self-Registration pattern is to enable microservices to automatically announce their presence and location to a central registry (like Eureka) upon startup, simplifying service discovery and allowing other services to find and communicate with them without manual configuration or hardcoded addresses. This promotes dynamic and resilient microservices architectures.

## What's in the Project

This project demonstrates the Microservices Self-Registration pattern using Java, Spring Boot (version 3.4.4), and Eureka for service discovery. It consists of three main components: a Eureka Server and two simple microservices, a Greeting Service and a Context Service, which discover and communicate with each other.

### Project Structure
* **`eureka-server`:** The central service registry where microservices register themselves.
* **`greeting-service`:** A simple microservice that provides a greeting.
* **`context-service`:** A microservice that consumes the greeting from the Greeting Service and adds context.

The **Eureka Server** acts as the discovery service. Microservices register themselves with the Eureka Server, providing their network location.

package 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);
}
}

The **Greeting Service** is a simple microservice that exposes an endpoint to retrieve a greeting.

package com.example.greetingservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GreetingServiceApplication {

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

Greeting Controller

package com.example.greetingservice.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

@GetMapping("/greeting")
public String getGreeting() {
return "Hello";
}
}

The **Context Service** consumes the greeting from the Greeting Service using OpenFeign and adds contextual information.

package com.example.contextservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ContextServiceApplication {

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

Feign Client : Spring Cloud OpenFeign is a declarative HTTP client that makes it easier to consume RESTful web services in your Spring Cloud applications. Instead of writing the boilerplate code for making HTTP requests, you simply declare interface with annotations that describe the web service you want to consume.

package com.example.contextservice.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "greeting-service")
public interface GreetingServiceClient {

@GetMapping("/greeting")
String getGreeting();
}

Context Controller

package com.example.contextservice.controller;

import com.example.contextservice.client.GreetingServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ContextController {

@Autowired
private GreetingServiceClient greetingServiceClient;

@Value("${user.region}")
private String userRegion;

@GetMapping("/context")
public String getContext() {
String greeting = greetingServiceClient.getGreeting();
return "The Greeting Service says: " + greeting + " from " + userRegion + "!";
}
}

1. Both the Greeting Service and the Context Service register themselves with the Eureka Server upon startup using the _@EnableDiscoveryClient_ annotation.
2. The Context Service, annotated with _@EnableFeignClients_, uses the GreetingServiceClient interface with _@FeignClient(name = "greeting-service")_ to declare its intent to communicate with the service named "greeting-service" in Eureka.
3. When the /context endpoint of the Context Service is accessed, it calls the _getGreeting()_ method of the GreetingServiceClient.
4. OpenFeign, leveraging the service discovery information from Eureka, resolves the network location of an available instance of the Greeting Service and makes an HTTP GET request to its /greeting endpoint.
5. The Greeting Service responds with "Hello", and the Context Service then adds the configured user.region to the response.

This project utilizes Spring Boot Actuator, which is included as a dependency, to provide health check endpoints for each microservice. These endpoints (e.g., /actuator/health) can be used by Eureka Server to monitor the health of the registered instances.

## Steps to use for this Project

Prerequisites:
- Java Development Kit (JDK): Make sure you have a compatible JDK installed (ideally Java 17 or later, as Spring Boot 3.x requires it).
- Maven or Gradle: You'll need either Maven (if you chose Maven during Spring Initializr setup) or Gradle (if you chose Gradle) installed on your system.
- An IDE (Optional but Recommended): IntelliJ IDEA, Eclipse, or Spring Tool Suite (STS) can make it easier to work with the project.
- Web Browser: You'll need a web browser to access the Eureka dashboard and the microservice endpoints.

Step :
- You'll need to build each microservice individually. Navigate to the root directory of each project in your terminal or command prompt and run the appropriate build command:
_cd eurekaserver
mvn clean install
cd ../greetingservice
mvn clean install
cd ../contextservice
mvn clean install_
Step :
- Navigate to the root directory of your eurekaserver project in your terminal or command prompt
_mvn spring-boot:run_
- Wait for the Eureka Server application to start. You should see logs in the console indicating that it has started on port 8761 (as configured).
- Open your web browser and go to http://localhost:8761/. You should see the Eureka Server dashboard. Initially, the list of registered instances will be empty.
Step :
- Run the Greeting Service
- Open a new terminal or command prompt.
- Navigate to the root directory of your greetingservice project.
- Run the Spring Boot application: _mvn spring-boot:run_
- Wait for the Greeting Service to start. You should see logs indicating that it has registered with the Eureka Server.
- Go back to your Eureka Server dashboard in the browser (http://localhost:8761/). You should now see GREETINGSERVICE listed under the "Instances currently registered with Eureka". Its status should be "UP".
Step :
- Run the Context Service
- Open a new terminal or command prompt.
- Navigate to the root directory of your contextservice project.
- Run the Spring Boot application: _mvn spring-boot:run_
- Wait for the Context Service to start. You should see logs indicating that it has registered with the Eureka Server.
- Go back to your Eureka Server dashboard in the browser (http://localhost:8761/). You should now see CONTEXTSERVICE listed under the "Instances currently registered with Eureka". Its status should be "UP".
STEP :
- Test the Greeting Service Directly: Open your web browser and go to http://localhost:8081/greeting. You should see the output: Hello.
- Test the Context Service (which calls the Greeting Service): Open your web browser and go to http://localhost:8082/context. You should see the output: The Greeting Service says: Hello from Chennai, Tamil Nadu, India!. This confirms that the Context Service successfully discovered and called the Greeting Service through Eureka.

Optional: Check Health Endpoints

You can also verify the health status of each service using Spring Boot Actuator:
- Greeting Service Health: http://localhost:8081/actuator/health (should return {"status":"UP"})
- Context Service Health: http://localhost:8082/actuator/health (should return {"status":"UP"})
- Eureka Server Health: http://localhost:8761/actuator/health (should return {"status":"UP"})

## When to use Microservices Self-Registration Pattern

- **Dynamic Environments:** When your microservices are frequently deployed, scaled up or down, or their network locations (IP addresses and ports) change often. This is common in cloud-based or containerized environments (like Docker and Kubernetes).
- **Large Number of Services:** As the number of microservices in your system grows, manually managing their configurations and dependencies becomes complex and error-prone. Self-registration automates this process.
- **Need for Automatic Service** Discovery: When services need to find and communicate with each other without hardcoding network locations. This allows for greater flexibility and reduces coupling.
- **Implementing Load Balancing:** Service registries like Eureka often integrate with load balancers, enabling them to automatically distribute traffic across available instances of a service that have registered themselves.
- **Improving System Resilience:** If a service instance fails, the registry will eventually be updated (through heartbeats or health checks), and other services can discover and communicate with the remaining healthy instances.
- **DevOps Automation:** This pattern aligns well with DevOps practices, allowing for more automated deployment and management of microservices.

## Real-World Applications of Self-Registration pattern

- E-Commerce platforms have numerous independent services for product catalogs, order processing, payments, shipping, etc. Self-registration allows these services to dynamically discover and communicate with each other as the system scales during peak loads or as new features are deployed.
- Streaming services rely on many microservices for user authentication, content delivery networks (CDNs), recommendation engines, billing systems, etc. Self-registration helps these services adapt to varying user demands and infrastructure changes.
- Social media These platforms use microservices for managing user profiles, timelines, messaging, advertising, and more. Self-registration enables these services to scale independently and handle the massive traffic they experience.

## Advantages

- Microservices can dynamically locate and communicate with each other without needing to know their specific network addresses beforehand. This is crucial in dynamic environments where IP addresses and ports can change frequently.
- Reduces the need for manual configuration of service locations in each microservice. Services don't need to be updated every time another service's location changes.
- Scaling microservices up or down becomes easier. New instances automatically register themselves with the service registry, making them immediately discoverable by other services without manual intervention.
- If a service instance fails, it will eventually stop sending heartbeats to the registry and will be removed. Consumers can then discover and connect to other healthy instances, improving the system's overall resilience.
- Services are less tightly coupled as they don't have direct dependencies on the physical locations of other services. This makes deployments and updates more flexible.
- Service registries often integrate with load balancers. When a new service instance registers, the load balancer can automatically include it in the pool of available instances, distributing traffic effectively.
- Microservices can be deployed across different environments (development, testing, production) without significant changes to their discovery mechanism, as long as they are configured to connect to the appropriate service registry for that environment.

## Trade-offs

- Introducing a service registry adds another component to your system that needs to be set up, managed, and monitored. This increases the overall complexity of the infrastructure.
- The service registry itself becomes a critical component. If the service registry becomes unavailable, it can disrupt communication between microservices. High availability for the service registry is therefore essential.
- Microservices need to communicate with the service registry for registration, sending heartbeats, and querying for other services. This can lead to increased network traffic.
- There might be a slight delay between when a microservice instance starts and when it becomes fully registered and discoverable in the service registry. This needs to be considered, especially during scaling events.
- You need to consider how your microservices will behave if they fail to register with the service registry upon startup. Robust error handling and retry mechanisms are often necessary.
- Microservices need to include and configure client libraries (like the Eureka Discovery Client) to interact with the service registry. This adds a dependency to your application code.
- In distributed service registries, ensuring consistency of the registry data across all nodes can be a challenge. Different registries might have different consistency models (e.g., eventual consistency).

## References

- Microservices Patterns: https://microservices.io/
- Eureka Documentation: https://github.com/Netflix/eureka | https://spring.io/projects/spring-cloud-netflix
- Spring Boot Documentation: https://spring.io/projects/spring-boot
- Spring Cloud OpenFeignDocumentation: https://spring.io/projects/spring-cloud-openfeign
- Spring Boot Actuator Documentation: https://www.baeldung.com/spring-boot-actuators
2 changes: 2 additions & 0 deletions microservices-self-registration/contextservice/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/mvnw text eol=lf
*.cmd text eol=crlf
33 changes: 33 additions & 0 deletions microservices-self-registration/contextservice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Binary file not shown.
Binary file not shown.
Binary file not shown.
73 changes: 73 additions & 0 deletions microservices-self-registration/contextservice/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> </parent>
<groupId>com.learning</groupId>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use groupId com.iluwatar

<artifactId>contextservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contextservice</name>
<description>contextservice</description>

<properties>
<spring-cloud.version>2024.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.38</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.learning.contextservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ContextserviceApplication {

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

}
Loading
Loading