Ribbon自身的负载均衡算法
- RoundRobinRule(轮询算法)
- RandomRule(随机算法)
- AvailabilityFilteringRule():会先过滤由于多次访问故障而处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问
- WeightedResponseTimeRule():根据平均响应的时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越高,刚启动时如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够会切换到WeightedResponseTimeRule
- RetryRule():先按照RoundRobinRule的策略获取服务,如果获取失败则在制定时间内进行重试,获取可用的服务。
- BestAviableRule():会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务
- ZoneAvoidanceRule():默认规则,符合判断server所在区域的性能和server的可用性选择服务器
但是在实际的生产环境当中,上面的规则很难满足我们的需求,这时我们就需要自己定义一个负载均衡的算法。如:现在我们有这样一个需求,我们需要每访问五次,轮询一次!怎么办?上面ribbon给的一些默认的算法好像都不行,行吧,那我们就自己写一个自定义负载均衡算法,开工!
客户端代码:pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
这里为什么要引入eureka?是因为我这边的案例是需要结合eureka来展示,当然ribbon也是可以脱离eureka的
启动类:
package com.wuhan.hanlin;
import com.wuhan.ribbon.MySelfRule;
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;
/**
* @author:hl.yuan
* @date:2019-11-13
*/
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "MICROSERVICECLOUD-DEPT", configuration = MySelfRule.class)
public class DeptConsumer8080_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer8080_App.class,args);
}
}
代码解读:@RibbonClient(name = “MICROSERVICECLOUD-DEPT”, configuration = MySelfRule.class)
其中的name指定针对哪个服务,进行负载均衡,而configuration则是指定负载均衡的算法具体实现类
注意这里的MySelfRule不能放在springboot主启动类的当前包和子包类,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是我们达不到特殊化指定的目的了
MySelfRule
package com.wuhan.ribbon;
import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author:hl.yuan
* @date:2019-12-03
*/
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
return new RandomRule();
}
}
自定义负载均衡类:RandomRule
package com.wuhan.ribbon;
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;
/**
* @author:hl.yuan
* @date:2019-12-03
*/
public class RandomRule extends AbstractLoadBalancerRule {
// 总共被调用的次数,目前要求每台被调用5次
private int total = 0;
// 当前提供服务的机器号
private int currentIndex = 0;
@Override
public void initWithNiwsConfig(IClientConfig iClientConfig) {
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
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;
}
//private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
//private int currentIndex = 0; // 当前提供服务的机器号
if (total < 5) {
server = upList.get(currentIndex);
total++;
} else {
total = 0;
currentIndex++;
if (currentIndex >= upList.size()) {
currentIndex = 0;
}
}
if (server == null) {
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
server = null;
Thread.yield();
}
return server;
}
}
到这里我们的自定义负载均衡的关键代码就搞定了,后面就是启动相关的eureka和相关业务服务了。
项目源码: 链接:https://pan.baidu.com/s/1fC7fKATs9SCXXT8VF1Pxfg
提取码:lwxl
此项目搭建是借鉴尚硅谷springcloud视频。