parent
5370a8b3dc
commit
9c03285d94
@ -0,0 +1,41 @@
|
||||
package com.xxl.job.admin.core.route;
|
||||
|
||||
import com.xxl.job.admin.core.route.strategy.*;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 17/3/10.
|
||||
*/
|
||||
public enum ExecutorRouteStrategyEnum {
|
||||
|
||||
FIRST("第一个", new ExecutorRouteFirst()),
|
||||
LAST("最后一个", new ExecutorRouteLast()),
|
||||
ROUND("轮询", new ExecutorRouteRound()),
|
||||
RANDOM("随机", new ExecutorRouteRandom()),
|
||||
CONSISTENT_HASH("一致性HASH", new ExecutorRouteConsistentHash()),
|
||||
FAILOVER("故障转移", null);
|
||||
|
||||
ExecutorRouteStrategyEnum(String title, ExecutorRouter router) {
|
||||
this.title = title;
|
||||
this.router = router;
|
||||
}
|
||||
|
||||
private String title;
|
||||
private ExecutorRouter router;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public ExecutorRouter getRouter() {
|
||||
return router;
|
||||
}
|
||||
|
||||
public static ExecutorRouteStrategyEnum match(String name, ExecutorRouteStrategyEnum defaultItem){
|
||||
for (ExecutorRouteStrategyEnum item: ExecutorRouteStrategyEnum.values()) {
|
||||
if (item.name().equals(name)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return defaultItem;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.xxl.job.admin.core.route;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 17/3/10.
|
||||
*/
|
||||
public abstract class ExecutorRouter {
|
||||
|
||||
public abstract String route(int jobId, ArrayList<String> addressList);
|
||||
|
||||
public static String route(int jobId, ArrayList<String> addressList, String executorRouteStrategy){
|
||||
if (CollectionUtils.isEmpty(addressList)) {
|
||||
return null;
|
||||
}
|
||||
ExecutorRouteStrategyEnum strategy = ExecutorRouteStrategyEnum.match(executorRouteStrategy, ExecutorRouteStrategyEnum.FIRST);
|
||||
String routeAddress = strategy.getRouter().route(jobId, addressList);
|
||||
return routeAddress;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int c1 = 0;
|
||||
int c2 = 0;
|
||||
int c3 = 0;
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
String ret = ExecutorRouter.route(i, new ArrayList<String>(Arrays.asList("127.0.0.1:0000", "127.0.0.1:2222", "127.0.0.1:3333")), ExecutorRouteStrategyEnum.CONSISTENT_HASH.name());
|
||||
if (ret.equals("127.0.0.1:0000")) {
|
||||
c1++;
|
||||
} else if (ret.equals("127.0.0.1:2222")) {
|
||||
c2++;
|
||||
} else if (ret.equals("127.0.0.1:3333")) {
|
||||
c3++;
|
||||
}
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println(end - start); // 1000*100=740、1000*1=162、
|
||||
|
||||
|
||||
System.out.println(c1);
|
||||
System.out.println(c2);
|
||||
System.out.println(c3);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.xxl.job.admin.core.route.strategy;
|
||||
|
||||
import com.xxl.job.admin.core.route.ExecutorRouter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 17/3/10.
|
||||
*/
|
||||
public class ExecutorRouteFirst extends ExecutorRouter {
|
||||
|
||||
@Override
|
||||
public String route(int jobId, ArrayList<String> addressList) {
|
||||
return addressList.get(0);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.xxl.job.admin.core.route.strategy;
|
||||
|
||||
import com.xxl.job.admin.core.route.ExecutorRouter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 17/3/10.
|
||||
*/
|
||||
public class ExecutorRouteLast extends ExecutorRouter {
|
||||
|
||||
@Override
|
||||
public String route(int jobId, ArrayList<String> addressList) {
|
||||
return addressList.get(addressList.size()-1);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.xxl.job.admin.core.route.strategy;
|
||||
|
||||
import com.xxl.job.admin.core.route.ExecutorRouter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 17/3/10.
|
||||
*/
|
||||
public class ExecutorRouteRandom extends ExecutorRouter {
|
||||
|
||||
private static Random localRandom = new Random();
|
||||
|
||||
@Override
|
||||
public String route(int jobId, ArrayList<String> addressList) {
|
||||
return addressList.get(localRandom.nextInt(addressList.size()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.xxl.job.admin.core.route.strategy;
|
||||
|
||||
import com.xxl.job.admin.core.route.ExecutorRouter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 17/3/10.
|
||||
*/
|
||||
public class ExecutorRouteRound extends ExecutorRouter {
|
||||
|
||||
private static ConcurrentHashMap<Integer, Integer> routeCountEachJob = new ConcurrentHashMap<Integer, Integer>();
|
||||
private static int count(int jobId) {
|
||||
Integer count = routeCountEachJob.get(jobId);
|
||||
count = (count==null)?0:++count;
|
||||
routeCountEachJob.put(jobId, count);
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String route(int jobId, ArrayList<String> addressList) {
|
||||
return addressList.get(count(jobId)%addressList.size());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue