microservices-self-registration/README.md
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.
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.
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 + "!";
}
}
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.
Prerequisites:
Step :
Optional: Check Health Endpoints
You can also verify the health status of each service using Spring Boot Actuator: