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β
| # | Chapter | Duration | Description |
|---|---|---|---|
| 1 | Introduction to Microservices | 45 min | Monolith vs Microservices, patterns, 12-Factor App |
| 2 | Service Discovery - Eureka | 1h | Eureka Server/Client, Load Balancing |
| 3 | Centralized Configuration | 1h | Spring Cloud Config, Git backend, Refresh |
| 4 | API Gateway | 1h15 | Spring Cloud Gateway, Routes, Filters |
| 5 | Inter-Service Communication | 1h30 | Feign, Resilience4j, Circuit Breaker |
Practical Workβ
| # | Lab | Duration | Description |
|---|---|---|---|
| 1 | Eureka Server and Clients | 1h30 | Create the service registry |
| 2 | Config Server | 1h | Centralized Git configuration |
| 3 | API Gateway | 1h30 | Gateway with routing and filters |
| 4 | Feign + Resilience4j | 2h | Resilient 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β
| Service | Responsibility | Port | Database |
|---|---|---|---|
| eureka-server | Service registry | 8761 | - |
| config-server | Centralized configuration | 8888 | Git repo |
| api-gateway | Single entry point | 8080 | - |
| user-service | User management | 8081 | PostgreSQL |
| product-service | Product catalog | 8082 | PostgreSQL |
| order-service | Order management | 8083 | PostgreSQL |
| inventory-service | Product stock | 8084 | PostgreSQL |
| notification-service | Emails/SMS | 8085 | - |
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 Boot | Spring Cloud | Java |
|---|---|---|
| 3.2.x | 2023.0.x (Leyton) | 17+ |
| 3.1.x | 2022.0.x (Kilburn) | 17+ |
| 3.0.x | 2022.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:
- Install Docker (essential for this module)
- Make sure you have 16GB+ of RAM (multiple services)
- Prepare an IDE with Spring support (IntelliJ recommended)
Start Chapter 1: Introduction to Microservices β