Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增随机权重路由策略 #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.crossoverjie.cim.common.route.algorithm.weightrandom;

import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
import com.crossoverjie.cim.common.route.algorithm.random.RandomHandle;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
* Function: 路由策略, 权重随机
*
* @author luozhou
* @Description:
* @date 2019/3/29 14:15
* @since JDK 1.8
*/
public class WeightRandomHandle implements RouteHandle {

private TreeMap<String, String> weigthTreeMap = new TreeMap<String, String>(new WeightComparator());
/**
* 存放相同权重节点,如果选中相同权重节点会再进入随机路由进行处理
*/
private List<String> sameWeightNodes = new ArrayList<>();


@Override
public String routeServer(List<String> values, String key) {
//初始化权重数据
// TODO: 2019-03-29 后期进行缓存,不用每次都需要处理权重
initWeightData(values);

int index = ThreadLocalRandom.current().nextInt(Integer.parseInt(weigthTreeMap.lastKey()));
Map.Entry entry = weigthTreeMap.tailMap(String.valueOf(index), false).firstEntry();
String serverNode = String.valueOf(entry.getValue());
if (sameWeightNodes.contains(serverNode))
serverNode = new RandomHandle().routeServer(sameWeightNodes, key);
return serverNode;
}

private void initWeightData(List<String> values) {
// TODO: 2019-03-29 后续优化此处代码,目前不够优雅
int total = 0;
int tmp = 0;
//处理总数
for (int i = 0; i < values.size(); i++) {
String[] serverNodes = values.get(i).split(":");
total += Integer.parseInt(serverNodes[3]);
}
//计算权重区间
for (int i = 0; i < values.size(); i++) {
String[] serverNodes = values.get(i).split(":");
tmp += Integer.parseInt(serverNodes[3]) * 100 / total;
weigthTreeMap.put(String.valueOf(tmp), values.get(i));
}
}

class WeightComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
if (Integer.parseInt((String) o1) > Integer.parseInt((String) o2))
return 1;
else return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.crossoverjie.cim.common.route.algorithm.weightrandom;

import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
import com.crossoverjie.cim.common.route.algorithm.weightrandom.WeightRandomHandle;
import org.junit.Test;

import java.util.*;

/**
* @author luozhou
* @Description:
* @date 2019/3/29 18:42
*/
public class WeightRandomHandleTest {

@Test
public void testWeightRandom(){
Map<String, Integer> countMap = new HashMap<>();
List<String> testList = new ArrayList<>();
testList.add("/route/ip-192.168.14.178:11211:8081:1");
testList.add("/route/ip-192.168.14.178:11211:8082:1");
testList.add("/route/ip-192.168.14.178:11211:8083:5");
testList.add("/route/ip-192.168.14.178:11211:8084:7");
testList.add("/route/ip-192.168.14.178:11211:8085:9");
testList.add("/route/ip-192.168.14.178:11211:8086:11");
RouteHandle handle = new WeightRandomHandle();
for (int i = 0; i <1000 ; i++) {
String routeStr = handle.routeServer(testList, "123");
if (!countMap.containsKey(routeStr)){
countMap.put(routeStr,1);
}else {
Integer intval= countMap.get(routeStr);
countMap.put(routeStr,++intval);
}

}
Iterator<Map.Entry<String, Integer>> iterable= countMap.entrySet().iterator();
while (iterable.hasNext()){
Map.Entry entry= iterable.next();
System.out.println( "节点:"+entry.getKey()+" 命中:"+entry.getValue() +"次");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class CIMServerApplication implements CommandLineRunner{

@Value("${server.port}")
private int httpPort ;
@Value("${cim.server.weight}")
private int weight;

public static void main(String[] args) {
SpringApplication.run(CIMServerApplication.class, args);
Expand All @@ -35,7 +37,7 @@ public static void main(String[] args) {
public void run(String... args) throws Exception {
//获得本机IP
String addr = InetAddress.getLocalHost().getHostAddress();
Thread thread = new Thread(new RegistryZK(addr, appConfiguration.getCimServerPort(),httpPort));
Thread thread = new Thread(new RegistryZK(addr, appConfiguration.getCimServerPort(),httpPort,weight));
thread.setName("registry-zk");
thread.start() ;
}
Expand Down
5 changes: 4 additions & 1 deletion cim-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ swagger.enable = true
# cim 服务器端口
cim.server.port=11211

# cim 服务节点的权重.权重级别是1-10
cim.server.weight=1

logging.level.root=info


Expand All @@ -25,7 +28,7 @@ monitor.channel.map.key=channelMap
app.zk.switch=true

# zk 地址
app.zk.addr=47.98.194.60:2182
app.zk.addr=127.0.0.1:2181

# zk 连接超时时限
app.zk.connect.timeout=15000
Expand Down