Spring Cloud之负载均衡组件Ribbon

432 阅读4分钟

Spring Cloud整理之负载均衡组件Ribbon

概述

Ribbon是什么

github地址:https://github.com/Netflix/ribbon/wiki/Getting-Started

image-20200829153908970
image-20200829153908970

目前进入了维护阶段,后续不再进行更新

image-20200829140345274
image-20200829140345274

Ribbon能做什么

负载均衡

image-20200829140645949
image-20200829140645949

集中式负载均衡

image-20200829140733612
image-20200829140733612

进程内负载均衡

image-20200829140741541
image-20200829140741541

演示

架构说明

image-20200829141037512
image-20200829141037512
image-20200829141048173
image-20200829141048173

Eureka客户端的依赖中包含了Ribbon

image-20200829141128356
image-20200829141128356

RestTemplate

官网: https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#rest-template-headersl

什么是RestTemplate

RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。接下来我们就来看看这些操作方法的使用。

如何使用

配置

添加配置类

@Configuration  //声明该类是一个配置类
public class RestTemplateConfig {
    @Bean
    @LoadBalanced //Ribbon负载均衡注解
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
使用
image-20200829141417992
image-20200829141417992
getForObject方法/getForEntity方法
image-20200829141459878
image-20200829141459878
image-20200829141508989
image-20200829141508989

使用

在RestTemplate配置类返回的方法上添加@LoadBalanced注解

@Configuration  //声明该类是一个配置类
public class RestTemplateConfig {
    @Bean
    @LoadBalanced //Ribbon负载均衡注解
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

测试效果

项目使用的是上个文章中Eureka的Demo

通过访问服务消费者order80发现 服务提供者8001 和 8002 端口交替出现,说明系统默认使用的是轮询的负载均衡算法

第一次访问返回

image-20200829142104862
image-20200829142104862

第二次访问返回

image-20200829142044662
image-20200829142044662

Ribbon核心组件IRule

IRule实现类

负载均衡模式,可以自己配置,也可以自己编写负载均衡算法,后面将介绍如何替换负载均衡算法

image-20200829142808173
image-20200829142808173
  1. com.netflix.loadbalancer.RoundRobinRule -- 轮询(默认)
  2. com.netflix.loadbalancer.RandomRule -- 随机
  3. com.netflix.loadbalancer.RetryRule -- 先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内进行重试,获取可用的服务
  4. WeightedResponseTimeRule -- 对RoundRobinRule的扩展,响应速度越快的实例选择权重越多大,越容易被选择
  5. BestAvailableRule -- 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务
  6. AvailabilityFilteringRule -- 先过滤掉故障实例,再选择并发较小的实例\
  7. ZoneAvoidanceRule -- 默认规则,复合判断server所在区域的性能和server的可用性选择服务器

如何替换负载均衡算法

修改cloud-consumer-order80

注意事项

image-20200829143008669
image-20200829143008669
image-20200829143745466
image-20200829143745466

新建规则类

新建个包,不能被@componentScan扫描到

com.cloud.rul.MyselfRule.class

@configuration
public class MyselfRule {
    @Bean
    public IRule myRule(){
        return new RandomRule(); //返回随机负载策略
    }
}

修改主启动类

主启动类添加@RibbonClient

@SpringBootApplication
@EnableEurekaClient
//name代表哪个服务,configuration表示这个服务使用的负载均衡策略类
@RibbonClient(name = "CLOUD-PROVIDER-PAYMENT", configuration = MyRule.class)
public class OrderApplication80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication80.class,args);
    }

}

测试

访问http://localhost/consumer/payment/get/1

界面上返回的端口号不再是交替出现,而是随机出现

Ribbon负载均衡算法

原理

image-20200829151931512
image-20200829151931512