Understanding Load Balancing in Spring Cloud: Registering Multiple Services and Code Analysis
This article explains the concept of load balancing, illustrates it with a bank queue analogy, and demonstrates how to register multiple Spring Cloud service instances, use Feign and DiscoveryClient, configure Ribbon's load‑balancing interceptor, and customize the balancing rule for effective microservice traffic distribution.
Load balancing (Load balancing) is a computer technology that distributes workload across multiple computers, network connections, CPUs, or other resources to optimize resource use, maximize throughput, minimize response time, and avoid overload.
Using a bank queue analogy, multiple service windows correspond to multiple service instances, and a ticket machine corresponds to a load balancer that assigns incoming requests to a specific instance.
In Spring Cloud, a single service may become a bottleneck under high concurrency, so registering multiple instances of the same service (e.g., setting server.port: 8091 while keeping the same spring.application.name) allows the load balancer to distribute traffic.
Example configuration for a second item service instance:
server:
port: 8091 # service port
spring:
application:
name: hutao-microservice-item
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:9090/eureka/
instance:
prefer-ip-address: trueA Feign client can be declared with @FeignClient(value = "hutao-microservice-item") to invoke the item service:
@FeignClient(value = "hutao-microservice-item")
@RequestMapping("/itemservice")
public interface FeignOrderService {
@GetMapping("item/{itemId}")
Items queryItem(@PathVariable("itemId") String itemId);
}The DiscoveryClient interface provides methods such as List<ServiceInstance> getInstances(String serviceId) to retrieve all instances of a given service ID.
Spring Cloud’s load‑balancing mechanism works through LoadBalancerInterceptor and RibbonLoadBalancerClient. The interceptor intercepts RestTemplate calls, and the Ribbon client selects a server via loadBalancer.chooseServer, defaulting to a round‑robin rule.
To customize the algorithm, the IRule implementation can be changed in the configuration, for example:
hutao-microservice-item:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRuleAfter restarting, the service instances are chosen randomly instead of sequentially.
Key takeaways: register multiple identical service IDs on different ports or hosts; enable @LoadBalanced on RestTemplate to let Ribbon handle instance selection; use DiscoveryClient to query instances; and customize the load‑balancing rule via NFLoadBalancerRuleClassName.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
