Ribbon
Spring Cloud Ribbon是基于NetFlix Ribbon实现的客户端负载均衡工具
主要功能是提供客户端的软件负载均衡算法,将NetFlix的中间层服务连接在一起。在配置文件中列出LoadBalancer后面的所有机器,Ribbon会自动帮助你基于某些规则(轮询、随机数轮询、随机连接等)去连接这些服务。
自定义算法:
package com.sw.myrule;
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;
import java.util.concurrent.ThreadLocalRandom;
/**
* @Author suaxi
* @Date 2020/12/29 16:44
*/
public class MyRandomRule extends AbstractLoadBalancerRule {
//自定义算法:每个服务访问5次之后,换下一个
private int total = 0; //被调用的次数
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) {
return null;
}
// int index = chooseRandomInt(serverCount); //生成区间随机数
// server = upList.get(index); //从活着的服务中,随机获取一个
if (total<5){
server = upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex++;
if (currentIndex>upList.size()){
currentIndex = 0; //当提供服务的人的执行次数大于活着的服务数量时,重置为0
}
server = upList.get(currentIndex); //从或者的服务中来获取指定的服务
}
if (server == null) {
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
server = null;
Thread.yield();
}
return server;
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
// TODO Auto-generated method stub
}
}
注册到Spring容器中
package com.sw.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author suaxi
* @Date 2020/12/29 16:12
*/
@Configuration
public class TestRule {
@Bean
public IRule myRule(){
return new MyRandomRule();
}
}
package com.sw.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author suaxi
* @Date 2020/12/28 22:15
*/
@Configuration
public class ConfigBean {
//配置负载均衡实现RestTemplate
//IRule
//RoundRobinRule 轮询
//RandomRule 随机数
//AvailabilityFilteringRule 先过滤不可用的服务,对剩下的进行轮询
//RetryRule 先按照轮询获取服务,如果获取服务失败,则会在指定的时间内进行重试
@Bean
@LoadBalanced //注册Ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
开启自定义Ribbon注解
package com.sw.springcloud;
import com.sw.myrule.TestRule;
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 suaxi
* @Date 2020/12/28 22:29
*/
@SpringBootApplication
@EnableEurekaClient
//在微服务启动的时候加载自定义的Ribbon类
@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = TestRule.class)
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}
评论 (0)