优化的解决方案:ribbon负载均衡算法_软负载均衡算法
优采云 发布时间: 2022-11-24 16:30优化的解决方案:ribbon负载均衡算法_软负载均衡算法
大家好,我是你们的好朋友Strones。今天就来聊聊ribbon负载均衡算法_软负载均衡算法,希望大家对编程的认识更上一层楼。
一、丝带介绍
Ribbon 是 Netflix 发布的负载均衡器,有助于控制 HTTP 和 TCP 客户端的行为。为Ribbon配置服务提供者地址列表后,Ribbon可以根据一定的负载均衡算法自动帮助服务消费者进行请求。Ribbon默认为我们提供了很多负载均衡算法,比如round robin、random等,当然我们也可以为Ribbon实现自定义的负载均衡算法。
一个。循环规则
默认情况下,轮询规则也是很多高级规则中退避的策略
BaseLoadBalancer类中的setRule方法
b. 可用性过滤规则
会过滤掉开熔断的服务或者并发连接数高的服务
C。加权响应时间规则
通过服务的平均响应时间,给每个服务一个权重。响应时间越长,权重越小,启动统计不足,应用轮询策略
// 开启权重配置
spring-cloud-order-service.ribbon.NFLoadBalancerRuleClassName=com.gupaoedu.springcloud.example.springclouduserservice.GpDefineIpHashRule
d. 重试规则
首先遵循轮询策略,如果请求服务失败,会在指定时间(30s)内重试
e. 最佳可用规则
先过滤掉断路器的服务,然后选择并发最少的那个
F。随机规则
获得随机服务
2.RoundRobinRule轮询
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
log.warn("no load balancer");
return null;
}
Server server = null;
int count = 0;
while (server == null && count++ = 10) {
log.warn("No available alive servers after 10 tries from load balancer: "
+ lb);
}
return server;
}
3.RandomRule随机
/** * Randomly choose from all living servers */
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
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 = chooseRandomInt(serverCount);
server = upList.get(index);
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;
<p>
" />
Thread.yield();
}
return server;
}
</p>
4.RetryRule重试
默认先使用round-robin算法(IRule subRule = new RoundRobinRule()),再使用retry算法;
默认是0.5s重试一次(long maxRetryMillis = 500)
<p>package com.netflix.loadbalancer;
import com.netflix.client.config.IClientConfig;
public class RetryRule extends AbstractLoadBalancerRule {
IRule subRule = new RoundRobinRule();
long maxRetryMillis = 500;
public RetryRule() {
}
public RetryRule(IRule subRule) {
this.subRule = (subRule != null) ? subRule : new RoundRobinRule();
}
public RetryRule(IRule subRule, long maxRetryMillis) {
this.subRule = (subRule != null) ? subRule : new RoundRobinRule();
this.maxRetryMillis = (maxRetryMillis > 0) ? maxRetryMillis : 500;
}
public void setRule(IRule subRule) {
this.subRule = (subRule != null) ? subRule : new RoundRobinRule();
}
public IRule getRule() {
return subRule;
}
public void setMaxRetryMillis(long maxRetryMillis) {
if (maxRetryMillis > 0) {
this.maxRetryMillis = maxRetryMillis;
} else {
this.maxRetryMillis = 500;
}
}
public long getMaxRetryMillis() {
return maxRetryMillis;
}
@Override
public void setLoadBalancer(ILoadBalancer lb) {
super.setLoadBalancer(lb);
subRule.setLoadBalancer(lb);
}
/* * Loop if necessary. Note that the time CAN be exceeded depending on the * subRule, because we're not spawning additional threads and returning * early. */
//具体重试代码
public Server choose(ILoadBalancer lb, Object key) {
long requestTime = System.currentTimeMillis();
long deadline = requestTime + maxRetryMillis;
Server answer = null;
answer = subRule.choose(key);
if (((answer == null) || (!answer.isAlive()))
&& (System.currentTimeMillis()