Skip to content
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

Microservices pattern : Self-Registration #3245

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

naman-sriv
Copy link

Pull Request Template

What does this PR do?

Implemented the Microservices Self-Registration pattern using Spring Boot (3.4.4) and Eureka. This PR includes the Eureka Server setup and two microservices (Greeting and Context) that register with Eureka and communicate using OpenFeign. Key Spring Cloud dependencies include Eureka Discovery Client, Eureka Server, and OpenFeign.

Copy link

github-actions bot commented Apr 7, 2025

PR Summary

This PR implements a microservices self-registration pattern using Spring Boot, Eureka, and OpenFeign. It includes a Eureka server and two microservices (Greeting and Context) that register with Eureka and communicate with each other. The project also includes unit tests and health checks.

Changes

File Summary
microservices-self-registration/README.md This file provides a comprehensive guide to the microservices self-registration pattern implemented in the project, including its intent, structure, usage instructions, advantages, and trade-offs. It also includes references to relevant documentation.
microservices-self-registration/application.log.2025-04-09.0.gz New file.
microservices-self-registration/contextservice/.gitattributes New file.
microservices-self-registration/contextservice/.gitignore New file.
microservices-self-registration/contextservice/application.log.2025-04-05.0.gz New file.
microservices-self-registration/contextservice/application.log.2025-04-07.0.gz New file.
microservices-self-registration/contextservice/application.log.2025-04-09.0.gz New file.
microservices-self-registration/contextservice/pom.xml This file defines the project dependencies for the contextservice microservice, including Spring Boot, Spring Cloud, Eureka, OpenFeign, and testing libraries.
microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/ContextserviceApplication.java This is the main application class for the contextservice microservice. It uses annotations to enable service discovery and OpenFeign client functionality.
microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java This class implements a custom health check for the contextservice that runs every 5 seconds and logs the health status.
microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/client/GreetingServiceClient.java This interface defines a Feign client for communication with the greetingservice using OpenFeign.
microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/controller/ContextController.java This controller handles requests to the /context endpoint, which retrieves a greeting from the greetingservice and adds contextual information.
microservices-self-registration/contextservice/src/main/resources/application.yml This file contains the configuration for the contextservice microservice, including server port, Eureka server URL, and user region.
microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java This class contains unit tests for the ContextController, verifying its functionality and integration with the GreetingServiceClient.
microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextserviceApplicationTests.java This class contains a basic integration test to verify that the Spring context loads correctly for the contextservice.
microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/TestConfig.java This class provides a configuration for unit testing, including a mock implementation of the GreetingServiceClient.
microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java This class contains unit tests for the custom health check, verifying its functionality in both healthy and unhealthy states.
microservices-self-registration/eurekaserver/.gitattributes New file.
microservices-self-registration/eurekaserver/.gitignore New file.
microservices-self-registration/eurekaserver/pom.xml This file defines the project dependencies for the Eureka server, including Spring Boot and Spring Cloud.
microservices-self-registration/eurekaserver/src/main/java/com/learning/eurekaserver/EurekaserverApplication.java This is the main application class for the Eureka server. It uses annotations to enable Eureka server functionality.
microservices-self-registration/eurekaserver/src/main/resources/application.yml This file contains the configuration for the Eureka server, including server port and self-preservation settings.
microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java This class contains a basic integration test to verify that the Spring context loads correctly for the Eureka server.
microservices-self-registration/greetingservice/.gitattributes New file.
microservices-self-registration/greetingservice/.gitignore New file.
microservices-self-registration/greetingservice/application.log.2025-04-05.0.gz New file.
microservices-self-registration/greetingservice/application.log.2025-04-07.0.gz New file.
microservices-self-registration/greetingservice/application.log.2025-04-09.0.gz New file.
microservices-self-registration/greetingservice/application.log.2025-04-11.0.gz New file.
microservices-self-registration/greetingservice/pom.xml This file defines the project dependencies for the greetingservice microservice, including Spring Boot, Spring Cloud, and Eureka.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (4)
  • b902012: Merge branch 'iluwatar:master' into master
  • afc7862: unit tests added for controllers
  • 6ad64ff: README.md updated
  • 84042e8: Services and information
Files Processed (30)
  • microservices-self-registration/README.md (1 hunk)
  • microservices-self-registration/contextservice/.gitattributes (1 hunk)
  • microservices-self-registration/contextservice/.gitignore (1 hunk)
  • microservices-self-registration/contextservice/.mvn/wrapper/maven-wrapper.properties (1 hunk)
  • microservices-self-registration/contextservice/application.log.2025-04-05.0.gz (0 hunks)
  • microservices-self-registration/contextservice/mvnw (1 hunk)
  • microservices-self-registration/contextservice/mvnw.cmd (1 hunk)
  • microservices-self-registration/contextservice/pom.xml (1 hunk)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/ContextserviceApplication.java (1 hunk)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java (1 hunk)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/client/GreetingServiceClient.java (1 hunk)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/controller/ContextController.java (1 hunk)
  • microservices-self-registration/contextservice/src/main/resources/application.yml (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextserviceApplicationTests.java (1 hunk)
  • microservices-self-registration/eurekaserver/.gitattributes (1 hunk)
  • microservices-self-registration/eurekaserver/.gitignore (1 hunk)
  • microservices-self-registration/eurekaserver/.mvn/wrapper/maven-wrapper.properties (1 hunk)
  • microservices-self-registration/eurekaserver/mvnw (1 hunk)
  • microservices-self-registration/eurekaserver/mvnw.cmd (1 hunk)
  • microservices-self-registration/eurekaserver/pom.xml (1 hunk)
  • microservices-self-registration/eurekaserver/src/main/java/com/learning/eurekaserver/EurekaserverApplication.java (1 hunk)
  • microservices-self-registration/eurekaserver/src/main/resources/application.yml (1 hunk)
  • microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java (1 hunk)
  • microservices-self-registration/greetingservice/.gitattributes (1 hunk)
  • microservices-self-registration/greetingservice/.gitignore (1 hunk)
  • microservices-self-registration/greetingservice/.mvn/wrapper/maven-wrapper.properties (1 hunk)
  • microservices-self-registration/greetingservice/application.log.2025-04-05.0.gz (0 hunks)
  • microservices-self-registration/greetingservice/mvnw (1 hunk)
  • microservices-self-registration/greetingservice/mvnw.cmd (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

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

Add the new module to parent pom.xml, otherwise it won't be built by CI

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • 93c6653: Merge branch 'iluwatar:master' into master
Files Processed (5)
  • microservices-self-registration/README.md (1 hunk)
  • microservices-self-registration/contextservice/application.log.2025-04-05.0.gz (0 hunks)
  • microservices-self-registration/contextservice/pom.xml (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java (1 hunk)
  • microservices-self-registration/greetingservice/application.log.2025-04-05.0.gz (0 hunks)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (3)
Files Processed (5)
  • microservices-self-registration/contextservice/application.log.2025-04-07.0.gz (0 hunks)
  • microservices-self-registration/contextservice/pom.xml (1 hunk)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java (1 hunk)
  • microservices-self-registration/eurekaserver/pom.xml (1 hunk)
  • microservices-self-registration/greetingservice/application.log.2025-04-07.0.gz (0 hunks)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (2)
  • microservices-self-registration/contextservice/pom.xml (1 hunk)
  • microservices-self-registration/eurekaserver/pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

@naman-sriv naman-sriv requested a review from iluwatar April 9, 2025 17:29
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • c2c13ae: Merge branch 'iluwatar:master' into master
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

Copy link
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

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

  • Add microservices-self-registration to parent pom.xml. Otherwise it's not built by CI.
  • Remove Maven wrappers

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • 17d019f: Merge branch 'iluwatar:master' into master
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (3)
Files Processed (2)
  • microservices-self-registration/contextservice/application.log.2025-04-09.0.gz (0 hunks)
  • microservices-self-registration/greetingservice/application.log.2025-04-09.0.gz (0 hunks)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • d06d61e: POM file updated for self-registration
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

correction to mudule name
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)

correction to mudule name

Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (2)
Files Processed (4)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/controller/ContextController.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextserviceApplicationTests.java (1 hunk)
  • microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java [28-35]

    enhancement: "Improve ContextControllerTest test robustness."

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • ab2c8ac: sonarqube comments on coverage addressed
Files Processed (3)
  • microservices-self-registration/application.log.2025-04-09.0.gz (0 hunks)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/TestConfig.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java [20-50]

    best practice: "Use @WebMvcTest for unit tests."

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • c4b33cd: Code coverage for custom health checks
Files Processed (1)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java (1 hunk)
Actionable Comments (1)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java [40-59]

    possible bug: "Flaky test due to time dependency."

Skipped Comments (0)

Comment on lines 40 to 59
// Force performHealthCheck to return true (by time simulation - might be flaky)
long currentTimeForTrue = System.currentTimeMillis();
while (currentTimeForTrue % 10000 >= 5000) {
Thread.sleep(10); // Wait until time is in the "true" range
currentTimeForTrue = System.currentTimeMillis();
}
ReflectionTestUtils.invokeMethod(healthCheck, "performHealthCheck");
healthCheck.updateHealthStatus();
assertTrue((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be true");

// Force performHealthCheck to return false (by time simulation - might be flaky)
long currentTimeForFalse = System.currentTimeMillis();
while (currentTimeForFalse % 10000 < 5000) {
Thread.sleep(10); // Wait until time is in the "false" range
currentTimeForFalse = System.currentTimeMillis();
}
ReflectionTestUtils.invokeMethod(healthCheck, "performHealthCheck");
healthCheck.updateHealthStatus();
assertFalse((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be false");
}

Choose a reason for hiding this comment

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

The test relies on time-based conditions (lines 40-45 and 50-55), which can lead to flaky test results. Consider refactoring to remove the time dependency or use a mocking framework to simulate the behavior of performHealthCheck.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (3)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java (1 hunk)
  • microservices-self-registration/eurekaserver/pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java [26-29]

    possible issue: "Improve health check simulation."

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (1)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java [1-43]

    enhancement: "Add negative test cases for health check."

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • fcf605d: changes to for code coverage improvement
Files Processed (5)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java (1 hunk)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/client/GreetingServiceClient.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java (1 hunk)
  • microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/myCustomHealthCheckTest.java (1 hunk)
  • microservices-self-registration/greetingservice/application.log.2025-04-11.0.gz (0 hunks)
Actionable Comments (0)
Skipped Comments (1)
  • microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java [26-29]

    possible issue: "Improve health check simulation."

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)
8.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Copy link
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

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

Additionally, please remove the log archives such as:

microservices-self-registration/application.log.2025-04-09.0.gz
microservices-self-registration/contextservice/application.log.2025-04-05.0.gz
microservices-self-registration/contextservice/application.log.2025-04-07.0.gz
microservices-self-registration/contextservice/application.log.2025-04-09.0.gz

We don't need those stored in the repository.

<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

@Component("myCustomHealthCheck")
public class MyCustomHealthCheck implements HealthIndicator {

private static final Logger log = LoggerFactory.getLogger(MyCustomHealthCheck.class);
Copy link
Owner

Choose a reason for hiding this comment

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

Use Lombok annotation @slf4j for logging


boolean performHealthCheck() {
boolean current = System.currentTimeMillis() % 10000 < 5000; // Simulate fluctuating health
log.debug("Performing health check, current status: {}", current);
Copy link
Owner

Choose a reason for hiding this comment

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

Remove debug-level logging not to spam the output

Comment on lines +43 to +62
<properties>
<java.version>21</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Copy link
Owner

Choose a reason for hiding this comment

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

I think you can use the maven-compiler-plugin from the parent pom.xml, so all this can be left out. This helps in maintenance work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants