Backend Development 28 min read

Understanding Spring Cloud: Eureka, Ribbon, OpenFeign, Hystrix, Zuul, Config and Bus

This article provides a comprehensive tutorial on Spring Cloud, covering its core components such as Eureka service discovery, Ribbon client‑side load balancing, OpenFeign declarative REST calls, Hystrix circuit breaking, Zuul API gateway, Config centralized configuration, and the Spring Cloud Bus for dynamic updates, complete with code examples and architectural diagrams.

Java Captain
Java Captain
Java Captain
Understanding Spring Cloud: Eureka, Ribbon, OpenFeign, Hystrix, Zuul, Config and Bus

What is Spring Cloud

Spring Cloud builds on Spring Boot to offer a simple programming model for common distributed system patterns, enabling developers to quickly create resilient, coordinated microservice applications.

Spring Cloud Versions

Version names follow London Underground stations (Angel, Brixton, Camden, Dalston, Edgware, Finchley, Greenwich, Hoxton) to indicate release order.

Spring Cloud Service Discovery – Eureka

Eureka is a REST‑based service registry that allows microservice providers to register metadata (IP, port, health URL) and consumers to discover them via a central server.

Key operations include Register, Renew (heartbeat every 30 seconds), Fetch Registries, Cancel (on shutdown), and Eviction (after 90 seconds without heartbeat).

Load Balancing – Ribbon

Ribbon is a client‑side load balancer (part of Netflix OSS) that runs on the consumer side, using algorithms such as RoundRobinRule, RandomRule, and RetryRule. It can be configured via providerName: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule .

Why Ribbon?

Ribbon distributes requests among multiple service instances to avoid overloading a single instance, improving high‑availability.

Comparison with Nginx

Nginx is a centralized load balancer, while Ribbon performs load balancing inside each consumer.

OpenFeign

OpenFeign integrates with Ribbon to provide declarative REST clients. By annotating an interface with @FeignClient("eureka-client-provider") , developers can invoke remote services as if they were local method calls.

@FeignClient(value = "eureka-client-provider")
public interface TestClient {
    @RequestMapping(value = "/provider/xxx", method = RequestMethod.POST)
    CommonResponse
> getPlans(@RequestBody PlanGetRequest request);
}

Hystrix – Circuit Breaker and Fallback

Hystrix protects distributed systems by isolating failures, providing circuit breaking and fallback mechanisms. Use @HystrixCommand with properties such as execution.isolation.thread.timeoutInMilliseconds and specify a fallbackMethod for graceful degradation.

@HystrixCommand(fallbackMethod = "getHystrixNews")
public News getNews(@PathVariable("id") int id) { /* call remote API */ }

public News getHystrixNews(@PathVariable("id") int id) { /* fallback logic */ }

Zuul – API Gateway

Zuul acts as the front door for all requests, providing routing, filtering, authentication, rate‑limiting, and monitoring. Basic configuration includes enabling the proxy with @EnableZuulProxy and setting the server port.

server:
  port: 9000
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9997/eureka

Routing can be customized with a prefix, route mappings, service name masking, path ignoring, and sensitive header handling.

Custom Filters

Implement filters by extending ZuulFilter . Example pre‑filter records request start time, and a post‑filter logs request duration.

@Component
public class PreRequestFilter extends ZuulFilter {
    public String filterType() { return FilterConstants.PRE_TYPE; }
    public int filterOrder() { return 0; }
    public boolean shouldFilter() { return true; }
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.set("startTime", System.currentTimeMillis());
        return null;
    }
}

@Component
public class AccessLogFilter extends ZuulFilter {
    public String filterType() { return FilterConstants.POST_TYPE; }
    public int filterOrder() { return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1; }
    public boolean shouldFilter() { return true; }
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        Long start = (Long) ctx.get("startTime");
        long duration = System.currentTimeMillis() - start;
        log.info("uri: " + request.getRequestURI() + ", duration: " + duration + "ms");
        return null;
    }
}

Rate Limiting Example

A pre‑filter using Guava's RateLimiter enforces a token‑bucket limit of 2 requests per second.

@Component
public class RouteFilter extends ZuulFilter {
    private static final RateLimiter RATE_LIMITER = RateLimiter.create(2);
    public String filterType() { return FilterConstants.PRE_TYPE; }
    public int filterOrder() { return -5; }
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        if (!RATE_LIMITER.tryAcquire()) {
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(429);
            return false;
        }
        return true;
    }
    public Object run() { return null; }
}

Spring Cloud Config

Config centralizes externalized configuration for distributed systems, typically backed by a Git repository. Clients fetch configuration at startup via the Config Server.

Dynamic Refresh

Using Spring Cloud Bus (e.g., with RabbitMQ or Kafka) and the @RefreshScope annotation, configuration changes can be propagated to running services without restart.

Spring Cloud Bus

Bus links services to a distributed message system, broadcasting state changes such as configuration updates across the cluster.

Summary

The article introduced the main Spring Cloud components: Eureka for service discovery, Ribbon for client‑side load balancing, OpenFeign for declarative REST calls, Hystrix for circuit breaking and fallback, Zuul as an API gateway with powerful filters, Config for centralized configuration management, and Bus for dynamic refresh across services.

microservicesEurekaSpring CloudOpenFeignHystrixconfigRibbonZuul
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.