Don't you use Ribbon yet? 5 minutes to take you to quickly understand

Spring Cloud Ribbon

1 What is load balancing

When the client accesses the back-end service, we can deploy multiple service levels to improve the throughput of the entire application, but which service should be accessed for a single request, this is what the load balancing needs to do. The requests are evenly distributed in the back-end services. In real scenarios, due to different machine performance and network conditions, the distribution of requests can be controlled by configuring weights, which is what load balancing does.

2 Introduction to Ribbon

Spring Cloud Ribbonis a client-side load balancing tool based on HTTPand , which is based on implementation. Through the encapsulation, we can easily automatically convert service-oriented requests into client-side load balancing service calls. Although it is only a tool class framework, it does not require independent deployment like service registry, configuration center, and API gateway, but it exists in almost every built microservice and infrastructure. Because the invocation between microservices, the forwarding of requests, etc. are actually implemented through, including what we will introduce later , it is also an implementation-based tool. Therefore, the correct understanding and use are very important for us to use to build microservices.TCPNetflix RibbonSpring CloudREST模版Spring Cloud RibbonSpring CloudAPI网关RibbonFeignRibbonSpring Cloud RibbonSpring Cloud

3 Getting Started Cases

We need to register at least 2 or more services in the registry to provide load balancing instances when making service calls

3.1 New startup entry

In order to see the call situation, we modify the /hellointerface as follows and add a line to the console to print

@GetMapping("/hello")
public String hello(){
    System.out.println("获得请求....");
    return "hello!";
}
复制代码

image-20220313180908732

image-20220313180928533

image-20220313181123769

3.2 View the instance after running

image-20220313181207841

image-20220313181241575

There are two instances that the registration is successful

3.3 Consumers enable load balancing

3.3.1 Modify the startup class

@SpringBootApplication
public class ServiceConsumerApplication {

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

    @Bean
    @LoadBalanced//开启负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
复制代码

Code Description:

1:在RestTemplate注册的bean上面添加 @LoadBalanced注解用以开启负载均衡
复制代码

3.3.2 Added interface for load balancing

ConsumerControllerAdd the following interface in

@GetMapping("/hello-loadbanlance")
public String hello2(){
    // 1.通过注册中心的服务名构建url
    String url = "http://service-provider/hello";
    // 2.发送请求
    return restTemplate.getForObject(url,String.class);
}
复制代码

Code Description:

1:url中的service-provider是注册在注册中心的服务提供者的名称,通过这种方式构建的url在restTemplate调用时restTemplate开启的负载均衡才可以生效。
复制代码

3.3.3 Start a consumer instance and initiate a call

3 consecutive requests

http://localhost:8002/consumer/hello-loadbanlance
复制代码

Observe the logs of the two service provider instances, and you can see that the request is load balanced to the two instances

Example 1:

image-20220313182428265

Example 2:

image-20220313182448342

4 Load Balancing Strategy

4.1 负载均衡策略介绍

Ribbon内置了多种负载均衡策略,内部负责负载均衡的顶级接口为:com.netflix.loadbalancer.IRule ,实现方式如下 :

image-20220313183557783

  • com.netflix.loadbalancer.RandomRule :随机策略
  • com.netflix.loadbalancer.RetryRule :重试策略
  • com.netflix.loadbalancer.RoundRobinRule :以轮询的方式进行负载均衡
  • com.netflix.loadbalancer.WeightedResponseTimeRule :权重策略,会计算每个服务的权 重,越高的被调用的可能性越大
  • com.netflix.loadbalancer.BestAvailableRule :最佳策略,遍历所有的服务实例,过滤掉 故障实例,并返回请求数最小的实例返回
  • com.netflix.loadbalancer.AvailabilityFilteringRule :可用过滤策略,过滤掉故障和请 求数超过阈值的服务实例,再从剩下的实例中轮询调用(默认)

在服务消费者的application.yml配置文件中修改负载均衡策略,格式:

{服务提供者名称}.ribbon.NFLoadBalancerRuleClassName

4.2 负载均衡策略配置

在消费者服务端配置文件中添加如下配置

service-provider: # 服务提供者在注册中心的服务名
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #负载均衡策略,此处为随机
复制代码

重启服务,并连续多次调用,可以看到此时两个提供者被调用的次数并不平衡,说明策略配置生效:

实例1:

image-20220313184254421

实例2:

image-20220313184318669

Guess you like

Origin juejin.im/post/7079432648036188196