JavaMicroservices

Spring Cloud: Eureka, Gateway, and Service Discovery

TT
TopicTrick Team
Spring Cloud: Eureka, Gateway, and Service Discovery

Spring Cloud: Eureka, Gateway, and Service Discovery

"In a distributed system, IP addresses are ephemeral. If your services are hard-coding connections to each other, your architecture is already obsolete."

In a massive enterprise ecosystem, you don't run one instance of a "Payment Service"; you run $50$ of them across different data centers, and they are constantly being moved, scaled, and restarted by your orchestrator. Hard-coding IP addresses or maintaining a manual load balancer configuration is a recipe for catastrophic failure. Netflix Eureka acts as a dynamic "Source of Truth"—the global phone book for your services—while Spring Cloud Gateway acts as the high-performance, non-blocking front door that orchestrates every request.

This 1,500+ word masterclass explores the Service Fabric, the Reactive architecture of the Gateway, and the advanced traffic management patterns (Rate Limiting, Circuit Breakers, and Token Relays) that define modern cloud-native systems in 2026.


1. Eureka Internals: The AP Registry of Truth

In the context of the CAP Theorem (Consistency, Availability, Partition Tolerance), Eureka is an AP System. It chooses Availability and Partition Tolerance over strict Consistency.

Peer-Aware Replication

Eureka servers work in a cluster. When a service registers with Eureka Server A, that registration is asynchronously replicated to Server B and C.

  • The Lifecycle: A service sends a POST request with its metadata (IP, Port, Health check URL).
  • The Renewal (Heartbeat): Every $30$ seconds, the service must "check in." If it misses $3$ renewals, Eureka considers it dead... unless it enters Self-Preservation.

Self-Preservation Mode: The Safety Valve

This is Eureka's most critical feature. If a network glitch occurs and Eureka Server loses connection to 50% of its services, it doesn't delete them. It assumes the Network is the problem, not the services. It enters "Self-Preservation," stopping all evictions to ensure that healthy services are still reachable. Choosing "Stale data" is safer than "No data" in a high-throughput environment.


2. Spring Cloud Gateway: The Non-Blocking Front Door

Older gateways (like Zuul 1) were blocking. If a microservice was slow, the thread in the gateway was held hostage, leading to thread exhaustion. Spring Cloud Gateway is built on Project Reactor and Netty, meaning it can handle $10,000$ concurrent connections with just a few dozen threads.

The Power of Predicates and Filters

  • Predicates: Determine where to route. We can route based on Path (/api/orders/**), Host (payments.topictrick.com), or even specific HTTP Headers.
  • Filters: Modify the request/response.
    • Pre-Filter: Used for global API-key validation and adding a X-Correlation-ID to the headers for tracing.
    • Post-Filter: Used to mask sensitive data (like Credit Card numbers) in the JSON response before it leaves the gateway.

3. Resilience: Circuit Breakers and Retries

In microservices, failure is a certainty. If the "Inventory Service" is down, your "Order Service" shouldn't hang for $30$ seconds and crash.

Implementing Resilience4j

We integrate Resilience4j directly into the Gateway:

  • Circuit Breaker: After $5$ failed attempts, the Gateway "opens" the circuit and returns a fallback response (e.g., "Feature temporarily unavailable") instantly, without hitting the downstream service.
  • Retry Pattern: For transient network errors, the Gateway will automatically retry the request $3$ times before giving up.

4. Security: The Token Relay Pattern

In a secure enterprise, your internal microservices should never be public. The Gateway is the only "Public" entry point. Token Relay ensures that when a user sends a JWT (JSON Web Token) to the Gateway, the Gateway:

  1. Validates the JWT.
  2. Extracts the user's roles.
  3. Injects the token into the headers of the request sent to the internal service.
  • The Result: Your internal microservices (like SHIPPING-SERVICE) don't need complex login logic; they just trust the token passed by the Gateway, maintaining a "Zero-Trust" architecture with minimal code duplication.

5. Traffic Engineering: Rate Limiting and Load Balancing

Distributed Rate Limiting with Redis

To protect against "noisy neighbors" or malicious actors, we implement a Request Rate Limiter.

  • The Algorithm: Token Bucket.
  • The Storage: Redis. By tracking request counts in Redis, all Gateway instances share the same view of a user's rate limit. If a user exceeds $100$ requests per minute, the Gateway returns 429 Too Many Requests in $2$ms, long before the request reaches your expensive business logic.

Client-Side Load Balancing

Instead of a single "Big Load Balancer" in the middle, Spring Cloud uses LoadBalancer. The Gateway fetches the list of IPs from Eureka and chooses one (Round Robin or Weighted) to call directly. This reduces the "Hop Count" and improves latency.


6. Case Study: Deploying to the Global Cloud

When a major streaming service migrated to Spring Cloud, they faced a "Thundering Herd" problem. When the Gateway restarted, $1,000$ microservices tried to register at once. The Fix:

  1. Incremental Backoff: Services were configured to stagger their registration attempts.
  2. Eureka Read-Only Caches: The Eureka server was tuned to serve the registry from an immutable cache, reducing JVM GC pressure during high-load events.
  3. Result: The architecture successfully scaled to handle $100,000$ requests per second with 99.99% availability.

Summary: Designing the Cloud Fabric

  1. Stateless First: The Gateway and Eureka should be stateless. Scale them out horizontally to handle more traffic.
  2. Explicit Timeouts: Never use the default "Infinite" timeout. Every route must have a connect-timeout and read-timeout.
  3. Monitor theRegistry: Use a dashboard to watch for "Self-Preservation" events—they are early warning signs of network instability.

You have moved from "Building a single app" to "Engineering a Resilient Distributed Ecosystem." Your microservices are now unified into a single, indestructible fabric.


7. Global Routing: Canary and Blue-Green Strategies

In 2026, downtime is unacceptable. We use the Gateway to manage complex deployment strategies like Canary Releases.

  • Canary Routing: You can use the Weight route predicate to send 5% of your users to the new v2.1 of your service. If the error rate in your logs stays low, you can gradually increase the weight to 100% without a single user noticing a deployment.
  • Header-Based Routing: You can route requests to different service versions based on the user's "Beta-Tester" flag in their JWT. This allows for safe, live testing of high-risk features in the production environment.

8. Distributed Rate Limiting: The Token Bucket

To prevent "Noisy Neighbors" from crashing your cluster, the Gateway implements Distributed Rate Limiting using Redis.

  • The Logic: Every user (identified by IP or API Key) has a "Bucket" in Redis. Every request consumes a token. If the bucket is empty, the Gateway returns 429 Too Many Requests.
  • The Scaling: Because the counts are stored in Redis, all 50 instances of your Gateway share the same bucket for each user. This ensures that a user cannot "cheat" the rate limit by rotating through different gateway IP addresses.

9. Security: Hardening the Cloud Entrance

The Gateway is your first line of defense. We configure it to automatically inject modern security headers into every response:

  1. HSTS (Strict-Transport-Security): Prevents protocol downgrade attacks.
  2. CSP (Content-Security-Policy): Prevents Cross-Site Scripting (XSS).
  3. X-Frame-Options: Prevents clickjacking.
  4. X-Content-Type-Options: Prevents MIME-sniffing attacks. By centralizing these at the Gateway, you ensure that every microservice in your fabric is "Secure by Default," regardless of who wrote it.

10. Performance: Netty's Event-Loop Optimization

Spring Cloud Gateway's speed comes from its Non-Blocking Execution.

  • Netty Threads: By default, Netty only creates as many threads as you have CPU cores. Each thread handles thousands of connections simultaneously using an Event Loop.
  • The Danger: If you ever write blocking code (like Thread.sleep or a standard JDBC call) inside a Gateway Filter, you will freeze the Event Loop and crash the performance of the entire gateway. Always use the reactive WebClient or execute blocking tasks in a dedicated, bounded thread pool.

11. Gateway Observability: Micrometer and Prometheus

Because the Gateway is the single point of entry, it is the best place to gather Golden Signals (Latency, Traffic, Errors, and Saturation).

  • Micrometer Integration: We expose Gateway metrics via Spring Boot Actuator.
  • Key Metrics to Watch:
    • spring_cloud_gateway_requests_seconds: Measures the latency for every route.
    • netty_eventloop_executor_capacity: Monitors if your non-blocking threads are becoming saturated.
    • gateway_route_fallback_total: Tracks how often your Resilience4j fallback logic is being triggered. By visualizing these in Grafana, you can detect service degradation before it affects your customers.

Conclusion: Designing the Indestructible Cloud Fabric

You have moved from "Building a single app" to "Engineering a Resilient Distributed Ecosystem." Your microservices are no longer isolated islands; they are unified into a single, indestructible fabric governed by centralized routing, security, and resilience policies. You are now prepared to manage the largest, most complex systems in the 2026 enterprise landscape.


Part of the Java Enterprise Mastery — engineering the fabric.