This is the 18th day of my participation in Gwen Challenge

On a dark night with high wind, xiaobian sat upright and nervous. With trembling hands, he slowly opened a layer of mystery ———— What is the Ribbon?

What is Ribbon?

The Ribbon is an open source project released by Netflix. Its main function is to provide a client-side software load balancing algorithm that connects Netflix’s middle-tier services together.

What does the Ribbon do?

The Ribbon client component provides a comprehensive set of configuration items such as connection timeout and retry, which are responsible for request distribution.

How to use the Ribbon?

The Ribbon automatically helps you connect all the machines behind Load Balancer (LB) in a configuration file based on certain rules (e.g. simple polling, random linking, etc.).

How does the Ribbon call?

@ LoadBalanced principle

Ribbon through a @ LoadBalanced annotations to realize the load balance between the RestTemplate request ClientHttpRequestInterceptor for RestTemplate in sends a request to intercept RestTemplate has an attribute is a List interceptors, LoadBalancerInterceptor is ClientHttpRequestInterceptor implementation class

role

The LoadBalancerInterceptor assigns the core logic of load balancing to loadBalancer. The following code uses @loadBalanced to load balancing.

Pay attention to

The @loadBalanced annotation belongs to Spring. This annotation is implemented by the Spring Cloud, not the Ribbon. When Spring initializes the container, if it detects that the Bean is annotated by @loadBalanced, Spring sets the interceptor of LoadBalancerInterceptor for it.

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(a) {
        return new RestTemplate();
    }



    @RestController
    class EurekaConsumerController {

        @Value("${server.port}")
        private String port;
        @Autowired
        private RestTemplate restTemplate;
        @Autowired
        private DiscoveryClient discoveryClient;


        private String serviceName = "eureka-provider";

        @GetMapping("/getProviderInfo")
        public String getProvider(a){
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);
            ServiceInstance serviceInstance = serviceInstances.stream()
                    .findAny().orElseThrow(() ->
                            new IllegalStateException("no " + serviceName + " instance available"));
            return restTemplate.getForObject(
                    "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() +
                            "/instanceInfo", String.class); }}Copy the code

Ribbon load Balancing Strategy?

  • RoundRobinRule (default)
  • RandomRule random
  • RetryRule Polling retries (retries are also polling by default)
  • WeightedResponseTimeRule The response speed determines the weight
  • BestAvailableRule optimally available (also RoundRobinRule)
  • AvailabilityFilteringRule availability filtering rules (the underlying have RoundRobinRule)
  • The Available avoidancerule zone has optimal performance