“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

The difference between

– Ribbon

Ribbon is a load balancer released by Netflix that helps control the behavior of HTTP and TCP clients. The Ribbon is client-side load balancing. As you all know, when we first started using the Springcloud microservices architecture, we used Netflix’s credit: https://docs.spring.io/spring-cloud-netflix/docs/2.2.9.RELEASE/reference/html/. Unfortunately, Eureka has long since been officially abandoned and will no longer be updated. This may be for better unified architecture.

– Spring-cloud-loadbalancer

Spring-cloud-loadbalancer is a new load balancing tool officially launched. Back in 2017, Spring started to try to develop spring-Cloud-load Balancer to replace ribbon, hosted in the Spring-Cloud-Incubator, and then, after a while, The sudden marking of this project as archived migration to Spring-Cloud-Commons shows that the official commitment to unifying the common infrastructure is progressing.

Back in Spring Cloud Hoxton.M2, the first to integrate Spring-cloud-loadbalancer to replace the old ribbon:

Spring Cloud Hoxton.M2 is the first release containing both blocking and non-blocking load balancer client implementations as an alternative to Netflix Ribbon which has entered maintenance mode. To use the new `BlockingLoadBalancerClient` with a `RestTemplate` you will need to include ` org. Springframework. Cloud: spring -- cloud - loadbalancer ` on your application's classpath. The same dependency can be 2 in a reactive application when using `@LoadBalanced WebClient.Builder` - the only difference is that Spring Cloud will auto-configure a `ReactorLoadBalancerExchangeFilterFunction` instance. See the [documentation] (HTTP: / / https://cloud.spring.io/spring-cloud-static/spring-cloud-commons/2.2.0.M2/reference/html/#_spring_restte mplate_as_a_load_balancer_client) for additional information. The new `ReactorLoadBalancerExchangeFilterFunction` can also be autowired and passed directly to `WebClient.Builder` (see the [documentation](https://cloud.spring.io/spring-cloud-commons/reference/html/#webflux-with-reactive-loadbalancer)). For all these features, [Project Reactor](https://projectreactor.io/)-based `RoundRobinLoadBalancer` is used underneath.Copy the code

From this you can see, the original is only support BlockingLoadBalancerClient, is also based on the RestTemplate. We know that the ribbon is also based on RestTemplate:

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setReadTimeout(env.getProperty("client.http.request.readTimeout", Integer.class, 15000));
        requestFactory.setConnectTimeout(env.getProperty("client.http.request.connectTimeout", Integer.class, 3000));
        RestTemplate rt = new RestTemplate(requestFactory);
        return rt;
}
Copy the code

However, the ribbon is clearly sophisticated when it comes to configuration:

backend:
  ribbon:
    client:
      enabled: true
    ServerListRefreshInterval: 5000

ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 1000
  eager-load:
    enabled: true
    clients: cas-server,customer-server
  MaxAutoRetries: 2
  MaxAutoRetriesNextServer: 3
  OkToRetryOnAllOperations: true
  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
Copy the code

You can configure multiple dimensions: timeout, refreshing the service list, retry mechanism, and so on.

Not so good for spring-cloud-loadbalancer, after all, a new cub. However, for the first time, Spring Cloud Hoxton introduced a loadbalancer that supports both blocking and non-blocking as Netflix Ribbon in maintenance. Now, let’s see how it works.

In actual combatspring-cloud-loadbalancer

When use, we learned from the original, only need to introduce org. Springframework. Cloud: spring – cloud – loadbalancer dependence, New BlockingLoadBalancerClient and RestTemplate can be used together. This dependency will also be introduced to support Reactive applications, which, like any other use, will only need to modify webClient.Builder with @loadBalanced.

Let’s start by importing dependencies, using nacOS-based service registration and discovery. Let’s start by injecting dependencies:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
Copy the code

In this case, we are using a new loadbalancer. We need to exclude ribbon dependencies or loadbalancer will not work. Also, we need to disable the ribbon’s load balancing capabilities:

spring:
  cloud:
    loadbalancer:
      ribbon:
        enabled: false
Copy the code

When disabled, we use it in combination with RestTemplate and decorate webClient.Builder with @loadBalanced.

@ LoadBalanced / / will not be able to use IP and other forms to request other service @ Bean public RestTemplate RestTemplate () {SimpleClientHttpRequestFactory requestFactory  = new SimpleClientHttpRequestFactory(); requestFactory.setReadTimeout(env.getProperty("client.http.request.readTimeout", Integer.class, 15000)); requestFactory.setConnectTimeout(env.getProperty("client.http.request.connectTimeout", Integer.class, 3000)); RestTemplate rt = new RestTemplate(requestFactory); return rt; }Copy the code

For those of you who are careful, you can begin to see that this Ribbon has the same configuration as the Ribbon. At this point, we start the service provider, consumer and test. I’m not going to show it here.

conclusion

Officially, the new load balancer will replace the ribbon, thanks to the introduction of Reactive support. This is an improvement in performance.

There are some limitations to spring-cloud-loadbalancer:

  • The Ribbon provides several default load balancing policies
  • At presentspring-cloud-loadbalancerOnly the retry operation is supported
  • The ribbon supports timeout, lazy load handling, retry, and advanced hystrix integration

In spring-cloud systems, the Ribbon is still used in most areas, but with spring-cloud-K8S, you may need to use spring-cloud-starter-Kubernetes-loadbalancer. As we learned earlier, the Ribbon based LB does not allow services to access each other across namespaces.