DEV Community

Nivas Mane
Nivas Mane

Posted on

Building Scalable Microservices with Spring Boot & Spring Cloud

Image description
In today's fast-moving software world, monolithic applications often fall short when it comes to agility, scalability, and resilience. Microservices architecture helps address these challenges by splitting a large application into smaller, manageable services. In this guide, we will walk through building scalable microservices using Spring Boot, Spring Cloud, Eureka Server, and API Gateway.

✨ Why Microservices?

Easier to develop, test, and deploy

Scalable independently

Fault isolation and better resilience

Technology agnostic for each service

πŸ”§ Tech Stack

Java 17+

Spring Boot 3.x

Spring Cloud Netflix Eureka

Spring Cloud Gateway

Spring Web

Spring Boot Actuator

Docker (optional for deployment)

πŸ“… Step 1: Setup Eureka Discovery Server

Add Dependencies (pom.xml):

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

Main Class:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

application.yml:

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
Enter fullscreen mode Exit fullscreen mode

πŸš€ Step 2: Create Microservice (e.g., User Service)

Add Dependencies:

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

application.yml:

spring:
  application:
    name: user-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8081
Enter fullscreen mode Exit fullscreen mode

Sample Controller:

@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping
    public List<String> getUsers() {
        return List.of("Alice", "Bob", "Charlie");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸšͺ Step 3: Setup Spring Cloud Gateway

Add Dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

application.yml:

spring:
  application:
    name: api-gateway

  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/users/**

server:
  port: 8080

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
Enter fullscreen mode Exit fullscreen mode

Now, when you hit http://localhost:8080/users, the request will route through the gateway to user-service.

πŸ“ˆ Step 4: Load Balancing

Spring Cloud Gateway uses Ribbon (or Spring Cloud LoadBalancer) behind the scenes to do client-side load balancing. If you deploy multiple instances of your microservice, requests will be automatically balanced.

🚜 Step 5: Add Health Check & Monitoring

Add Actuator:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.yml:

management:
  endpoints:
    web:
      exposure:
        include: health,info
Enter fullscreen mode Exit fullscreen mode

Use /actuator/health endpoint to monitor service status.

πŸ“Œ Best Practices

Use config server for centralized configurations

Add Circuit Breaker for resilience (Resilience4j/Hystrix)

Use API Gateway for centralized security

Log tracing with Sleuth + Zipkin

😎 Conclusion

With Spring Boot and Spring Cloud, you can quickly build a scalable and resilient microservices system. In this post, we covered service discovery with Eureka, routing with API Gateway, and how to load balance between instances. Stay tuned for the next article in this series!

If you enjoyed this, follow me on Dev.to and check out the next blog: Securing Microservices with Spring Security & OAuth2 (Okta)

Top comments (0)