Ribbon有多种策略,比如轮询,随机,根据响应时间加权.
Ribbon七种算法:
1.RoundRobinRule 轮询
2.RandomPule 随机
3.AvailabilityFilteringRule 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问
4.WeightedResponseTimeRule 根据平均响应时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越高,刚启动时如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够,会切换到WeightedResponseTimeRule
5.RetryRule 先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内会进行重试,获取可用的服务.
6.BestAvailableRule 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务
7.ZoneAvoidanceRule 默认规则,复合判断server所在区域的性能和server的可用性选择服务器
1.更换Ribbon算法
1.在消费者服务里面的配置类
@Bean
public IRule myRule(){
return new RandomRule();//达到的目的,用我们重新选择的随机算法替代默认的轮询。
}
需要重启Eureka注册中心让算法生效
2.自定义Ribbon算法
启动类添加@RibbonClient注解
在启动该微服务的时候就能去加载我们自定义的Ribbon配置类,从而使配置生效
@SpringBootApplication
@EnableEurekaClient //跟Eureka整合
//在启动该微服务的时候就能去加载我们的自定义Ribbon配置类,从而使配置生效
//@RibbonClient(name=”MICROSERVICECLOUD-DEPT”,configuration=MySelfRule.class)
@RibbonClient(name=”MICROSERVICECLOUD-DEPT”,configuration=MySelfRule.class)
public class DeptConsumer80_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
编写MySelfRule类,需要注意,这个自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是说达不到特殊化定制的目的了
也不能在主启动类在一起的包,或者是主启动类所在的包的子包下,因为@SpringBootApplication注解里面包含了@ComponentScan注解
@Configuration
public class MySelfRule{
@Bean
public IRule myRule(){
//return new RandomRule();// Ribbon默认是轮询,我自定义为随机
//return new RoundRobinRule();// Ribbon默认是轮询,我自定义为随机
return new RandomRule_ZY();// 我自定义为每台机器5次
}
}
根据源码改写RandomRule_ZY类
import java.util.List;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
public class RandomRule_ZY extends AbstractLoadBalancerRule
{
// total = 0 // 当total==5以后,我们指针才能往下走,
// index = 0 // 当前对外提供服务的服务器地址,
// total需要重新置为零,但是已经达到过一个5次,我们的index = 1
// 分析:我们5次,但是微服务只有8001 8002 8003 三台,OK?
//
private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
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
List
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);// java.util.Random().nextInt(3);
// server = upList.get(index);
// 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) {
/
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)
{
}
}