文章目录
一、Ribbon负载均衡
1.1 概念
基于Netflix Ribbon实现的一套客户端负载均衡的工具。
主要功能是提供客户端的软件负载均衡算法,将netflix的中间层服务连接在一起,Ribbon客户端组件提供一些完善的配置项如连接超时,重试等,简单的说,就是在配置中列出Load Balancer后面所有的机器,Ribbon会自动帮你基于某种规则(如简单轮询,随机连接等)去连接这些机器,我们也很容易使用Ribbon实现自定义的负载均衡算法。j
1.2 作用
LB,负载均衡,在微服务或者分布式集群中经常用的一种应用,负载均衡简单的说就是将用户的请求平摊到多个服务器上,从而达到系统的HA,常见的负载均衡的软件Nginx,LVS,硬件F5等,相应的在中间件,例如:dubbo和SpringCloud中均给我们提供了负载均衡,SpringCloud的负载均衡算法可以自定义。
1.3 负载均衡分类
- 集中式LB
在服务的消费方和提供方之间使用独立的LB设施(可以是硬件),由该设施负责把访问的请求通过某种策略转发至服务的提供方。 - 进程内LB
将LB逻辑集成至消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器。Ribbon就属于一个进程内LB,他只是一个类库,集成于消费方进程,消费方通过他来获取到服务提供方的地址。
二、Ribbon配置初步
2.1 修改客户端的pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
2.2 修改客户端的配置文件
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7003.com:7003/eureka,http://eureka7004.com:7004/eureka,http://eureka7005.com:7005/eureka
2.3 修改配置类
- 真正干活的是RestTemplate实例,
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate getInstance(){
return new RestTemplateBuilder().build();
}
}
2.4 修改主启动类
@SpringBootApplication
@EnableEurekaClient
public class RubbishCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(RubbishCustomerApplication.class, args);
}
}
2.5 修改客户端访问类
- 通过微服务的真实名称访问
@RestController
public class RubbishControler {
//String URL = "http://127.0.0.1:7001";
String URL = "http://Rubbish-Server";
......
}
2.6 测试
- 启动eureka集群,再启动service服务生产模块,再启动客户端。
- 访问结果
* 访问过程
客户端通过在eureka服务上找到对应名称的微服务,然后去使用该微服务。不是通过以前写死的地址去访问。
三、
3.1 ribbon工作步骤
- 第一步先选择eurekaServer,他优先选择同一个区域内负载较少的server
- 第二步根据用户指定的策略,再从server中选择一个地址,其中ribbon提供了多种策略:比如轮询,随机和根据响应时间加权。
3.2 添加新的服务
- 修改端口号
server:
port: 8003
- 修改数据库url(这样那就可以拥有自己独立的数据库)
spring:
url: jdbc:mysql://localhost:3306/Bishe
- 微服务名称不能改变
spring:
application:
name: Rubbish-Server
记得修改主启动类:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class RubbishService8003Application {
public static void main(String[] args) {
SpringApplication.run(RubbishService8003Application.class, args);
}
}
3.3 测试
- 启动eureka集群
- 启动生产者模块(这里配置了3个,且拥有自己独立的数据库)
- 启动消费者模块
第一次访问:
>第二次访问:
>第三次访问:
可见Ribbon负载均衡他通过在eureka上查询可用的服务,默认采用轮询的算法,每次将可用的服务地址提供给客户端使用。
3.4 测试2
- 可用的eureka服务,和可用的微服务提供者。
四、Ribbon核心组件IRule
4.1 概述
根据特定算法从服务列表中选取一个要访问的服务。
4.2 提供的算法
- RoundRobinRule
轮询 - RandomRule
随机 - AailabilityFilteringRule
会过滤掉由于多次访问故障而处于断路器跳闸状态的服务,还有病并发的连接数量超过阀值的服务,然后对剩余的服务列表按照轮询策略进行访问。 - WeightdResponseTimeRue
根据平均响应时间计算所有服务的权重,响应速度越快权重越大被选中的几率越高,刚启动时如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够,会切换到AailabilityFilteringRule。 - RetryRule
先按照RoundRobinRule的策略获取服务,如果获取服务失败则在制定的时间内进行重试,获取可用的服务。(刚开始会有几次访问到不可用的服务,后面会跳过这个不可用的服务。) - BestAvailableRule
会过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量小的服务。 - ZoneAvoidanceRule
默认规则,复合判断server所在区域的性能和server的可用性选择服务器。
4.3 更换默认的算法
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate getInstance(){
return new RestTemplateBuilder().build();
}
@Bean
public IRule myRule(){
return new RoundRobinRule();
}
}
五、自定义Ribbon算法
5.1 修改主启动类
- 客户端上添加@RibbonClient
- name是服务名称,configuration是配置类的名称
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "Rubbish-Server",configuration = MySelRule.class)
public class RubbishCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(RubbishCustomerApplication.class, args);
}
}
5.2 注意的问题
- 自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是说我们达不到特殊化定制的目的。
- 自定义配置的类的路径(可作为参考)
5.3 自定义算法配置类
@Configuration
public class MySelRule {
@Bean
public IRule myRule(){
return new RandomRule();
}
}
5.4 简单自定义负载均衡算法
- 顺序调用服务,每个机器调用5次
- 参考Ribbon提供的轮询算法
public class MyRule extends AbstractLoadBalancerRule {
public MyRule() {
}
@SuppressWarnings({"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"})
int total = 0;//被调用的次数
int currentIndex = 0;//当前提供服务的机器号
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;
}
if(total < 5){
server = (Server)upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex ++;
if(currentIndex >= upList.size()){
currentIndex = 0;
}
}
if (server == null) {
Thread.yield();
} else {
if (server.isAlive()) {
return server;
}
server = null;
Thread.yield();
}
}
return server;
}
}
public Server choose(Object key) {
return this.choose(this.getLoadBalancer(), key);
}
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
}