Spring Cloud学习笔记——06Ribbon:负载均衡(基于客户端)

一. 负载均衡以及Ribbon

1. Ribbon是什么?
  1. Spring Cloud Ribbon 是基于Netflix Ribbon 实现的一套客户端负载均衡的工具。
    简单的说,Ribbon 是 Netflix 发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将 Netflix 的中间层服务连接在一起。Ribbon 的客户端组件提供一系列完整的配置项,如:连接超时、重试等。简单的说,就是在配置文件中列出 LoadBalancer (简称LB:负载均衡) 后面所有的及其,Ribbon 会自动的帮助你基于某种规则 (如简单轮询,随机连接等等) 去连接这些机器。我们也容易使用 Ribbon 实现自定义的负载均衡算法!
2. Ribbon能干嘛?
  1. LB,即负载均衡 (LoadBalancer) ,在微服务或分布式集群中经常用的一种应用。
  2. 负载均衡简单的说就是将用户的请求平摊的分配到多个服务上,从而达到系统的HA (高用)。
  3. 常见的负载均衡软件有 Nginx、Lvs 等等。
  4. Dubbo、SpringCloud 中均给我们提供了负载均衡,SpringCloud 的负载均衡算法可以自定义。
3. 负载均衡简单分类:
  1. 集中式LB
    即在服务的提供方和消费方之间使用独立的LB设施,如Nginx,由该设施负责把访问请求通过某种策略转发至服务的提供方!
  2. 进程式LB
    将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选出一个合适的服务器。
    Ribbon 就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址!

二. 集成Ribbon

  1. 导入依赖

    <!--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

    server:
      port: 80
      
    # Eureka配置  
    eureka:
      client:
        register-with-eureka: false   # 不向Eureka注册自己
        service-url: 
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    
    
  3. 在客户端主程序中添加相关注解

    @SpringBootApplication
    @EnableEurekaClient
    public class DeptConsumer_80 {
        public static void main(String[] args) {
            SpringApplication.run(DeptConsumer_80.class, args);
        }
    }
    
  4. 在客户端的RestTemplate实现中添加负载均衡的注解

    @Configuration
    // @Configuration --> spring applicationContext.xml
    public class ConfigBean {
        // 配置负载均衡实现RestTemplate
        @Bean
        @LoadBalanced //Rabbin基于客户端实现
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }
    
  5. 修改DeptConsumerController

    因为服务访问的路径会动态变化, 所以需要修改DeptConsumerController中指定的路径

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 通过Rabbin实现的时候, 这里的地址应该是一个变量
     * 应该通过服务名来访问
     */
    // private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
    

三. 使用Ribbon实现负载均衡

1. 架构图

  • 在这里插入图片描述

2. 具体步骤

  1. 创建多个服务端
    在这里插入图片描述

  2. 修改每个服务的配置文件

    # 指定服务的端口号
    server:
      port: 8003
    
    # mybatis的配置
    mybatis:
      type-aliases-package: com.hjf.pojo
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
    spring:
      application:
        # 不同客户端的服务名称保持一致, 因为是通过服务名称来访问
        name: springcloud-provider-dept
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        # 每个服务填自己相关的数据库
        url: jdbc:mysql://jiangfengtime.top:3306/db03?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456
    
    # eureka配置
    # 1. 服务注册到哪里
    eureka:
      client:
        service-url:
          # 单机发布
          # defaultZone: http://localhost:7001/eureka/
          # 集群发布
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        # 修改eureka上默认描述信息
        # 每个服务的注册信息
        instance-id: springcloud-provider-dept8003
    
    # info配置(顶级)
    info:
      app.name: hjf-springcloud
      company.name: www.jiangfengtime.top
    
  3. 创建对应的数据表
    在这里插入图片描述

  4. 启动服务, 查看eureka集群信息
    在这里插入图片描述

  5. 查询数据信息

    刷新连接, 会读取不同数据库中的数据
    默认是轮询

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

3. 自定义切换方法

1. 调用系统自带的方法
  1. 案例
    @Configuration
    // @Configuration --> spring applicationContext.xml
    public class ConfigBean {
        /**
         * 配置负载均衡实现RestTemplate
         *  IRule类
         *      RoundRobinRule: 轮询(默认)
         *      RandomRule: 随机
         *      AvailabilityFilteringRule: 先会过滤掉, 跳闸, 访问故障的服务, 对剩下的进行轮询
         *      RetryRule: 会先按照轮询获取服务, 如果服务获取失败, 则会在指定的时间内, 进行重试
         */
        @Bean
        @LoadBalanced //Rabbin基于客户端实现
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    
        // 轮询
        @Bean
        public IRule myRule(){
            return new RandomRule();
        }
    }
    
2. 将自定义方法分离到单独的类中
  1. 自定义负载均衡算法类

    将1中的方法拆出到单独的自定义类中

    // 自定义轮询类
    @Configuration
    public class HjfRule {
        // 轮询
        @Bean
        public IRule myRule(){
            return new RandomRule();
        }
    }
    
  2. 主函数中引入负载均衡算法类

    @SpringBootApplication
    @EnableEurekaClient
    // 引入自定义的负载均衡算法类
    // 在微服务启动的时候, 就可加载我们自定义的Ribbon类
    @RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT", configuration = HjfRule.class)
    public class DeptConsumer_80 {
        public static void main(String[] args) {
            SpringApplication.run(DeptConsumer_80.class, args);
        }
    }
    
    
  3. 注意
    自定义类不能声明在主函数的统计目录之下, 因为在主函数目录之下的bean会被自动扫描加载
    该包不要和主启动类所在的包同级,要跟启动类所在包同级
    在这里插入图片描述

  4. 自定义类

    public class HjfRandomRule extends AbstractLoadBalancerRule {
        private int total = 0;  // 被访问的次数
        private int currentIndex = 0;   // 当前是谁在提供服务
        
        public Server choose(ILoadBalancer lb, Object key) {
            if (lb == null) {
                return null;
            }
            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;
                }
    
                // ****************自定义方法****************************
                if (total < 5) {
                    server = upList.get(currentIndex);
                    total += 1;
                } else {
                    total = 0;
                    currentIndex += 1;
                    if (currentIndex >= upList.size()){
                        currentIndex = 0;
                    }
                    // 从或者的服务中, 获取指定的服务来操作
                    server = upList.get(currentIndex);
                }
                // ********************************************
                if (server == null) {
                    Thread.yield();
                    continue;
                }
                if (server.isAlive()) {
                    return (server);
                }
                server = null;
                Thread.yield();
            }
            return server;
        }
    
    
    	@Override
    	public Server choose(Object key) {
    		return choose(getLoadBalancer(), key);
    	}
    
    	@Override
    	public void initWithNiwsConfig(IClientConfig clientConfig) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值