博客
关于我
SpringCloud进阶(四)Ribbon
阅读量:345 次
发布时间:2019-03-04

本文共 4456 字,大约阅读时间需要 14 分钟。

五 Ribbon

5.1 简介

Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 —负载均衡的工具。Ribbon主要功能是提供客户端的软件负载均衡算法服务调用,Ribbon客户端组件提供一些列完善的配置项如连接超时,重试等.

简单的说,就是在配置文件中列出Load Balance 后面所有的机器,Ribbon会自动的帮助你基于某种规则(轮循)

  • 负载均衡

在这里插入图片描述

简单的说就是将用户的请求平摊的分配到多个服务上,从而达到系统的高可用.

  • Ribbon本地负载均衡客户端与Nginx服务端负载均衡的区别
  • Nginx是服务器负载均衡,客户端所有请求都会交给Nginx,然后由Nginx实现转发请求.—即负载均衡是由服务端实现的.
  • Ribbon本地负载均衡,在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术.
  • Ribbon本地负载均衡—进程内,将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器.Ribbon就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务方的地址.(各个科室)
  • Nginx服务端负载均衡—集中式,即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5,也可是软件如Nginx),有该设施负责把访问请求通过某种策略转发值服务的提供方(医院)

5.2 基本使用

在这里插入图片描述

在这里插入图片描述

参考集群代码

在这里插入图片描述

5.3 RestTemplate

  • IRule的实现类

在这里插入图片描述

在这里插入图片描述

  • 修改规则 注意:该包不要和主启动类所在的包同级,要跟启动类所在包同级
package com.Rule;import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.RandomRule;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyRule {       /**     * IRule:     ** RoundRobinRule 轮询策略     ** RandomRule 随机策略     ** AvailabilityFilteringRule : 会先过滤掉,跳闸,访问故障的服务~,对剩下的进行轮询~     ** RetryRule : 会先按照轮询获取服务~,如果服务获取失败,则会在指定的时间内进行,重试     * @return     */    //修改默认规则    @Bean    public  IRule  myRule(){           return new RandomRule(); //随机规则    }}
  • 自定义规则

在这里插入图片描述

  • 编写算法
package com.Rule;import com.netflix.client.config.IClientConfig;import com.netflix.loadbalancer.AbstractLoadBalancerRule;import com.netflix.loadbalancer.ILoadBalancer;import com.netflix.loadbalancer.Server;import java.util.List;public class MyRandomRule extends AbstractLoadBalancerRule  {       private int total = 0;    //总共被调用的次数,目前要求每台调用次数是总机器的数量2倍    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
upList = lb.getReachableServers();//当前可用机器数量 List
allList = lb.getAllServers();//总机器数量 int serverCount = allList.size(); if (serverCount == 0) { /* * No servers. End regardless of pass, because subsequent passes * only get more restrictive. */ return null; }// int index = rand.nextInt(serverCount);// server = upList.get(index); if (total < (allList.size() * 2)) { server = upList.get(currentIndex); total++; } else { total = 0; currentIndex++; if (currentIndex >= upList.size()) { currentIndex = 0; } } if (server == null) { /* * The only time this should happen is if the server list were * somehow trimmed. This is a transient condition. Retry after * yielding. */ Thread.yield(); continue; } if (server.isAlive()) { return (server); } // Shouldn't actually happen.. but must be transient or a bug. server = null; Thread.yield(); } return server; } @Override public Server choose(Object key) { return choose(getLoadBalancer(), key); } @Override public void initWithNiwsConfig(IClientConfig clientConfig) { }}
  • 编写配置类
package com.Rule;import com.netflix.loadbalancer.IRule;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyRules {       @Bean    public IRule myRules(){           return new MyRandomRule();    }}
  • 开启服务
package com.shu;import com.Rule.MyRules;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.ribbon.RibbonClient;@SpringBootApplication@EnableEurekaClient//在微服务启动的时候就能加载自定义的Ribbon类(自定义的规则会覆盖原有默认的规则)//开启负载均衡,并指定自定义的规则@RibbonClient(value = "CLOUD-PROVIDE-PAYMENT",configuration = MyRules.class)public class OrderMain80 {       public static void main(String[] args) {           SpringApplication.run(OrderMain80.class);    }}
  • 查看结果

每一个服务接受6次请求,完成请求的分发

在这里插入图片描述

转载地址:http://jebe.baihongyu.com/

你可能感兴趣的文章
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>