Cloud Native 29 min read

Master Spring Cloud: From Eureka Service Discovery to Zuul Gateway and Config Management

This comprehensive guide walks you through Spring Cloud's core components—including Eureka for service discovery, Ribbon for client‑side load balancing, OpenFeign for declarative REST calls, Hystrix for circuit breaking, Zuul as an API gateway, and Config plus Bus for centralized configuration—showing how they work together to build resilient microservices architectures.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Master Spring Cloud: From Eureka Service Discovery to Zuul Gateway and Config Management

Spring Cloud Overview

Spring Cloud extends Spring Boot to provide a programming model for common distributed‑system patterns such as service registration, configuration management, messaging, client‑side load balancing, circuit breaking, and monitoring. It enables rapid development of resilient microservice applications.

Version Naming

Spring Cloud releases are named after London Underground stations (e.g., Angel, Brixton, Camden, Dalston, Edgware, Finchley, Greenwich, Hoxton) and follow alphabetical order to indicate release chronology.

Eureka Service Discovery

Eureka is a REST‑based service registry. Service providers register their metadata (IP, port, health‑check URL) with the Eureka server; consumers query the registry to discover available instances.

Key operations:

Register : Provider sends metadata to the Eureka server.

Renew : Provider sends a heartbeat every 30 seconds. Missing three consecutive heartbeats (≈90 seconds) causes eviction.

Fetch Registries : Consumer retrieves the list of registered instances, cached locally and refreshed periodically.

Cancel : Provider removes its entry on shutdown.

Eviction : Automatic removal after missed heartbeats.

Ribbon Client‑Side Load Balancing

Ribbon runs on the consumer side. It obtains the list of service instances from Eureka and applies a load‑balancing algorithm before invoking a provider.

Default algorithm: RoundRobinRule. Other built‑in rules:

RoundRobinRule : Simple round‑robin selection.

RandomRule : Randomly picks an instance.

RetryRule : Retries with a timeout if the initial selection fails.

Custom algorithms can be implemented by providing a class that implements IRule and configuring it in application.yml or application.properties:

providerName:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

RestTemplate vs. OpenFeign

RestTemplate is a low‑level HTTP client that requires manual URL construction and request handling.

@Autowired
private RestTemplate restTemplate;
private static final String SERVICE_PROVIDER_A = "http://localhost:8081";

@PostMapping("/judge")
public boolean judge(@RequestBody Request request) {
    String url = SERVICE_PROVIDER_A + "/service1";
    return restTemplate.postForObject(url, request, Boolean.class);
}

OpenFeign provides a declarative HTTP client that integrates with Ribbon for load balancing, eliminating boilerplate code.

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

@RestController
public class TestController {
    @Autowired
    private TestClient testClient;

    @PostMapping("/test")
    public CommonResponse<List<Plan>> get(@RequestBody PlanGetRequest request) {
        return testClient.getPlans(request);
    }
}

Hystrix Circuit Breaker and Fallback

Hystrix isolates service calls, providing timeout, circuit‑breaker, and fallback mechanisms to prevent cascading failures.

@HystrixCommand(commandProperties = {
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1200")
})
public List<Xxx> getXxxx() {
    // business logic
}

@HystrixCommand(fallbackMethod = "getHystrixNews")
@GetMapping("/get/news/{id}")
public News getNews(@PathVariable("id") int id) {
    // remote call
}

public News getHystrixNews(@PathVariable("id") int id) {
    return new News("Service overloaded, please try later");
}

Hystrix also supports thread‑pool isolation (circuit‑breaker) and provides a dashboard for real‑time metrics.

Zuul API Gateway

Zuul acts as the edge service, offering dynamic routing, filtering, authentication, rate limiting, and monitoring.

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

Enable with @EnableZuulProxy on the main application class.

Routing configuration (application.yml):

zuul:
  prefix: /zuul
  routes:
    consumer1: /FrancisQ1/**
    consumer2: /FrancisQ2/**
  ignore-services: "*"
  ignore-patterns: **/auto/**

Example pre‑filter that records request start time:

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

Post‑filter that logs request duration:

@Component
public class AccessLogFilter extends ZuulFilter {
    @Override
    public String filterType() { return FilterConstants.POST_TYPE; }
    @Override
    public int filterOrder() { return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1; }
    @Override
    public boolean shouldFilter() { return true; }
    @Override
    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 using Guava RateLimiter (2 requests per second):

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

Spring Cloud Config

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

Dynamic refresh can be achieved with Spring Cloud Bus (e.g., RabbitMQ or Kafka) that broadcasts /actuator/bus-refresh events to all instances annotated with @RefreshScope.

Spring Cloud Bus

Bus links services to a distributed message system, propagating state changes such as configuration updates across the cluster. A POST to /actuator/bus-refresh triggers all @RefreshScope beans to reload their properties.

Summary of Core Components

Eureka – service discovery registry.

Ribbon – client‑side load balancer (default RoundRobinRule).

OpenFeign – declarative HTTP client that uses Ribbon.

Hystrix – circuit breaker and fallback for resilience.

Zuul – edge gateway providing routing and filter capabilities.

Config – centralized configuration server (Git‑backed).

Bus – message bus for broadcasting configuration changes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MicroserviceseurekaSpring CloudHystrixCONFIGRibbonZuul
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

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.