SpringCloud-狂神(4. 负载均衡-Ribbon)学习笔记

上一篇 : 3. Eureka-服务注册与发现

下一篇 : 5. 负载均衡-Feign

1. 概述

1.1 Ribbon 是什么

  • SpringCloud Ribbon 是基于 Netflix Ribbon 实现的一套客户端负载均衡的工具
  • 主要功能是提供客户端的软件负载均衡算法,将 Netflix 的中间服务处连接在一起。
  • Ribbon 的客户端组件提供一系列完整的配置项,如:连接超时、重试等。简单的说就是在配置文件中列出 LoadBalance (LB:负载均衡)后面所有的机器,Ribbon 会自动帮你基于某种规则去连接这些机器

在这里插入图片描述

1.2 负载均衡(LB)

  • 核心作用:就是将用户的请求平摊在多个服务器上,从而达到系统的 High Availability(HA :高可用)

  • 常见的负载均衡软件有 Nginx、Lvs 等

  • Bubbo 和 SpringCloud 都提供了负载均衡,SpringCloud 的负载均衡算法可以自定义

  • 负载均衡算法实现的方式:轮询、权重、随机

  • 负载均衡的简单分类:
    1、集中式

    在服务的 消费者 和 提供者之间使用独立的 LB 设施,如Nginx(反向代理服务器) ,由该设施负载把访问请求通过某种策略发送至服务的提供者

    2、进程式

    将LB逻辑集成到 消费者,消费者 从服务注册中心得到哪些地址可用,然后自己再从这些地址中选出一个合适服务器

Ribbon 就属于进程内 LB ,它只是一个类库,集成与消费者进程,消费者通过他来获取到服务提供者的地址

2. 使用

2.1 整合 Ribbon

  • 由上述可知,Ribbon 是需要集成在消费端的
  • 所以在消费端 : springcloud-03-consumer-dept-8082 进行修改
  1. 在 POM 文件中添加 Ribbon、Eureka 依赖

    	<!--ribbon-->
    	<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
    	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-ribbon</artifactId>
    		<version>1.4.7.RELEASE</version>
    	</dependency>
    	<!--Eureka 服务提供者-->
    	<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
    	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-eureka</artifactId>
    		<version>1.4.7.RELEASE</version>
    	</dependency>
    
  2. 编写 application.yml

    # Eureka
    eureka:
      client:
        # 不向注册中心注册自己
        register-with-eureka: false
        # 配置 可连接的注册中心
        service-url:
          defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.2:7002/eureka/
    
  3. 在主启动类上添加 Eureka 启动项 : @EnableEurekaClient

  4. 在之前把 RestTemplate 注册到 Bean 的配置方法上添加一个注解

    配置 负载均衡实现 RestTemplate : @LoadBalanced
    在这里插入图片描述

  5. 修改控制层

     /**提供者 URL 的前缀
     *
     * 不使用 Ribbon 时 ,这里就是第一中写法
     *
     * 使用 Ribbon 实现负载均衡 时,这里就不能写死为一个地址,
     *       而需要通过注册中心的服务名来访问
     *       服务名:在 提供者 YML 文件中配置的 spring:application:name: 的值
     *            或者 监控页面的 Application 字段值
     */
    //private static final String REST_URL_PREFIX = "http://localhost:8081";
    
    private static final String REST_URL_PREFIX = "http://SpringCloud-02-provider";
    
    
  6. 启动集群、提供者、消费者,进行测试

    进入 消费者页面 发出请求,得到正确结果

    成功

2.2 添加提供者,观察负载均衡

  1. 添加一个数据库 : spring_cloud_02

    CREATE DATABASE `spring_cloud_02`
    
    USE `spring_cloud_02`
    
    DROP TABLE IF EXISTS `dept`;
    CREATE TABLE `dept` (
      `deptno` bigint(20) NOT NULL AUTO_INCREMENT,
      `dname` varchar(60) DEFAULT NULL,
      `db_source` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '数据库的标识',
      PRIMARY KEY (`deptno`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='部门表';
    
    insert into dept(dname, `db_source`) values ('开发部', database());
    insert into dept(dname, `db_source`) values ('人事部', database());
    insert into dept(dname, `db_source`) values ('财务部', database());
    insert into dept(dname, `db_source`) values ('市场部', database());
    insert into dept(dname, `db_source`) values ('运维部', database());
    
  2. 新建一个 服务提供者 : springcloud-02-provider-dept-8083

  3. 把另一个 提供者 的文件都复制过来,再做修改

  4. 修改 application.yml (端口号、数据库、描述信息)

    【注意】多个提供者的服务名必须一致

    server:
      port: 8083
    
    mybatis:
      type-aliases-package: com.demo.pojo
      mapper-locations: classpath:mybatis/mapper/*.xml
      config-location: classpath:mybatis/mybatis-config.xml
    
    spring:
      application:
        name: SpringCloud-02-provider
      datasource:
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/spring_cloud_02?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        type: com.alibaba.druid.pool.DruidDataSource
    
    logging:
      level:
        com.demo.mapper: DEBUG
    
    # Eureka,配置服务注册到哪里
    eureka:
      client:
        service-url:
          # 配置监控页面的地址,这是在 Eureka Server 中配置的
          defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.2:7002/eureka/
      instance:
        # 修改 Eureka 监控页面上的默认描述信息
        instance-id: springcloud-provider-dept-8083
    
  5. 修改主启动类,添加注解

    //在配置过这个注解后,服务启动会自动注册到 Eureka Server
    @EnableEurekaClient
    //开启服务发现
    @EnableDiscoveryClient

  6. 启动集群测试

    在监控页面已经可以看到两个提供者了

    在这里插入图片描述

  7. 查看负载均衡效果

    消费者一直发送请求

    在这里插入图片描述
    在这里插入图片描述
    可以观察到会在两个提供者之间轮询

2.3 修改负载均衡策略

  • 负载均衡有一个核心的结构 : IRule
  • 进入该接口在这里插入图片描述
  • 实现类
    在这里插入图片描述

修改为已经实现的策略

  • 因为是在消费者端做负载均衡,所以在消费者中修改

  • 把已经实现的策略注册的 Bean 中即可

  • 修改 springcloud-03-consumer-dept-8082 的 BeanConfig 文件

    @Bean
    public IRule myRule(){
        // 先使用已经实现的策略——随机
        return new RandomRule();
    }
    

    重建消费者,刷新请求,会发现不再轮询,会在已有的提供者之间随机选择

修改配置 Ribbon 的方式

  1. 新建一个 MyRule 类,并且把上面那个 myRule 方法挪过去
    注意】:这个 MyRule 类 不能在主应用程序的上下文(也就是 主启动类的同级目录中),所以需要单独的创建一个包
    在这里插入图片描述

    @Configuration
    public class MyRule {
        /**
         * 修改默认的负载均衡策略
         */
        @Bean
        public IRule customize(){
            // 先使用已经实现的策略——随机
            return new RandomRule();
        }
    }
    
  2. 在主启动类上加上 Ribbon 的注解:@RibbonClient

    @SpringBootApplication
    @EnableEurekaClient
    // configuration:标注 Rule 的配置类 ; name:标注需要配置的服务名
    @RibbonClient(configuration = MyRule.class,name = "SpringCloud-02-provider")
    public class Springcloud03ConsumerDept8082Application {
    	public static void main(String[] args) {
    		SpringApplication.run(Springcloud03ConsumerDept8082Application.class, args);
    	}
    }
    

自定义策略(简单示例)

  • 可以点开刚刚看的那个 RandomRule 的源代码,复制过来修改一下
  • 修改要求:每个提供者访问五次
  1. 编写 DiyRule()

    public class DiyRule extends AbstractLoadBalancerRule {
    
        public Server choose(ILoadBalancer lb, Object key) {
            if (lb == null) {
                return null;
            } else {
                Server server = null;
    
                while(server == null) {
                    if (Thread.interrupted()) {
                        return null;
                    }
                    // 获得可获得(活着的)的服务
                    List<Server> upList = lb.getReachableServers();
                    // 获得所有的服务
                    List<Server> allList = lb.getAllServers();
                    int serverCount = allList.size();
                    if (serverCount == 0) {
                        return null;
                    }
    
                //==上面是写死的======中间是修改部分==================
                    System.out.println("自定义的 Rule");
                    System.out.println(upList.size());
                    // 访问某一个提供者的次数
                    int times = 0;
                    // 提供者的下标
                    int index = 0;
                    // 从活着的服务中随机获取一个
                    server = (Server)upList.get(index);
                    if (times < 4){
                        times++;
                    }else {
                        times = 1;
                        index = (index + 1) % upList.size();
                    }
    
                //==下面是写死的======中间是修改部分===================
    
                    if (server == null) {
                        Thread.yield();
                    } else {
                        if (server.isAlive()) {
                            return server;
                        }
    
                        server = null;
                        Thread.yield();
                    }
                }
    
                return server;
            }
        }
    
        @Override
        public void initWithNiwsConfig(IClientConfig iClientConfig) {
    
        }
    
        @Override
        public Server choose(Object o) {
            return null;
        }
    }
    
    
  2. MyRule 类

    @Configuration
    public class CustomizedRule {
        /**
         * 修改默认的负载均衡策略
         */
        @Bean
        public IRule customize(){
            // 先使用已经实现的策略——随机
            return new DiyRule();
        }
    }
    
  3. 在主启动类上添加

    // configuration:标注 Rule 的配置类 ; name:标注需要配置的服务名
    @RibbonClient(name = "SPRINGCLOUD-PROVIDER", configuration = CustomizedRule.class)
    

3. 小结

Eureka 和 Ribbon 整合以后,在消费端,就不需要关系请求服务的 IP地址 和 端口号了,就只需要知道服务名称,直接调用即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuan_404

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值