正在显示
2 个修改的文件
包含
30 行增加
和
0 行删除
cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/random/RandomHandle.java
0 → 100644
| 1 | +package com.crossoverjie.cim.common.route.algorithm.random; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.common.route.algorithm.RouteHandle; | ||
| 4 | +import java.util.concurrent.ThreadLocalRandom; | ||
| 5 | + | ||
| 6 | +import java.util.List; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * Function: 路由策略, 随机 | ||
| 10 | + * | ||
| 11 | + * @Auther: jiangyunxiong | ||
| 12 | + * @Date: 2019/3/7 11:56 | ||
| 13 | + * @since JDK 1.8 | ||
| 14 | + */ | ||
| 15 | +public class RandomHandle implements RouteHandle { | ||
| 16 | + | ||
| 17 | + @Override | ||
| 18 | + public String routeServer(List<String> values, String key) { | ||
| 19 | + int size = values.size(); | ||
| 20 | + if (size == 0) { | ||
| 21 | + throw new RuntimeException("CIM 服务器可用服务列表为空"); | ||
| 22 | + } | ||
| 23 | + int offset = ThreadLocalRandom.current().nextInt(size); | ||
| 24 | + | ||
| 25 | + return values.get(offset); | ||
| 26 | + } | ||
| 27 | +} |
| @@ -24,6 +24,9 @@ app.zk.root=/route | @@ -24,6 +24,9 @@ app.zk.root=/route | ||
| 24 | #路由策略,轮询 | 24 | #路由策略,轮询 |
| 25 | #app.route.way=com.crossoverjie.cim.common.route.algorithm.loop.LoopHandle | 25 | #app.route.way=com.crossoverjie.cim.common.route.algorithm.loop.LoopHandle |
| 26 | 26 | ||
| 27 | +#路由策略,随机 | ||
| 28 | +#app.route.way=com.crossoverjie.cim.common.route.algorithm.random.RandomHandle | ||
| 29 | + | ||
| 27 | #路由策略,一致性 hash | 30 | #路由策略,一致性 hash |
| 28 | app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle | 31 | app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle |
| 29 | 32 |
-
请 注册 或 登录 后发表评论