当前位置:网站首页>Using ribbon to realize client load balancing

Using ribbon to realize client load balancing

2022-06-11 10:37:00 chentian114

Use Ribbon Achieve client load balancing

Preface

In the foreword 《 Use Eureka Provide service registration and discovery 》https://mp.weixin.qq.com/s/L2Tx6Lk0jl6GW-g0SeUXyw in , Based on Eureka Realize service registration and discovery . Generally speaking , In the production environment , Each micro service will deploy multiple instances , So how do service consumers allocate requests to multiple service provider instances ?

stay Spring Cloud in , When Ribbon and Eureka When used in combination ,Ribbon Can be automatically from Eureka Server Gets a list of service provider addresses , And based on the load balancing algorithm , Request one of the service provider instances .

Ribbon brief introduction

Ribbon yes Netflix Published load balancer , It helps control HTTP and TCP The behavior of the client . by Ribbon After configuring the service provider address list , Ribbon Can be based on some kind of load balancing algorithm , Automatically help service consumers to request . Ribbon Many load balancing algorithms are provided by default , For example, polling 、 Random etc. . Of course , We can also do Ribbon Implement custom load balancing algorithm .

6.Eureka And Ribbon Use the architecture diagram together

Integrate for service consumers Ribbon

  1. Create project , Copy project micro-consumer-movie , take artifactId It is amended as follows micro-consumer-movie-ribbon .

  2. Introduce... For the project Ribbon Dependence ,spring-cloud-starter-eureka Included by default Ribbon Dependence spring-cloud-starter-ribbon .

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
  1. by RestTemplate add to @LoadBalance annotation , Can be RestTemplate Integrate Ribbon , Make it load balanced .
@Configuration
public class RestConfig {
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
    
        return new RestTemplate();
    }
}
  1. start-up Eureka Server Registry Center and Multiple service providers micro-provider-user .
java -jar micro-discovery-eureka-0.0.1-SNAPSHOT.jar

java -jar micro-provider-user-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar micro-provider-user-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
  1. Modify the request address of the service consumer calling the service provider ,http://localhost:8000/ Change it to http://micro-provider-user/ .
@RestController
@RequestMapping("/movie/v1")
public class MovieController {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/{id}")
    public User findById(@PathVariable Long id){
    
        User entity = restTemplate.getForObject("http://micro-provider-user/user/v1/"+id, User.class);
        return entity;
    }

    @GetMapping("/log-instance")
    public void logUserInstance(){
    
        ServiceInstance serviceInstance = loadBalancerClient.choose("micro-provider-user");
        logger.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
    }
}

micro-provider-user Is the virtual host name of the user microservice (virtual host name), When Ribbon and Eureka When used in combination , The virtual host name will be automatically mapped to the network address of the microservice .

By default , The virtual host name is consistent with the service name . Of course , You can also use configuration properties eureka.instance.virtual-host-name or eureka.instance.secure-virtual-host-name Specify the virtual host name .

  1. Start the service consumer service micro-consumer-movie-ribbon , test Ribbon , Multiple access http://localhost:8010/movie/v1/1 , It can be found that the requests are evenly distributed to the two user microservice nodes , It shows that load balancing has been realized .

visit http://localhost:8010/movie/v1/log-instance , logUserInstance() Method used in LoadBalancerClient Of API Get the currently selected user microservice node more intuitively .

Use properties to customize Ribbon To configure

Ribbon Support the use of custom attributes , The supported properties are as follows :

  • NFLoadBalancerClassName : To configure ILoadBalancer Implementation class of
  • NFLoadBalancerRuleClassName : To configure IRule Implementation class of .
  • NFLoadBalancerPingClassName : To configure IPing Implementation class of .
  • NIWSServerListClassName : To configure ServerList Implementation class of .
  • NIWSServerListFilterClassName : To configure ServerListFilter Implementation class of .

practice : Modify by attributes Ribbon Client Load balancing rules of .

  1. Copy project micro-consumer-movie-ribbon , take ArtifactId It is amended as follows micro-consumer-movie-ribbon-custom-props .

  2. modify application.yml , Name it micro-provider-user Of Ribbon Client The load balancing rule of is set to random :

micro-provider-user:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
  1. Start the service micro-consumer-movie-ribbon-custom-props , test http://localhost:8010/movie/v1/log-instance , Check the log , You will find that the requests will be randomly distributed to two user microservice nodes .

Out of the Eureka Use Ribbon

In reality, some micro services may not be registered to Eureka Server On , To use Ribbon Load balancing , What to do ?

7. Out of the Eureka Use Ribbon Architecture diagram

  1. Copy project micro-consumer-movie-ribbon , take ArtifactId It is amended as follows micro-consumer-movie-ribbon-without-eureka .

  2. Delete eureka Dependence spring-cloud-starter-eureka , And add Ribbon rely on

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
  1. Remove the on the startup class @EnableDiscoveryClient annotation .

  2. Modify the configuration application.yml , by micro-provider-user Set up Ribbon The client sets the requested address list :

server:
  port: 8010

spring:
  application:
    name: micro-consumer-movie

micro-provider-user:
  ribbon:
    listOfServers: localhost:8000,localhost:8001
  1. Start multiple service providers micro-provider-user .
java -jar micro-provider-user-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar micro-provider-user-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
  1. Start the service micro-consumer-movie-ribbon-without-eureka , test http://localhost:8010/movie/v1/2 or http://localhost:8010/movie/v1/log-instance .

From the results , Although the movie microservices and user microservices are not registered at this time Eureka On , Ribbon Still working , The request will still be allocated to two user microservice nodes .

Hunger is loaded

Spring Cloud For each name Ribbon Client Maintain a sub application context , This context is lazy loaded by default . Of a given name Ribbon Client On the first request , The corresponding context will be loaded , therefore , First requests tend to be slow .

from Spring Cloud Dalston Start , We can configure starvation loading . for example

ribbon:
  eager-load:
    enabled: true
    clients: client1,client2

such , For the name client1 , clent2 Of Ribbon Client , The corresponding sub application context will be loaded at startup , from And improve the access speed of the first request .

Code warehouse

https://gitee.com/chentian114/spring-cloud-practice

official account

 originally chen

Reference resources

《Spring Cloud And Docker Microservice architecture practice 》 Zhou Li

原网站

版权声明
本文为[chentian114]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111031525311.html