Spring Cloud Microservices Guide (Eureka, API Gateway, Circuit Breaker)
Spring Cloud
Section titled “Spring Cloud”Spring Cloud is a collection of tools for building distributed microservice systems.
Features
Section titled “Features”| Feature | Tool |
|---|---|
| Service Discovery | Eureka |
| API Gateway | Spring Cloud Gateway |
| Load Balancing | Spring LoadBalancer (formerly Ribbon) |
| Circuit Breaker | Resilience4j |
| Distributed Configuration | Spring Cloud Config |
| Distributed Tracing | Micrometer Tracing + Zipkin/OpenTelemetry (Spring Cloud Sleuth on older, pre-Boot-3 stacks) |
Interview Definition
Spring Cloud provides tools and frameworks to implement common microservice patterns such as service discovery, centralized configuration, API gateways, circuit breakers, load balancing, and distributed tracing.
Service Discovery
Section titled “Service Discovery”Service discovery allows microservices to locate one another dynamically without hardcoding URLs.
Without Service Discovery
Section titled “Without Service Discovery”Order Service -> http://localhost:8082/paymentWith Service Discovery
Section titled “With Service Discovery”Order Service -> PAYMENT-SERVICEThe service registry resolves the actual running instance.
Common Tools
Section titled “Common Tools”- Eureka
- Consul
- Apache ZooKeeper
- Kubernetes Service Discovery
Eureka Service Discovery
Section titled “Eureka Service Discovery”Eureka is a Netflix-developed service registry that enables microservices to register themselves and discover other services.
Components
Section titled “Components”- Eureka Server
- Eureka Client
Workflow
Section titled “Workflow”sequenceDiagram
participant U as User Service
participant O as Order Service
participant E as Eureka Server
participant P as Payment Service
U->>E: Register
O->>E: Register
P->>E: Register
O->>E: Discover PAYMENT-SERVICE
E-->>O: Return available instance
O->>P: Invoke API
Eureka Server
Section titled “Eureka Server”@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {}Eureka Client
Section titled “Eureka Client”@EnableEurekaClientIn modern Spring Cloud, this annotation is optional — simply having spring-cloud-starter-netflix-eureka-client on the classpath auto-registers the service with Eureka. @EnableEurekaClient is still accepted for explicitness/backward compatibility.
API Gateway
Section titled “API Gateway”An API Gateway acts as the single entry point for all client requests.
Without Gateway
Section titled “Without Gateway”Client -> User ServiceClient -> Order ServiceClient -> Payment ServiceWith Gateway
Section titled “With Gateway”Client -> API Gateway -> MicroservicesResponsibilities
Section titled “Responsibilities”- Request routing
- Authentication
- Rate limiting
- Logging
- Load balancing
- Request aggregation
Popular Implementations
Section titled “Popular Implementations”- Spring Cloud Gateway
- Netflix Zuul
- Kong
- NGINX
For a full HLD/LLD treatment (component breakdown, class diagram, and Java implementations for routing/auth/rate limiting/load balancing/caching), see the API Gateway system design case study.
Circuit Breaker States
Section titled “Circuit Breaker States”A circuit breaker prevents cascading failures by temporarily stopping calls to failing services — see the existing Resilience4j usage example for a code sample. The three states it moves through:
stateDiagram-v2
[*] --> Closed
Closed --> Open: Failure threshold exceeded
Open --> Half_Open: Wait duration elapsed
Half_Open --> Closed: Successful calls
Half_Open --> Open: Failure
Common Libraries
Section titled “Common Libraries”- Resilience4j
- Hystrix (deprecated)
Overall Spring Boot Microservices Architecture
Section titled “Overall Spring Boot Microservices Architecture”flowchart TD
Client --> Gateway["API Gateway"]
Gateway --> User["User Service"]
Gateway --> Order["Order Service"]
Gateway --> Payment["Payment Service"]
User -. Registers .-> Eureka
Order -. Registers .-> Eureka
Payment -. Registers .-> Eureka
Config["Spring Cloud Config Server"] --> User
Config --> Order
Config --> Payment
Trace["Zipkin / Sleuth"] -. Trace Data .-> User
Trace -. Trace Data .-> Order
Trace -. Trace Data .-> Payment
CB["Resilience4j"] -. Protects .-> Order
Key Takeaways
Section titled “Key Takeaways”- Eureka lets services register themselves and discover each other by name (
PAYMENT-SERVICE) instead of hardcoded host:port URLs. - An API Gateway centralizes routing, authentication, rate limiting, and request aggregation behind a single entry point.
- A circuit breaker moves through three states — Closed (normal), Open (blocking calls after too many failures), and Half-Open (testing recovery) — before returning to Closed.
- A full Spring Cloud microservices setup combines Eureka (discovery), an API Gateway (routing), Spring Cloud Config (configuration), Resilience4j (resilience), and Sleuth/Zipkin (tracing) around the services themselves.