Spring Boot Interview Questions
Spring Boot
Java
BackendWeb DevelopmentQuestion 25
How do you implement service discovery in a Spring Boot microservice architecture?
Answer:
Implementing service discovery in a Spring Boot microservice architecture involves setting up a service registry and configuring your microservices to register with and discover other services from this registry. Spring Cloud Netflix Eureka is a popular choice for this purpose. Hereβs a detailed guide on how to implement service discovery using Eureka.
1. Set Up Eureka Server
First, create a new Spring Boot project to act as the Eureka Server.
-
Create a Eureka Server Project
- Use Spring Initializr to generate a new Spring Boot project.
- Add dependencies for Eureka Server and Spring Web.
- Dependencies to include:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
-
Configure the Eureka Server
-
In the main application class, add the
@EnableEurekaServer
annotation.@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
-
Configure
application.yml
for the Eureka Server.server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false server: enable-self-preservation: false
-
2. Set Up Eureka Clients (Microservices)
Each microservice that will register with the Eureka Server should be configured as a Eureka client.
-
Create Microservice Projects
- Use Spring Initializr to create new Spring Boot projects for your microservices.
- Add dependencies for Eureka Client, Spring Web, and other necessary components.
- Dependencies to include:
<dependencies> <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-web</artifactId> </dependency> </dependencies>
-
Configure Each Microservice
-
In the main application class, add the
@EnableEurekaClient
annotation.@SpringBootApplication @EnableEurekaClient public class ProductServiceApplication { public static void main(String[] args) { SpringApplication.run(ProductServiceApplication.class, args); } }
-
Configure
application.yml
for each microservice to register with the Eureka Server.spring: application: name: product-service eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
-
3. Using RestTemplate for Inter-service Communication
-
Enable Load Balancing with Ribbon
- Add the
@LoadBalanced
annotation to aRestTemplate
bean to enable client-side load balancing.@Configuration public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
- Add the
-
Make REST Calls Using Service Names
- Use the service name to make REST calls.
@Service public class ProductService { @Autowired private RestTemplate restTemplate; public Inventory getInventory(Long productId) { return restTemplate.getForObject("http://inventory-service/inventory/" + productId, Inventory.class); } }
- Use the service name to make REST calls.
4. Setting Up an API Gateway
An API Gateway is often used to route requests to the appropriate microservices.
-
Create API Gateway Project
- Use Spring Initializr to create a new Spring Boot project for the API Gateway.
- Add dependencies for Spring Cloud Gateway and Eureka Client.
- Dependencies to include:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
-
Configure the API Gateway
-
In the main application class, add the
@EnableEurekaClient
annotation.@SpringBootApplication @EnableEurekaClient public class ApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayApplication.class, args); } }
-
Configure
application.yml
for routing requests to different services.spring: application: name: api-gateway eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ spring: cloud: gateway: routes: - id: product-service uri: lb://product-service predicates: - Path=/products/** - id: inventory-service uri: lb://inventory-service predicates: - Path=/inventory/**
-
5. Running the System
- Start the Eureka Server: Run the Eureka Server application.
- Start Microservices: Run each microservice application. They should register themselves with the Eureka Server.
- Start API Gateway: Run the API Gateway application.
6. Testing the Setup
- Access Eureka Dashboard: Open a web browser and go to
http://localhost:8761
to see the Eureka dashboard, which shows all registered services. - Access Microservices via API Gateway:
- Try accessing the product service via the API Gateway:
curl http://localhost:8080/products
- Similarly, access other microservices through the API Gateway.
- Try accessing the product service via the API Gateway:
Summary
Implementing service discovery in a Spring Boot microservice architecture involves:
- Setting up a Eureka Server to act as a service registry.
- Configuring each microservice as a Eureka client to register with the Eureka Server.
- Using
RestTemplate
or Feign clients with load balancing to enable inter-service communication. - Optionally setting up an API Gateway to manage and route requests to the appropriate microservices.
By following these steps, you can achieve a scalable and resilient microservice architecture with service discovery and load balancing capabilities.