Spring Boot Microservices Basics
Microservices is an architecture where one large application is split into small, independent services. Each service runs on its own, handles one business capability, and communicates with other services over HTTP or messaging.
Monolith vs Microservices
Monolith (single app): Microservices: ────────────────────────────── ──────────────────────────────────────── ┌──────────────────────────┐ ┌──────────┐ ┌──────────┐ ┌────────┐ │ Single Spring Boot App │ │ User │ │ Order │ │Payment │ │ ┌────────────────────┐ │ │ Service │ │ Service │ │Service │ │ │ Users Orders │ │ → └──────────┘ └──────────┘ └────────┘ │ │ Payment Inventory │ │ │ │ │ │ └────────────────────┘ │ └──────────────┴─────────────┘ └──────────────────────────┘ HTTP / Message Bus
When Monolith Makes Sense
- Small teams (1–5 developers)
- Early-stage product with frequent pivots
- Simple domain logic
When Microservices Make Sense
- Different teams own different parts of the system
- Parts need to scale independently (payment service handles 10x more load)
- Parts use different technologies or deployment cycles
Communication Between Services
Synchronous (HTTP REST)
Service A calls Service B and waits for the response:
Order Service needs user details:
OrderService ──GET /users/5──▶ UserService ──▶ returns User JSON
◀────────────────────────────────────── User JSON response
│
continues processing
// In Order Service — calling User Service
@Service
public class OrderService {
private final RestTemplate restTemplate;
public OrderService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public User getUserDetails(Long userId) {
return restTemplate.getForObject(
"http://user-service/api/users/" + userId,
User.class
);
}
}
Asynchronous (Message Queue)
Service A publishes an event. Service B processes it later — no waiting:
Order Service ──"OrderPlaced" event──▶ Message Queue (RabbitMQ/Kafka)
│
▼ (async)
Notification Service reads event
and sends email to customer
Service Discovery with Eureka
With many services, hardcoding URLs is impractical. Eureka is a service registry — each service registers itself, and others look up addresses by name.
┌─────────────────────────────────────┐
│ Eureka Server (Registry) │
│ user-service → 192.168.1.5:8081 │
│ order-service → 192.168.1.6:8082 │
│ payment-service → 192.168.1.7:8083 │
└─────────────────────────────────────┘
▲ ▲
Register on startup Look up service address
│ │
User Service Order Service
Eureka Server Setup
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServer { ... }
Eureka Client (each microservice)
# application.properties spring.application.name=user-service eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApp { ... }
API Gateway
An API Gateway is a single entry point for all client requests. It routes each request to the correct service:
Mobile App / Browser
│
▼
API Gateway (:80)
┌─────────────────────────────────────────────────┐
│ /api/users/** ──▶ user-service:8081 │
│ /api/orders/** ──▶ order-service:8082 │
│ /api/pay/** ──▶ payment-service:8083 │
└─────────────────────────────────────────────────┘
Spring Cloud Gateway is the recommended API gateway for Spring Boot microservices.
Key Spring Cloud Components
Component Purpose ───────────────────────── ───────────────────────────────────────────── Spring Cloud Eureka Service registry and discovery Spring Cloud Gateway API gateway and routing Spring Cloud Config Centralized configuration management Spring Cloud LoadBalancer Client-side load balancing Spring Cloud OpenFeign Declarative HTTP client between services Resilience4j Circuit breaker, retry, rate limiting
Summary
- Microservices split one large app into small, independently deployable services
- Services communicate via REST (synchronous) or message queues (asynchronous)
- Eureka provides service registration and discovery — no hardcoded URLs
- An API Gateway is the single entry point that routes requests to correct services
- Spring Cloud extends Spring Boot with tools built specifically for microservices
