/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.loadbalancer;
import java.util.List;
import java.util.Random;
import com.netflix.client.config.IClientConfig;
/**
* A loadbalacing strategy that randomly distributes traffic amongst existing
* servers.
*
* @author stonse
*
*/
public class RandomRule extends AbstractLoadBalancerRule {
Random rand;
public RandomRule() {
rand = new Random(); //new 一个 Random类对象
}
/**
* 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; //刚开始不知道服务哪个为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); //假如有三台微服务serverCount=3 随机获取一个数字(nextInt(3)左闭右开)
server = upList.get(index); //根据随机数获取集合的一个微服务
if (server == null) { //如果微服务微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()) { //假如这个微server是活的
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次
思路如下:
下面我们自定义一个这样的满足上面需求的策略算法:
package com.atguigu.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;
public class RandomRule_KZ 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; //刚开始不知道服务哪个为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); //假如有三台微服务serverCount=3 随机获取一个数字(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) { //如果微服务微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()) { //假如这个微server是活的
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
}
}
关键的两个变量
这个自定义配置类已经完成,我们需要使用我们自己定义的负载均衡策略算法RandomRule_KZ
package com.atguigu.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
//return new RoundRobinRule();
// return new RandomRule();//达到的目的,用我们重新选择的随机算法替代默认的轮询。
//自定义算法(每台机器访问五次)
return new RandomRule_KZ();
}
}
代码我们就已经修改完成,我们就测试下能不能实现每个微服务访问5次的需求,
依然启动所有的微服务,测试;http://localhost/consumer/dept/get/1
不停刷新会发现每个微服务都被调用五次
不停刷新会发现每个微服务都被调用五次
不停刷新会发现每个微服务都被调用五次
综上所讲,通过该源码的方式我们实现了自己的需求
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/ywl470812087/article/details/102646218
内容来源于网络,如有侵权,请联系作者删除!