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.

Top Architect
Top Architect
Top Architect
Understanding Load Balancing in Spring Cloud: Registering Multiple Services and Code Analysis

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: true

A 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.RandomRule

After 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.

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.

MicroservicesBackend Developmentload balancingfeignSpring CloudRibbonDiscoveryClient
Top Architect
Written by

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.

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.