Unlocking Spring Cloud: A Hands‑On Guide to Service Discovery, Load Balancing, and More
This article provides a comprehensive, beginner‑friendly overview of Spring Cloud’s core components—including Eureka, Ribbon, OpenFeign, Hystrix, Zuul, Config, and Bus—explaining their concepts, real‑world analogies, and essential configuration snippets with code examples.
What is Spring Cloud
Spring Cloud offers a simple, opinionated programming model for the most common distributed system patterns, helping developers build resilient, coordinated applications. It is built on Spring Boot, making it easy to adopt and quickly deploy to production.
Spring Cloud serves as a one‑stop solution for microservice architectures, providing service discovery, configuration management, message bus, load balancing, circuit breaking, and more.
Spring Cloud Versions
Version names are based on London Underground stations (Angel, Brixton, Camden, Dalston, Edgware, Finchley, Greenwich, Hoxton) following alphabetical order.
Eureka Service Discovery
Eureka is a REST‑based service used primarily in AWS for locating services, providing load balancing and fault‑tolerant server discovery. It includes a Java client (Eureka Client) with built‑in load balancing.
The service discovery pattern works like a real‑estate agency: providers (landlords) register their services (houses) with Eureka (the agency), and consumers (tenants) query the registry to find providers.
Service Register – Providers send metadata such as IP, port, health URL, and homepage to the Eureka server.
Renew – Clients send a heartbeat every 30 seconds; if the server does not receive a heartbeat for 90 seconds, the instance is removed.
Fetch Registries – Clients retrieve the registry list and cache it locally, refreshing every 30 seconds.
Cancel – Before shutdown, a client notifies the server to remove its entry.
Eviction – The server evicts instances that miss three consecutive renewals.
Ribbon Load Balancing
RestTemplate
RestTemplate is the HTTP client used by services. Example:
@Autowired
private RestTemplate restTemplate;
// Provider address; with Eureka use the service name
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);
}Why Ribbon?
Ribbon is a client‑side load balancer that selects a provider from the list obtained from Eureka using various algorithms.
RoundRobinRule – default round‑robin strategy.
RandomRule – random selection.
RetryRule – retries within a timeout.
Change the algorithm via configuration:
providerName:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRuleOpenFeign
OpenFeign runs on the consumer side, uses Ribbon for load balancing, and enables declarative REST calls.
After adding OpenFeign, a consumer can invoke a remote service as if it were a local interface.
Hystrix – Circuit Breaker and Fallback
Hystrix isolates remote calls, provides timeouts, circuit breaking, and fallback mechanisms to improve system resilience.
Key concepts:
Circuit Breaker – opens when the failure rate exceeds a threshold within a time window.
Fallback – alternative logic executed when a call fails, improving user experience.
Thread Isolation – each remote call runs in its own thread pool to prevent cascading failures.
Example annotation:
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1200")
})
public List<Xxx> getXxxx() {
// ...
}Zuul API Gateway
Zuul acts as the front door for all requests, providing dynamic routing, monitoring, resilience, and security.
Typical configuration (application.yml):
server:
port: 9000
eureka:
client:
service-url:
defaultZone: http://localhost:9997/eureka
zuul:
prefix: /zuul
routes:
consumer1: /FrancisQ1/**
consumer2: /FrancisQ2/**
ignore-services: "*"
ignore-patterns: **/auto/**Custom pre‑filter to record 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 to log URI and latency:
@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/100 + "ms");
return null;
}
}Spring Cloud Config
Config provides centralized externalized configuration for distributed systems, supporting Git or SVN backends.
Clients fetch configuration from a Config Server at startup. Dynamic refresh can be achieved with Spring Cloud Bus.
Spring Cloud Bus
Bus links services to a distributed message system, broadcasting events such as configuration changes.
Combined with Config, a POST to /bus/refresh on any instance propagates the new configuration to the whole cluster.
Summary
The article introduced the main Spring Cloud components—Eureka, Ribbon, OpenFeign, Hystrix, Zuul, Config, and Bus—explaining their purpose, core concepts, and essential configuration snippets to help readers build and manage microservice systems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
