This website uses cookies to enhance the user experience

Spring Boot Interview Questions

25 Questions
Spring Boot

Spring Boot

Java

Java

BackendWeb Development

Question 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.

  1. 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>
  2. 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.

  1. 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>
  2. 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

  1. Enable Load Balancing with Ribbon

    • Add the @LoadBalanced annotation to a RestTemplate bean to enable client-side load balancing.
      @Configuration
      public class RestTemplateConfig {
          @Bean
          @LoadBalanced
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
      }
  2. 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);
          }
      }

4. Setting Up an API Gateway

An API Gateway is often used to route requests to the appropriate microservices.

  1. 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>
  2. 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

  1. Start the Eureka Server: Run the Eureka Server application.
  2. Start Microservices: Run each microservice application. They should register themselves with the Eureka Server.
  3. Start API Gateway: Run the API Gateway application.

6. Testing the Setup

  1. Access Eureka Dashboard: Open a web browser and go to http://localhost:8761 to see the Eureka dashboard, which shows all registered services.
  2. 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.

Summary

Implementing service discovery in a Spring Boot microservice architecture involves:

  1. Setting up a Eureka Server to act as a service registry.
  2. Configuring each microservice as a Eureka client to register with the Eureka Server.
  3. Using RestTemplate or Feign clients with load balancing to enable inter-service communication.
  4. 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.

Recent job openings

Greece, Athens, Attica

Remote

Full-time

posted 2 days ago

Greece, Palaio Faliro, Attica

Remote

Full-time

posted 2 days ago

United Kingdom, Sheffield City Centre, England

Remote

Full-time

JavaScript

JavaScript

PHP

PHP

+4

posted 2 days ago

France, Rennes, Brittany

Remote

Full-time

posted 2 days ago

Greece, Athens, Attica

Remote

Full-time

Java

Java

SQL

SQL

posted 2 days ago