Concept Explanation
In a monolith, calling another component is a method call. In a distributed system it is a network call — and the first question is where is that service right now?
Hard-coding http://192.168.1.42:8081 breaks the moment the container restarts on a different host. Service discovery solves this: each instance registers itself under a logical name, and callers look it up.
Eureka: The Registry
The server is almost entirely configuration:
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
Each service registers on startup:
spring.application.name=order-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
Now other services call it by name, not address:
@FeignClient(name = "inventory-service")
public interface InventoryClient {
@GetMapping("/api/stock/{sku}")
StockLevel stockFor(@PathVariable String sku);
}
Feign resolves inventory-service through Eureka and load-balances across every registered instance. Scaling from one instance to five requires no code change anywhere.
The API Gateway
Without a gateway, clients must know every service's address, and cross-cutting concerns get reimplemented per service. A gateway gives you one entry point:
spring:
cloud:
gateway:
routes:
- id: orders
uri: lb://order-service # lb:// = load-balanced via discovery
predicates:
- Path=/api/orders/**
- id: inventory
uri: lb://inventory-service
predicates:
- Path=/api/stock/**
The gateway is the natural home for authentication, rate limiting, request logging, and CORS — written once instead of five times.
Circuit Breakers
When a downstream service is slow, every caller thread blocks waiting on it. Threads run out, the caller stops responding, and the failure spreads. A circuit breaker stops the bleeding:
@CircuitBreaker(name = "inventory", fallbackMethod = "unknownStock")
public StockLevel stock(String sku) {
return inventoryClient.stockFor(sku);
}
private StockLevel unknownStock(String sku, Throwable cause) {
log.warn("Inventory unavailable for {}", sku, cause);
return StockLevel.unknown(); // degrade, do not crash
}
Three states: closed (normal), open (failing fast without calling), half-open (probing whether it recovered).
Common Mistakes
- Hard-coded hostnames: They work on your laptop and nowhere else.
- No timeout on inter-service calls: The single most common cause of cascading outages.
- A gateway that holds business logic: It should route and enforce cross-cutting concerns, not compute prices.
- Chatty services: One page triggering twelve internal calls means twelve chances to fail and twelve latencies added.
Best Practices
- Always define a fallback for every remote call. Degraded output beats an error page.
- Propagate a correlation id through every hop so one request can be traced across services.
- Keep the registry highly available — it is a single point of failure if you run one node.
- Version your APIs so services can deploy independently without breaking callers.
Practice Exercises
- The Registry: Start a Eureka server and register two services. Confirm both appear in the dashboard.
- The Route: Configure a gateway route so
/api/orders/**reaches the order service by logical name. - The Open Circuit: Stop a downstream service and verify your fallback method runs instead of an exception surfacing.