Skip to content

Commit 6ad64ff

Browse files
committed
README.md updated
1 parent 84042e8 commit 6ad64ff

File tree

1 file changed

+170
-2
lines changed

1 file changed

+170
-2
lines changed

microservices-self-registration/README.md

+170-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,177 @@ tag:
2020

2121
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.
2222

23-
## Example
23+
## What's in the Project
2424

25-
In this example we have implemented two microservices with utmost simplicity, Greeting Service and Context Service, to provide greeting and contextual information respectively.
25+
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.
26+
27+
### Project Structure
28+
* **`eureka-server`:** The central service registry where microservices register themselves.
29+
* **`greeting-service`:** A simple microservice that provides a greeting.
30+
* **`context-service`:** A microservice that consumes the greeting from the Greeting Service and adds context.
31+
32+
The **Eureka Server** acts as the discovery service. Microservices register themselves with the Eureka Server, providing their network location.
33+
34+
package com.example.eurekaserver;
35+
36+
import org.springframework.boot.SpringApplication;
37+
import org.springframework.boot.autoconfigure.SpringBootApplication;
38+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
39+
40+
@SpringBootApplication
41+
@EnableEurekaServer
42+
public class EurekaServerApplication {
43+
44+
public static void main(String[] args) {
45+
SpringApplication.run(EurekaServerApplication.class, args);
46+
}
47+
}
48+
49+
The **Greeting Service** is a simple microservice that exposes an endpoint to retrieve a greeting.
50+
51+
package com.example.greetingservice;
52+
53+
import org.springframework.boot.SpringApplication;
54+
import org.springframework.boot.autoconfigure.SpringBootApplication;
55+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
56+
57+
@SpringBootApplication
58+
@EnableDiscoveryClient
59+
public class GreetingServiceApplication {
60+
61+
public static void main(String[] args) {
62+
SpringApplication.run(GreetingServiceApplication.class, args);
63+
}
64+
}
65+
66+
Greeting Controller
67+
68+
package com.example.greetingservice.controller;
69+
70+
import org.springframework.web.bind.annotation.GetMapping;
71+
import org.springframework.web.bind.annotation.RestController;
72+
73+
@RestController
74+
public class GreetingController {
75+
76+
@GetMapping("/greeting")
77+
public String getGreeting() {
78+
return "Hello";
79+
}
80+
}
81+
82+
The **Context Service** consumes the greeting from the Greeting Service using OpenFeign and adds contextual information.
83+
84+
package com.example.contextservice;
85+
86+
import org.springframework.boot.SpringApplication;
87+
import org.springframework.boot.autoconfigure.SpringBootApplication;
88+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
89+
import org.springframework.cloud.openfeign.EnableFeignClients;
90+
91+
@SpringBootApplication
92+
@EnableDiscoveryClient
93+
@EnableFeignClients
94+
public class ContextServiceApplication {
95+
96+
public static void main(String[] args) {
97+
SpringApplication.run(ContextServiceApplication.class, args);
98+
}
99+
}
100+
101+
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.
102+
103+
package com.example.contextservice.client;
104+
105+
import org.springframework.cloud.openfeign.FeignClient;
106+
import org.springframework.web.bind.annotation.GetMapping;
107+
108+
@FeignClient(name = "greeting-service")
109+
public interface GreetingServiceClient {
110+
111+
@GetMapping("/greeting")
112+
String getGreeting();
113+
}
114+
115+
Context Controller
116+
117+
package com.example.contextservice.controller;
118+
119+
import com.example.contextservice.client.GreetingServiceClient;
120+
import org.springframework.beans.factory.annotation.Autowired;
121+
import org.springframework.beans.factory.annotation.Value;
122+
import org.springframework.web.bind.annotation.GetMapping;
123+
import org.springframework.web.bind.annotation.RestController;
124+
125+
@RestController
126+
public class ContextController {
127+
128+
@Autowired
129+
private GreetingServiceClient greetingServiceClient;
130+
131+
@Value("${user.region}")
132+
private String userRegion;
133+
134+
@GetMapping("/context")
135+
public String getContext() {
136+
String greeting = greetingServiceClient.getGreeting();
137+
return "The Greeting Service says: " + greeting + " from " + userRegion + "!";
138+
}
139+
}
140+
141+
1. Both the Greeting Service and the Context Service register themselves with the Eureka Server upon startup using the _@EnableDiscoveryClient_ annotation.
142+
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.
143+
3. When the /context endpoint of the Context Service is accessed, it calls the _getGreeting()_ method of the GreetingServiceClient.
144+
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.
145+
5. The Greeting Service responds with "Hello", and the Context Service then adds the configured user.region to the response.
146+
147+
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.
148+
149+
## Steps to use for this Project
150+
151+
Prerequisites:
152+
- Java Development Kit (JDK): Make sure you have a compatible JDK installed (ideally Java 17 or later, as Spring Boot 3.x requires it).
153+
- 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.
154+
- An IDE (Optional but Recommended): IntelliJ IDEA, Eclipse, or Spring Tool Suite (STS) can make it easier to work with the project.
155+
- Web Browser: You'll need a web browser to access the Eureka dashboard and the microservice endpoints.
156+
157+
Step :
158+
- 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:
159+
_cd eurekaserver
160+
mvn clean install
161+
cd ../greetingservice
162+
mvn clean install
163+
cd ../contextservice
164+
mvn clean install_
165+
Step :
166+
- Navigate to the root directory of your eurekaserver project in your terminal or command prompt
167+
_mvn spring-boot:run_
168+
- 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).
169+
- 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.
170+
Step :
171+
- Run the Greeting Service
172+
- Open a new terminal or command prompt.
173+
- Navigate to the root directory of your greetingservice project.
174+
- Run the Spring Boot application: _mvn spring-boot:run_
175+
- Wait for the Greeting Service to start. You should see logs indicating that it has registered with the Eureka Server.
176+
- 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".
177+
Step :
178+
- Run the Context Service
179+
- Open a new terminal or command prompt.
180+
- Navigate to the root directory of your contextservice project.
181+
- Run the Spring Boot application: _mvn spring-boot:run_
182+
- Wait for the Context Service to start. You should see logs indicating that it has registered with the Eureka Server.
183+
- 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".
184+
STEP :
185+
- Test the Greeting Service Directly: Open your web browser and go to http://localhost:8081/greeting. You should see the output: Hello.
186+
- 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.
187+
188+
Optional: Check Health Endpoints
189+
190+
You can also verify the health status of each service using Spring Boot Actuator:
191+
- Greeting Service Health: http://localhost:8081/actuator/health (should return {"status":"UP"})
192+
- Context Service Health: http://localhost:8082/actuator/health (should return {"status":"UP"})
193+
- Eureka Server Health: http://localhost:8761/actuator/health (should return {"status":"UP"})
26194

27195
## When to use Microservices Self-Registration Pattern
28196

0 commit comments

Comments
 (0)