diff --git a/microservices-self-registration/README.md b/microservices-self-registration/README.md
new file mode 100644
index 000000000000..bfc837817c57
--- /dev/null
+++ b/microservices-self-registration/README.md
@@ -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
\ No newline at end of file
diff --git a/microservices-self-registration/application.log.2025-04-09.0.gz b/microservices-self-registration/application.log.2025-04-09.0.gz
new file mode 100644
index 000000000000..d51965d73d7a
Binary files /dev/null and b/microservices-self-registration/application.log.2025-04-09.0.gz differ
diff --git a/microservices-self-registration/contextservice/.gitattributes b/microservices-self-registration/contextservice/.gitattributes
new file mode 100644
index 000000000000..3b41682ac579
--- /dev/null
+++ b/microservices-self-registration/contextservice/.gitattributes
@@ -0,0 +1,2 @@
+/mvnw text eol=lf
+*.cmd text eol=crlf
diff --git a/microservices-self-registration/contextservice/.gitignore b/microservices-self-registration/contextservice/.gitignore
new file mode 100644
index 000000000000..549e00a2a96f
--- /dev/null
+++ b/microservices-self-registration/contextservice/.gitignore
@@ -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/
diff --git a/microservices-self-registration/contextservice/application.log.2025-04-05.0.gz b/microservices-self-registration/contextservice/application.log.2025-04-05.0.gz
new file mode 100644
index 000000000000..6ceb03b833a7
Binary files /dev/null and b/microservices-self-registration/contextservice/application.log.2025-04-05.0.gz differ
diff --git a/microservices-self-registration/contextservice/application.log.2025-04-07.0.gz b/microservices-self-registration/contextservice/application.log.2025-04-07.0.gz
new file mode 100644
index 000000000000..eb2a63ce194e
Binary files /dev/null and b/microservices-self-registration/contextservice/application.log.2025-04-07.0.gz differ
diff --git a/microservices-self-registration/contextservice/application.log.2025-04-09.0.gz b/microservices-self-registration/contextservice/application.log.2025-04-09.0.gz
new file mode 100644
index 000000000000..bd773dc8ba59
Binary files /dev/null and b/microservices-self-registration/contextservice/application.log.2025-04-09.0.gz differ
diff --git a/microservices-self-registration/contextservice/pom.xml b/microservices-self-registration/contextservice/pom.xml
new file mode 100644
index 000000000000..ea6d105bd06f
--- /dev/null
+++ b/microservices-self-registration/contextservice/pom.xml
@@ -0,0 +1,73 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 3.4.4
+
+ com.learning
+ contextservice
+ 0.0.1-SNAPSHOT
+ contextservice
+ contextservice
+
+
+ 2024.0.1
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.projectlombok
+ lombok
+ 1.18.38
+ provided
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-eureka-client
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ ${spring-cloud.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+
+
+
\ No newline at end of file
diff --git a/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/ContextserviceApplication.java b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/ContextserviceApplication.java
new file mode 100644
index 000000000000..eb22d094ffce
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/ContextserviceApplication.java
@@ -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);
+ }
+
+}
diff --git a/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java
new file mode 100644
index 000000000000..0226fc50e803
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java
@@ -0,0 +1,42 @@
+package com.learning.contextservice;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.boot.actuate.health.HealthIndicator;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+
+@Component("myCustomHealthCheck")
+public class MyCustomHealthCheck implements HealthIndicator {
+
+ private static final Logger log = LoggerFactory.getLogger(MyCustomHealthCheck.class);
+
+ private volatile boolean isHealthy = true;
+
+ @Scheduled(fixedRate = 5000) // Run every 5 seconds
+ public void updateHealthStatus() {
+ // Perform checks here to determine the current health
+ // For example, check database connectivity, external service availability, etc.
+ isHealthy = performHealthCheck();
+ log.info("Update health status : {}", isHealthy);
+ }
+
+ boolean performHealthCheck() {
+ boolean current = System.currentTimeMillis() % 10000 < 5000; // Simulate fluctuating health
+ log.debug("Performing health check, current status: {}", current);
+ return current; // Simulate fluctuating health
+ }
+
+ @Override
+ public Health health() {
+ if (isHealthy) {
+ log.info("Health check successful, service is UP");
+ return Health.up().withDetail("message", "Service is running and scheduled checks are OK").build();
+ } else {
+ log.warn("Health check failed, service is DOWN");
+ return Health.down().withDetail("error", "Scheduled health checks failed").build();
+ }
+ }
+}
diff --git a/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/client/GreetingServiceClient.java b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/client/GreetingServiceClient.java
new file mode 100644
index 000000000000..367a3bdd496c
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/client/GreetingServiceClient.java
@@ -0,0 +1,11 @@
+package com.learning.contextservice.client;
+
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+
+@FeignClient(name = "greetingservice")
+public interface GreetingServiceClient {
+
+ @GetMapping("/greeting")
+ String getGreeting();
+}
diff --git a/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/controller/ContextController.java b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/controller/ContextController.java
new file mode 100644
index 000000000000..5ad8969e0871
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/controller/ContextController.java
@@ -0,0 +1,26 @@
+package com.learning.contextservice.controller;
+
+import com.learning.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 {
+
+ private final GreetingServiceClient greetingServiceClient;
+ private final String userRegion;
+
+ @Autowired
+ public ContextController(GreetingServiceClient greetingServiceClient, @Value("${user.region}") String userRegion) {
+ this.greetingServiceClient = greetingServiceClient;
+ this.userRegion = userRegion;
+ }
+
+ @GetMapping("/context")
+ public String getContext() {
+ String greeting = greetingServiceClient.getGreeting();
+ return "The Greeting Service says: "+greeting+" from "+userRegion;
+ }
+}
diff --git a/microservices-self-registration/contextservice/src/main/resources/application.yml b/microservices-self-registration/contextservice/src/main/resources/application.yml
new file mode 100644
index 000000000000..dfef73bbbeec
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/main/resources/application.yml
@@ -0,0 +1,25 @@
+server:
+ port: 8082
+
+spring:
+ application:
+ name: contextservice
+
+eureka:
+ client:
+ service-url.defaultZone: http://localhost:8761/eureka
+
+user:
+ region: Chennai, Tamil Nadu, India
+
+management:
+ endpoint:
+ health:
+ show-details: always
+ web:
+ exposure:
+ include: health
+
+logging:
+ file:
+ name: application.log
diff --git a/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java
new file mode 100644
index 000000000000..f11da867cda0
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java
@@ -0,0 +1,49 @@
+package com.learning.contextservice;
+
+import com.learning.contextservice.client.GreetingServiceClient;
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Import;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.bean.override.mockito.MockitoBean;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+
+@SpringBootTest(classes = ContextserviceApplication.class)
+@AutoConfigureMockMvc
+@Import(TestConfig.class)
+class ContextControllerTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockitoBean
+ private GreetingServiceClient greetingServiceClient;
+
+ @Value("${user.region}")
+ private String userRegion;
+
+ @Test
+ void shouldReturnContextGreeting() throws Exception{
+ Mockito.when(greetingServiceClient.getGreeting()).thenReturn("Mocked Hello");
+
+ mockMvc.perform(MockMvcRequestBuilders.get("/context")
+ .accept(MediaType.TEXT_PLAIN))
+ .andExpect(MockMvcResultMatchers.status().isOk())
+ .andExpect(MockMvcResultMatchers.content().string("The Greeting Service says: Mocked Hello from Chennai, Tamil Nadu, India"));
+ }
+
+ @Test
+ void shouldReturnContextServiceHealthStatusUp() throws Exception {
+ mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
+ .andExpect(MockMvcResultMatchers.status().isOk())
+ .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"status\":\"UP\"")));
+ }
+}
diff --git a/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextserviceApplicationTests.java b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextserviceApplicationTests.java
new file mode 100644
index 000000000000..a5d5c869c664
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextserviceApplicationTests.java
@@ -0,0 +1,17 @@
+package com.learning.contextservice;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class ContextserviceApplicationTests {
+
+ @Test
+ void contextLoads() {
+ // This is a basic integration test that checks if the Spring Application Context loads successfully.
+ // If the context loads without any exceptions, the test is considered passing.
+ // It is often left empty as the act of loading the context is the primary verification.
+ // You can add specific assertions here if you want to verify the presence or state of certain beans.
+ }
+
+}
diff --git a/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/TestConfig.java b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/TestConfig.java
new file mode 100644
index 000000000000..f378f46f59df
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/TestConfig.java
@@ -0,0 +1,17 @@
+package com.learning.contextservice;
+
+import com.learning.contextservice.client.GreetingServiceClient;
+import org.mockito.Mockito;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class TestConfig {
+
+ @Bean
+ public GreetingServiceClient greetingServiceClient() {
+ GreetingServiceClient mockClient = Mockito.mock(GreetingServiceClient.class);
+ Mockito.when(mockClient.getGreeting()).thenReturn("Mocked Hello");
+ return mockClient;
+ }
+}
diff --git a/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java
new file mode 100644
index 000000000000..129209469827
--- /dev/null
+++ b/microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java
@@ -0,0 +1,33 @@
+package com.learning.contextservice;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.test.util.ReflectionTestUtils;
+import org.springframework.boot.actuate.health.Status;
+import static org.junit.jupiter.api.Assertions.*;
+
+class MyCustomHealthCheckTest {
+
+ @Test
+ void testHealthUp() {
+ MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
+ // Simulate a healthy state
+ ReflectionTestUtils.setField(healthCheck, "isHealthy", true);
+ Health health = healthCheck.health();
+ assertEquals(Status.UP, health.getStatus());
+ assertTrue(health.getDetails().containsKey("message"));
+ assertEquals("Service is running and scheduled checks are OK", health.getDetails().get("message"));
+ }
+
+ @Test
+ void testHealthDown() {
+ MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
+ // Simulate an unhealthy state
+ ReflectionTestUtils.setField(healthCheck, "isHealthy", false);
+ Health health = healthCheck.health();
+ assertEquals(Status.DOWN, health.getStatus());
+ assertTrue(health.getDetails().containsKey("error"));
+ assertEquals("Scheduled health checks failed", health.getDetails().get("error"));
+ }
+
+}
\ No newline at end of file
diff --git a/microservices-self-registration/eurekaserver/.gitattributes b/microservices-self-registration/eurekaserver/.gitattributes
new file mode 100644
index 000000000000..3b41682ac579
--- /dev/null
+++ b/microservices-self-registration/eurekaserver/.gitattributes
@@ -0,0 +1,2 @@
+/mvnw text eol=lf
+*.cmd text eol=crlf
diff --git a/microservices-self-registration/eurekaserver/.gitignore b/microservices-self-registration/eurekaserver/.gitignore
new file mode 100644
index 000000000000..549e00a2a96f
--- /dev/null
+++ b/microservices-self-registration/eurekaserver/.gitignore
@@ -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/
diff --git a/microservices-self-registration/eurekaserver/pom.xml b/microservices-self-registration/eurekaserver/pom.xml
new file mode 100644
index 000000000000..b1a4b26cf4f4
--- /dev/null
+++ b/microservices-self-registration/eurekaserver/pom.xml
@@ -0,0 +1,55 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 3.4.4
+
+ com.learning
+ eurekaserver
+ 0.0.1-SNAPSHOT
+ eurekaserver
+ eurekaserver
+
+
+ 2024.0.1
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-eureka-server
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ ${spring-cloud.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+
+
+
\ No newline at end of file
diff --git a/microservices-self-registration/eurekaserver/src/main/java/com/learning/eurekaserver/EurekaserverApplication.java b/microservices-self-registration/eurekaserver/src/main/java/com/learning/eurekaserver/EurekaserverApplication.java
new file mode 100644
index 000000000000..80b3d904ff4c
--- /dev/null
+++ b/microservices-self-registration/eurekaserver/src/main/java/com/learning/eurekaserver/EurekaserverApplication.java
@@ -0,0 +1,15 @@
+package com.learning.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);
+ }
+
+}
diff --git a/microservices-self-registration/eurekaserver/src/main/resources/application.yml b/microservices-self-registration/eurekaserver/src/main/resources/application.yml
new file mode 100644
index 000000000000..51f8a815d251
--- /dev/null
+++ b/microservices-self-registration/eurekaserver/src/main/resources/application.yml
@@ -0,0 +1,10 @@
+server:
+ port: 8761
+
+eureka:
+ client:
+ register-with-eureka: false
+ fetch-registry: false
+ server:
+ enable-self-preservation: true
+
diff --git a/microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java b/microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java
new file mode 100644
index 000000000000..b5150fefa940
--- /dev/null
+++ b/microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java
@@ -0,0 +1,17 @@
+package com.learning.eurekaserver;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class EurekaserverApplicationTests {
+
+ @Test
+ void contextLoads() {
+ // This is a basic integration test that checks if the Spring Application Context loads successfully.
+ // If the context loads without any exceptions, the test is considered passing.
+ // It is often left empty as the act of loading the context is the primary verification.
+ // You can add specific assertions here if you want to verify the presence or state of certain beans.
+ }
+
+}
diff --git a/microservices-self-registration/greetingservice/.gitattributes b/microservices-self-registration/greetingservice/.gitattributes
new file mode 100644
index 000000000000..3b41682ac579
--- /dev/null
+++ b/microservices-self-registration/greetingservice/.gitattributes
@@ -0,0 +1,2 @@
+/mvnw text eol=lf
+*.cmd text eol=crlf
diff --git a/microservices-self-registration/greetingservice/.gitignore b/microservices-self-registration/greetingservice/.gitignore
new file mode 100644
index 000000000000..549e00a2a96f
--- /dev/null
+++ b/microservices-self-registration/greetingservice/.gitignore
@@ -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/
diff --git a/microservices-self-registration/greetingservice/application.log.2025-04-05.0.gz b/microservices-self-registration/greetingservice/application.log.2025-04-05.0.gz
new file mode 100644
index 000000000000..93d6a2e62ac1
Binary files /dev/null and b/microservices-self-registration/greetingservice/application.log.2025-04-05.0.gz differ
diff --git a/microservices-self-registration/greetingservice/application.log.2025-04-07.0.gz b/microservices-self-registration/greetingservice/application.log.2025-04-07.0.gz
new file mode 100644
index 000000000000..40c96f702853
Binary files /dev/null and b/microservices-self-registration/greetingservice/application.log.2025-04-07.0.gz differ
diff --git a/microservices-self-registration/greetingservice/application.log.2025-04-09.0.gz b/microservices-self-registration/greetingservice/application.log.2025-04-09.0.gz
new file mode 100644
index 000000000000..59f2cbc3c8df
Binary files /dev/null and b/microservices-self-registration/greetingservice/application.log.2025-04-09.0.gz differ
diff --git a/microservices-self-registration/greetingservice/application.log.2025-04-11.0.gz b/microservices-self-registration/greetingservice/application.log.2025-04-11.0.gz
new file mode 100644
index 000000000000..62d73c3020c1
Binary files /dev/null and b/microservices-self-registration/greetingservice/application.log.2025-04-11.0.gz differ
diff --git a/microservices-self-registration/greetingservice/pom.xml b/microservices-self-registration/greetingservice/pom.xml
new file mode 100644
index 000000000000..45988a145866
--- /dev/null
+++ b/microservices-self-registration/greetingservice/pom.xml
@@ -0,0 +1,69 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 3.4.4
+
+ com.learning
+ greetingservice
+ 0.0.1-SNAPSHOT
+ greetingservice
+ greetingservice
+
+
+ 2024.0.1
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.projectlombok
+ lombok
+ 1.18.38
+ provided
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-eureka-client
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ ${spring-cloud.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+
+
+
\ No newline at end of file
diff --git a/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/GreetingserviceApplication.java b/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/GreetingserviceApplication.java
new file mode 100644
index 000000000000..7a549a084284
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/GreetingserviceApplication.java
@@ -0,0 +1,17 @@
+package com.learning.greetingservice;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+@EnableDiscoveryClient
+@ComponentScan("com.learning.greetingservice.controller")
+public class GreetingserviceApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(GreetingserviceApplication.class, args);
+ }
+
+}
diff --git a/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/MyCustomHealthCheck.java b/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/MyCustomHealthCheck.java
new file mode 100644
index 000000000000..218a4ad002d4
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/MyCustomHealthCheck.java
@@ -0,0 +1,41 @@
+package com.learning.greetingservice;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.boot.actuate.health.HealthIndicator;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Component("myCustomHealthCheck")
+public class MyCustomHealthCheck implements HealthIndicator {
+
+ private static final Logger log = LoggerFactory.getLogger(MyCustomHealthCheck.class);
+
+ private volatile boolean isHealthy = true;
+
+ @Scheduled(fixedRate = 5000) // Run every 5 seconds
+ public void updateHealthStatus() {
+ // Perform checks here to determine the current health
+ // For example, check database connectivity, external service availability, etc.
+ isHealthy = performHealthCheck();
+ log.info("Update health status : {}", isHealthy);
+ }
+
+ boolean performHealthCheck() {
+ boolean current = System.currentTimeMillis() % 10000 < 5000; // Simulate fluctuating health
+ log.debug("Performing health check, current status: {}", current);
+ return current; // Simulate fluctuating health
+ }
+
+ @Override
+ public Health health() {
+ if (isHealthy) {
+ log.info("Health check successful, service is UP");
+ return Health.up().withDetail("message", "Service is running and scheduled checks are OK").build();
+ } else {
+ log.warn("Health check failed, service is DOWN");
+ return Health.down().withDetail("error", "Scheduled health checks failed").build();
+ }
+ }
+}
diff --git a/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/controller/GreetingsController.java b/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/controller/GreetingsController.java
new file mode 100644
index 000000000000..ea385beb1abe
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/controller/GreetingsController.java
@@ -0,0 +1,13 @@
+package com.learning.greetingservice.controller;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class GreetingsController {
+
+ @GetMapping("/greeting")
+ public String getGreeting() {
+ return "Hello";
+ }
+}
diff --git a/microservices-self-registration/greetingservice/src/main/resources/application.yml b/microservices-self-registration/greetingservice/src/main/resources/application.yml
new file mode 100644
index 000000000000..adcfac884c2b
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/main/resources/application.yml
@@ -0,0 +1,22 @@
+server:
+ port: 8081
+
+spring:
+ application:
+ name: greetingservice
+eureka:
+ client:
+ service-url.defaultZone: http://localhost:8761/eureka
+
+management:
+ endpoint:
+ health:
+ show-details: always
+ web:
+ exposure:
+ include: health
+
+logging:
+ file:
+ name: application.log
+
diff --git a/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/GreetingserviceApplicationTests.java b/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/GreetingserviceApplicationTests.java
new file mode 100644
index 000000000000..945898278aa9
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/GreetingserviceApplicationTests.java
@@ -0,0 +1,17 @@
+package com.learning.greetingservice;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class GreetingserviceApplicationTests {
+
+ @Test
+ void contextLoads() {
+ // This is a basic integration test that checks if the Spring Application Context loads successfully.
+ // If the context loads without any exceptions, the test is considered passing.
+ // It is often left empty as the act of loading the context is the primary verification.
+ // You can add specific assertions here if you want to verify the presence or state of certain beans.
+ }
+
+}
diff --git a/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/MyCustomHealthCheckTest.java b/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/MyCustomHealthCheckTest.java
new file mode 100644
index 000000000000..8ba8b2a30f93
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/MyCustomHealthCheckTest.java
@@ -0,0 +1,22 @@
+package com.learning.greetingservice;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.test.util.ReflectionTestUtils;
+import org.springframework.boot.actuate.health.Status;
+import static org.junit.jupiter.api.Assertions.*;
+
+class MyCustomHealthCheckTest {
+
+ @Test
+ void testHealthUp() {
+ MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
+ // Simulate a healthy state
+ ReflectionTestUtils.setField(healthCheck, "isHealthy", true);
+ Health health = healthCheck.health();
+ assertEquals(Status.UP, health.getStatus());
+ assertTrue(health.getDetails().containsKey("message"));
+ assertEquals("Service is running and scheduled checks are OK", health.getDetails().get("message"));
+ }
+
+}
\ No newline at end of file
diff --git a/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/controller/GreetingControllerTest.java b/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/controller/GreetingControllerTest.java
new file mode 100644
index 000000000000..5ae98c8b6aeb
--- /dev/null
+++ b/microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/controller/GreetingControllerTest.java
@@ -0,0 +1,36 @@
+package com.learning.greetingservice.controller;
+
+import com.learning.greetingservice.GreetingserviceApplication;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+
+@SpringBootTest(classes = GreetingserviceApplication.class)
+@AutoConfigureMockMvc
+@ActiveProfiles("test")
+class GreetingControllerTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @Test
+ void shouldReturnGreeting() throws Exception{
+ mockMvc.perform(MockMvcRequestBuilders.get("/greeting")
+ .accept(MediaType.TEXT_PLAIN))
+ .andExpect(MockMvcResultMatchers.status().isOk())
+ .andExpect(MockMvcResultMatchers.content().string("Hello"));
+ }
+
+ @Test
+ void shouldReturnHealthStatusUp() throws Exception{
+ mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
+ .andExpect(MockMvcResultMatchers.status().isOk())
+ .andExpect(MockMvcResultMatchers.content().string(org.hamcrest.Matchers.containsString("\"status\":\"UP\"")));
+ }
+}
diff --git a/microservices-self-registration/pom.xml b/microservices-self-registration/pom.xml
new file mode 100644
index 000000000000..4b708cb0eb12
--- /dev/null
+++ b/microservices-self-registration/pom.xml
@@ -0,0 +1,63 @@
+
+
+
+
+ java-design-patterns
+ com.iluwatar
+ 1.26.0-SNAPSHOT
+
+ 4.0.0
+ microservices-self-registration
+ pom
+
+ eurekaserver
+ greetingservice
+ contextservice
+
+
+
+ 21
+ ${java.version}
+ ${java.version}
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+ ${maven.compiler.source}
+ ${maven.compiler.target}
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index fb0bfd7565e1..48bcd69d6300 100644
--- a/pom.xml
+++ b/pom.xml
@@ -166,6 +166,7 @@
microservices-distributed-tracingmicroservices-idempotent-consumermicroservices-log-aggregation
+ microservices-self-registrationmodel-view-controllermodel-view-intentmodel-view-presenter