Can Ribbon really be replaced by spring-cloud-loadbalancer?

background

  • Morning brush circle see Spring Cloud Hoxton.M2 Released The news was posted to my knowledge planet and a friend would come to ask the following questions.Take half a day to learn the source code of spring-cloud-loadbalancer and summarize this article

  • Spring Cloud Hoxton.M2 is the first version to incorporate a new loadbalancer implementation to replace 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.
  • Origin of spring-cloud-loadbalancer
  1. In 2017 Spring began trying to develop a new project, spring-cloud-loadbalancer instead of ribbon, hosted on

spring-cloud-incubator incubator
(To mention more, most of the top projects like spring cloud alibaba have been hatched from here, representing the direction of spring cloud development.)

  1. After N months of no maintenance and thinking spring had abandoned the project, I suddenly moved the project as an archive spring-cloud-commons
  2. Release version 2.2.0.M2

How to use

  • This is based on the latest version of hoxton.m2, so configure spring's proxy maven Library
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.M2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
</dependencyManagement>
  • Add nacos-client, use version 2.1.0, pay special attention to excluding ribbon dependencies, otherwise loadbalancer is invalid
<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>
  • Add loadbalancer pom coordinates
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
  • Configure whether to use or configure as ribbon
@Configuration
public class LbConfiguration {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@GetMapping("/demo")
public String doOtherStuff() {
    return restTemplate.getForObject("http://big-provider-server/demo", String.class);
}

Source Parsing

LoadBalancerClient implementation

  • The current version only provides an implementation of BlockingLoadBalancerClient, note the Chinese comment
// Delete retains only core code attention
public class BlockingLoadBalancerClient implements LoadBalancerClient {

    @Override
    public <T> T execute(String serviceId, LoadBalancerRequest<T> request)
            throws IOException {
        // Query available instances based on service name
        ServiceInstance serviceInstance = choose(serviceId);
        return execute(serviceId, serviceInstance, request);
    }

    @Override
    public ServiceInstance choose(String serviceId) {
        // Get Load Balancing Policy
        ReactiveLoadBalancer<ServiceInstance> loadBalancer = loadBalancerClientFactory
                .getInstance(serviceId);
        // Execute load balancing policy to get available instances
        Response<ServiceInstance> loadBalancerResponse = Mono.from(loadBalancer.choose())
                .block();
        return loadBalancerResponse.getServer();
    }

}

Load Balancer Load Balance Policy Implementation

  • There is currently only one RoundRobinLoadBalancer polling for how to select server s
public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {
    public Mono<Response<ServiceInstance>> choose(Request request) {
        ServiceInstanceSupplier supplier = this.serviceInstanceSupplier.getIfAvailable();
        return supplier.get().collectList().map(instances -> {
            if (instances.isEmpty()) {
                log.warn("No servers available for service: " + this.serviceId);
                return new EmptyResponse();
            }
            // TODO: enforce order?
            int pos = Math.abs(this.position.incrementAndGet());

            ServiceInstance instance = instances.get(pos % instances.size());

            return new DefaultResponse(instance);
        });
    }

}

Compare with ribbon

Default load balancing comparison

  • ribbon provides the default load balancing strategy in 7. Common overlays are common. Usually we use ZoneAvoidanceRule composite to determine the performance of the server region and the availability of the server selection server

Configuration Richness

  • Currently, spring-cloud-loadbalancer only supports configuration of retry operations
  • ribbon supports timeout, lazy load handling, retry, and integration of advanced properties with hystrix

conclusion

  • Honest and practical ribbon

Keywords: Java Spring Maven Load Balance github

Added by starnol on Wed, 21 Aug 2019 05:35:18 +0300