Skip to main content

Module 13: Spring Cloud and Microservices

🎯 Module Objectives​

By the end of this module, you will be able to:

  • βœ… Understand microservices vs monolithic architecture
  • βœ… Set up a Service Discovery with Eureka
  • βœ… Centralize configuration with Spring Cloud Config
  • βœ… Create an API Gateway with Spring Cloud Gateway
  • βœ… Implement inter-service communication with Feign
  • βœ… Apply resilience patterns with Resilience4j
  • βœ… Understand patterns: Circuit Breaker, Retry, Rate Limiter

πŸ“‹ Prerequisites​

Before starting this module, make sure you master:

  • βœ… Module 1-6: Basics of Spring Boot and Spring Data JPA
  • βœ… Module 7: Transactions and configuration
  • βœ… Module 8: Spring Security (to secure services)
  • βœ… Module 12: Docker and deployment (to orchestrate services)
  • βœ… REST API concepts
  • βœ… Docker and Docker Compose (essential!)

πŸ—οΈ Module Architecture​


πŸ“š Module Plan​

Theoretical Chapters​

#ChapterDurationDescription
1Introduction to Microservices45 minMonolith vs Microservices, patterns, 12-Factor App
2Service Discovery - Eureka1hEureka Server/Client, Load Balancing
3Centralized Configuration1hSpring Cloud Config, Git backend, Refresh
4API Gateway1h15Spring Cloud Gateway, Routes, Filters
5Inter-Service Communication1h30Feign, Resilience4j, Circuit Breaker

Practical Work​

#LabDurationDescription
1Eureka Server and Clients1h30Create the service registry
2Config Server1hCentralized Git configuration
3API Gateway1h30Gateway with routing and filters
4Feign + Resilience4j2hResilient communication

Estimated total duration: ~12 hours


🎬 Before / After this Module​

❌ BEFORE (Monolithic Architecture)​

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ MONOLITH APPLICATION β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Users β”‚ β”‚Products β”‚ β”‚ Orders β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ SINGLE DATABASE β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Problems:
- One bug = entire app down
- Scaling: all or nothing
- Deployment: redeploy everything
- Technologies: single stack
- Teams: merge conflicts

βœ… AFTER (Microservices Architecture)​

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Users β”‚ β”‚ Products β”‚ β”‚ Orders β”‚
β”‚ Service β”‚ β”‚ Service β”‚ β”‚ Service β”‚
β”‚ :8081 β”‚ β”‚ :8082 β”‚ β”‚ :8083 β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
β”‚ β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
└───── Eureka Server β”œβ”€β”€β”€β”€β”˜
β”‚ :8761 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ API Gateway β”‚
β”‚ :8080 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Advantages:
- Fault isolation
- Independent scaling
- Continuous deployment
- Polyglot (Java, Node, Python...)
- Autonomous teams

🧩 Spring Cloud Components​


🏒 Use Case: E-Commerce Microservices​

Throughout this module, we will build an e-commerce platform:

Services to Create​

ServiceResponsibilityPortDatabase
eureka-serverService registry8761-
config-serverCentralized configuration8888Git repo
api-gatewaySingle entry point8080-
user-serviceUser management8081PostgreSQL
product-serviceProduct catalog8082PostgreSQL
order-serviceOrder management8083PostgreSQL
inventory-serviceProduct stock8084PostgreSQL
notification-serviceEmails/SMS8085-

Order Flow​


πŸ”‘ Key Concepts​

1. Service Discovery (Why?)​

Without Service Discovery:
- Hardcoded URLs: http://192.168.1.10:8081
- If the service moves β†’ everything breaks
- Manual load balancing

With Eureka:
- Services register: "I am user-service"
- Clients ask: "Where is user-service?"
- Eureka responds with all instances
- Automatic load balancing

2. Centralized Configuration (Why?)​

Without Config Server:
- Each service has its application.properties
- Changing a DB password = modify 10 files
- Redeploy all services

With Config Server:
- Configuration in Git
- One place to modify
- Refresh without restart
- Encryption of secrets

3. API Gateway (Why?)​

Without Gateway:
- Client knows all services
- CORS to manage everywhere
- Auth to implement everywhere
- Rate limiting impossible

With Gateway:
- Single entry point
- Centralized auth
- Rate limiting
- Centralized logging
- Request/response transformation

4. Circuit Breaker (Why?)​

Without Circuit Breaker:
- Service A calls Service B
- Service B is down
- Service A waits... timeout
- Cascade failure β†’ everything down

With Circuit Breaker:
- Detects failures
- "Opens" the circuit
- Returns fallback immediately
- "Closes" when B is back

πŸ“¦ Technical Stack​

Maven Dependencies (BOM)​

<!-- In the parent POM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Compatible Versions​

Spring BootSpring CloudJava
3.2.x2023.0.x (Leyton)17+
3.1.x2022.0.x (Kilburn)17+
3.0.x2022.0.x (Kilburn)17+

⚠️ When to Use Microservices?​

βœ… Good Reasons​

  • Large application with multiple teams
  • Need for different scaling per component
  • Well-defined business domains (Bounded Contexts)
  • Need for resilience (fault isolation)
  • Frequent and independent deployments

❌ Bad Reasons​

  • "It's trendy"
  • Small project / small team
  • MVP / Prototype
  • Team without DevOps experience
  • Limited infrastructure

πŸ’‘ Golden Rule​

"If you don't have the problems of a monolith, you don't need microservices."

β€” Martin Fowler


πŸ—ΊοΈ Module Progression​


πŸš€ Ready to Start?​

This module is advanced and requires a good mastery of previous modules. If you are ready:

  1. Install Docker (essential for this module)
  2. Make sure you have 16GB+ of RAM (multiple services)
  3. Prepare an IDE with Spring support (IntelliJ recommended)

Start Chapter 1: Introduction to Microservices β†’


Back to home