Ribbon
1.概念
官网:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-ribbon
Ribbon是一个客户端负载均衡器,可以让您对HTTP和TCP客户端的行为进行大量控制。Feign已使用Ribbon,因此如果您正在使用,@FeignClient则此部分也适用。
Ribbon中的一个核心概念是指定客户端的概念。每个负载均衡器都是一组组件的一部分,这些组件一起工作以按需联系远程服务器,并且该集合具有您作为应用程序开发人员提供的名称(例如,使用@FeignClient 注释)。Spring Cloud根据ApplicationContext需要为每个命名客户端 创建一个新的集合 RibbonClientConfiguration。这包含(除其他外)a ILoadBalancer,a RestClient和a ServerListFilter。
2.负载均衡策略
- RandomRule:随机策略
- RoundRobinRule:轮询策略
- WeightedResponseTimeRule:加权策略
- BestAvailableRule:请求数最少策略
- AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值)
- RetryRule:对选定的负载均衡策略机上重试机制。
- ZoneAvoidanceRule:复合判断server所在区域的性能和server的可用性选择serve
2.pom的GAV
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3.主启动类上注解
@EnableEurekaClient
4.RestTemplate
@Configuration
public class ConfigBean //boot -->spring applicationContext.xml --- @Configuration配置 ConfigBean = applicationContext.xml
{
@Bean
@LoadBalanced//Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具。
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
@Bean
public IRule myRule()
{
//return new RoundRobinRule();
//return new RandomRule();//可以用我们重新选择的随机算法替代默认的轮询。
return new RetryRule();
}
}
5.自定义负载均衡算法
5.1.自定义类
package com.qin.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: WZB
* @Date: 2019/4/15 22:17
* @Description:
*/
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<Server> upList = lb.getReachableServers();
List<Server> 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);// 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)
{
// TODO Auto-generated method stub
}
}
5.2.注入自定义类
package com.qin.ribbon;
import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: WZB
* @Date: 2019/4/15 22:14
* @Description:
*/
@Configuration
public class MySelfRule {
@Bean
public IRule myRule()
{
//return new RandomRule();// Ribbon默认是轮询,我自定义为随机
//return new RoundRobinRule();// Ribbon默认是轮询,我自定义为随机
return new RandomRule_ZY();// 我自定义为每台机器5次
}
}
5.3.主启动类上
注意自定义的负载均衡类不能和主启动类同包或在其子包下
@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)