Spring cloud uses Ribbon to achieve client load balancing

Keywords: Programming Spring

@[toc]

Spring cloud uses Ribbon to achieve client load balancing

Preface

In Spring cloud, when multiple services of the same type start to register in the service registry, the frequency service is the cluster When the consumer (client) consumes, it needs to select and call the service.

Service registration click

The default call in Spring cloud relies on eureka's ribbon to implement the client load balancing strategy

Let's register two services first

Then, it uses ribbon to realize the load balancing strategy

ribbon dependence

ribbon relies on eureka for load balancing, so we do not need to import packages. By default, it exists in eureka dependency

Load balancing

Just add @ LoadBalanced annotation on it


@EnableDiscoveryClient
@SpringBootApplication
public class EurekaclientApplication {

    //Turn on load balancing
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(EurekaclientApplication.class, args);
    }

}

Consumption call

@RestController
public class GetUserController {

    @Autowired
    RestTemplate restTemplate;

    //Service discovery object
    @Autowired
    DiscoveryClient discoveryClient;


    @RequestMapping("get")
    public User getUser(int id){
        //The name of the user microservice is used to send requests to this service
        String  servceId = "user-server";

        String url = "http://" + servceId  + "/getUser?id=" + id;
        System.out.println(url +  " =  " +  url);
        User forObject = restTemplate.getForObject(url, User.class);


}

test

Print the url and port of each call


    @Autowired
    LoadBalancerClient loadBalancerClient;


        for (int i = 0; i < 100; i++) {

            ServiceInstance choose = loadBalancerClient.choose("user-server");
            System.out.println("The first " + (i+1) + "Secondary execution " +  choose.getPort() + choose.getUri());
        }
        return forObject;
    }

give the result as follows

We can see that each time it is a round robin call, and then a round robin call request (2 services in total).

Default policy

There is no configuration policy specified in ribbon auto configuration to implement rotation training policy by default

Set random policy

Set load balancing as random strategy

serviceId.ribbon.NFLoadBalancerRuleClassName = custom load balancing policy class

Configure in configuration file


user-server: # This configuration is the requested service name
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Or by hard coding


@Configuration
public class RibbonConfiguration {
 
    @Bean
    public IRule ribbonRule() {
        // Load balancing rule, change to random
        return new RandomRule();
    }

By default, the configuration file is larger than hard coding

test result

Random access per request

Custom policy

By default, ribbon implements load balancing strategy through IRule interface, and by default, it implements load rotation training through zoneaavoidancerule

  • Structural drawing

In addition, the self-contained load balancing strategy

Realizing Ribbon load balancing without relying on eureka

ribbon can realize load balancing without eureka as follows

#Cancel the use of Eureka by Ribbon
ribbon.eureka.enabled=false

#Configure the microservice nodes that the Ribbon can access. Multiple nodes are separated by commas
 user-server.ribbon.listOfServers=localhost:6869,localhost:6870

Problems that may come up in the end

May report an error

When the spring cloud ribbon implements load balancing, it prompts the Request URI does not contain a valid hostname: http://PRODUCT_SERVICE/

The reason is that the service name cannot be underlined

Code address Portal

Posted by CreativeWebDsign on Wed, 18 Mar 2020 05:08:16 -0700