Topic 8: – 4 steps of Building Microservices with Spring Boot

image 2

Play Store Application link – Spring Boot in 13 steps – App on Google Play


Step 1: Setting Up Microservices

Create Service Projects Using Spring Initializr

Github project link (Order service ) – https://github.com/kuldeep101990/Building-Microservices-with-Spring-Boot-Order

Github project link (Project service ) – https://github.com/kuldeep101990/Building-Microservices-with-Spring-Boot-Product

  1. Go to https://start.spring.io/.
  2. Create the following projects:
    • Service 1: Product Service
      • Dependencies: Spring Web, Spring Boot DevTools
    • Service 2: Order Service
      • Dependencies: Spring Web, Spring Boot DevTools
  3. Import both projects into your IDE as Maven projects.

Create REST Endpoints

Product Service

package com.example.productservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
    @GetMapping("/products")
    public String getProducts() {
        return "List of Products";
    }
}

Order Service

package com.example.orderservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
    @GetMapping("/orders")
    public String getOrders() {
        return "List of Orders";
    }
}

Run each service on different ports (e.g., Product Service: 8081, Order Service: 8082).


Step 2: Adding Service Discovery with Spring Cloud Eureka

Github project link – https://github.com/kuldeep101990/Service-Discovery

Create Eureka Server

  1. Go to Spring Initializr and create a new project:
    • Dependencies: Spring Cloud Eureka Server, Spring Boot DevTools

Configure Eureka Server

Add dependencies in pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Enable Eureka Server:

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);
    }
}

Configure application.properties:

server.port=8761
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Register Services with Eureka

Add the following dependency to the Product Service and Order Service:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Add Eureka configuration in each service’s application.properties:

Product Service

server.port=8081
spring.application.name=product-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

Order Service

server.port=8082
spring.application.name=order-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

Start the Eureka Server, Product Service, and Order Service. Verify that the services are registered in the Eureka dashboard at http://localhost:8761.


Step 3: Implementing API Gateway with Spring Cloud Gateway

Github project link – https://github.com/kuldeep101990/Spring-Cloud-Gateway

Create API Gateway Project

  1. Go to Spring Initializr and create a project:
    • Dependencies: Spring Cloud Gateway, Spring Boot DevTools

Configure API Gateway

Add the following routes in application.properties:

server.port=8080
spring.application.name=api-gateway
spring.cloud.gateway.routes[0].id=product-service
spring.cloud.gateway.routes[0].uri=lb://product-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/products/**
spring.cloud.gateway.routes[1].id=order-service
spring.cloud.gateway.routes[1].uri=lb://order-service
spring.cloud.gateway.routes[1].predicates[0]=Path=/orders/**

Start the API Gateway. Access endpoints via:

  • http://localhost:8080/products
  • http://localhost:8080/orders

Step 4: Centralized Configuration with Spring Cloud Config

Github project link – https://github.com/kuldeep101990/Spring-Cloud-Config

Create Config Server

  1. Go to Spring Initializr and create a project with the following dependencies:
    • Spring Cloud Config Server
    • Spring Boot DevTools
  2. Enable Config Server:
    In the ConfigServerApplication.java class, enable the Config Server by adding the
    @EnableConfigServer annotation.

    package com.example.configserver;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {
    public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args);
    }
    }
  3. Configure application.properties:
    Update the application.properties file to configure the local folder for storing configurations.
    server.port=8888
    spring.application.name=config-server spring.cloud.config.server.local.uri=file:///C:/config-repo
    • Explanation:
      • file:///C:/config-repo: Points to the local folder where the configuration files are stored. Update the path based on your system and folder location.
        download folder from here – https://github.com/kuldeep101990/config-repo-folder
      • Make sure the folder contains the configuration files for each service (e.g., product-service.properties, order-service.properties).

Connect Services to the Config Server

  1. Update Product Service and Order Service:
    • Add the Spring Cloud Config Client dependency to both Product Service and Order Service‘s pom.xml

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
  2. Configure Services to Use Config Server:
    • In each service’s application.properties, point to the local Config Server:
    spring.cloud.config.uri=http://localhost:8888

Start the Config Server and Services

  1. Run Config Server:
    • Start the Config Server (running on localhost:8888). It will read the configurations from the local folder you specified (C:/config-repo).
  2. Run Product Service and Order Service:
    • Start your Product Service and Order Service. They will fetch their configurations from the Config Server and apply them accordingly.

Test the Setup

  1. Access the Configured Services:
    • The Product Service and Order Service will fetch their configurations from the local Config Server and apply them accordingly.
    • You can modify the application.properties files in the local folder, and the services will automatically pick up the changes without needing to restart the Config Server.

Note – Run these applications in below order-

  1. Configuration Server (Step 4):
    • This server will provide the settings (like application.properties) for the Product Service, Order Service, and API Gateway.
    • These settings will be picked up from a local folder called config-repo.
  2. Service Discovery (Step 2):
    • This is like a phonebook where services can register themselves and find each other.
    • The Product Service and Order Service will register themselves on the Eureka Server so other services (like the API Gateway) can find them.
    • Example URL for Eureka Server: http://localhost:8761.
  3. Product and Order Services (Step 1):
    • These are the main services that handle specific tasks:
      • Product Service runs on http://localhost:8081.
      • Order Service runs on http://localhost:8082.
  4. API Gateway (Step 3):
    • This acts as a single entry point to access both services through one common URL.
    • Instead of accessing Product Service or Order Service directly, you can access them via the API Gateway at http://localhost:8080.
    • Example URLs via the gateway:
      • http://localhost:8080/products (to access Product Service).
      • http://localhost:8080/orders (to access Order Service).

Conclusion

This guide covered the essential steps to build microservices with Spring Boot:

  1. Creating microservices.
  2. Integrating service discovery using Spring Cloud Eureka.
  3. Implementing an API Gateway with Spring Cloud Gateway.
  4. Centralizing configuration with Spring Cloud Config.

Leave a Reply

Your email address will not be published. Required fields are marked *