When a consumer initiates a request, it’s intercepted by Ribbon. Ribbon pulls the provider list from Eureka, Eureka returns the list, and Ribbon selects a server based on IRule to send the request.
Detailed interception flow: Request -> DynamicServerListLoadBalancer (extracts service ID from URL, e.g., userService) -> DynamicServerListLoadBalancer -> Eureka -> DynamicServerListLoadBalancer -> IRule -> DynamicServerListLoadBalancer -> Execute Request.
Load Balancing Strategies
| Built-in Load Balancing Rule Class | Description |
|---|---|
| ZoneAvoidanceRule (Eureka Default) | Selects servers based on zone availability. Uses Zones to categorize servers (e.g., a data center or a rack) and then performs round-robin within the Zone. |
| RoundRobinRule | Simple round-robin through the service list. This is the default Ribbon rule. |
| AvailabilityFilteringRule | Ignores the following two types of servers: (1) By default, if a connection fails 3 times, the server is set to a “short-circuit” state for 30 seconds. If it fails again, the duration increases exponentially. (2) Servers with high concurrency. Clients configured with this rule will ignore servers where the active connection count is too high. The upper limit can be configured via <clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit. |
| WeightedResponseTimeRule | Assigns a weight to each server. The longer the response time, the lower the weight. Servers are chosen randomly, but the weight influences the selection probability. |
| BestAvailableRule | Ignores “short-circuited” servers and picks the one with the lowest concurrent connection count. |
| RandomRule | Randomly picks an available server. |
| RetryRule | Selection logic with a retry mechanism. |
Using the Random Strategy
You can modify the load balancing rule by defining an IRule implementation. There are two ways to do this:
- Code Method: Define a new
IRulein a configuration class (Global setting).
| |
- Configuration File Method: Add configuration to the
application.ymlof the consumer (e.g., orderServer) to modify the rule for a specific service.
| |
Eager Loading
Ribbon uses lazy loading by default, meaning it creates the LoadBalanceClient only during the first access. This can result in a long initial request time. Eager loading creates it when the project starts, reducing the latency of the first request. Enable eager loading in the config:
| |