1. The background

Today we’ll learn about SpringCloud’s client-side load balancing Ribbon

We continue to use the previous Eureka-Server as the service registry today

The following versions use Springboot and SpringCloud

  • Springboot version: 2.3.5-release
  • Springcloud version: Hoxton SR9

2. Why does the first invocation fail?

The Ribbon does not initiate the Client load balancing process when the service is started. The Ribbon creates a Client when the service is called. Therefore, the duration of the first call includes not only the time it takes to send the HTTP request. The RibbonClient creation time is also included. If the RibbonClient creation time is slow and the timeout time is short, the RibbonClient creation time can easily appear as described above.

We also know this from the log, which shows the following information when the first call is made:

The 2020-12-10 08:29:54, 201 INFO [main] com.net flix. Loadbalancer. DynamicServerListLoadBalancer - DynamicServerListLoadBalancer for client hello-service initialized: DynamicServerListLoadBalancer: {NFLoadBalancer: name = hello - service, the current list of Servers = [192.168.99.176:9901], the Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:1; Active connections count: Tripped count: 0; Circuit breaker tripped count: 0; Active Connections per server: 0.0; tripped tripped count: 0;  },Server stats: [[Server: 192.168.99.176:9901; Zone: UNKNOWN; Total Requests: 0; Successive connection failure: 0; Total blackout seconds: 0;  Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; Total Failure Count in last (1000) mSECs :0; Average Resp time:0.0; Total Failure count in last (1000) mSECs :0; 90 the percentile resp time: 0.0; 95 the percentile resp time: 0.0; min resp time: 0.0; Max resp time: 0.0. Stddev resp time: 0.0]]} ServerList: ConsulServerList {serviceId = 'hello - service, tag = null}Copy the code

Feign’s implementation is based on the Ribbon, so it has the same problem. Here’s how to fix it.

3. Solutions

The solution is simple, since it takes time to generate RibbonClient on the first call, let it be created in advance instead of on the first call.

Several new parameters are available in the Dlaston release of Spring Cloud that can be easily used to do this.

ribbon:
  eager-load:
    enabled: true
    clients: ms-ribbon-producer
Copy the code

Parameter Description:

  • Ribbon. Eagle-load. Enabled: Enables the hungry loading mode of the ribbon
  • Ribbon. Eagle-load. Clients: specifies the client name and service name that needs to be loaded hungry

After the above configuration is complete, we try to restart the service consumer. At this time, we find that we have not started to invoke the service interface, but the log of initialization load balancing has been printed. This means that the ribbon’s hungry loading module Settings are working.