作者 crossoverJie
提交者 GitHub

Merge pull request #90 from crossoverJie/fix-issue-79

Fix issue 79
@@ -103,6 +103,11 @@ public class SortArrayMap { @@ -103,6 +103,11 @@ public class SortArrayMap {
103 return size; 103 return size;
104 } 104 }
105 105
  106 + public void clear(){
  107 + buckets = new Node[DEFAULT_SIZE];
  108 + size = 0 ;
  109 + }
  110 +
106 /** 111 /**
107 * 数据节点 112 * 数据节点
108 */ 113 */
@@ -29,7 +29,7 @@ public enum StatusEnum { @@ -29,7 +29,7 @@ public enum StatusEnum {
29 29
30 SERVER_NOT_AVAILABLE("7100", "cim server is not available, please try again later!"), 30 SERVER_NOT_AVAILABLE("7100", "cim server is not available, please try again later!"),
31 31
32 - RECONNECT_FAIL("7200", "reconnect fail, continue to retry!"), 32 + RECONNECT_FAIL("7200", "Reconnect fail, continue to retry!"),
33 33
34 /** 登录信息不匹配 */ 34 /** 登录信息不匹配 */
35 ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"), 35 ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"),
@@ -20,6 +20,8 @@ public class SortArrayMapConsistentHash extends AbstractConsistentHash { @@ -20,6 +20,8 @@ public class SortArrayMapConsistentHash extends AbstractConsistentHash {
20 20
21 @Override 21 @Override
22 public void add(long key, String value) { 22 public void add(long key, String value) {
  23 + // fix https://github.com/crossoverJie/cim/issues/79
  24 + sortArrayMap.clear();
23 for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) { 25 for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
24 Long hash = super.hash("vir" + key + i); 26 Long hash = super.hash("vir" + key + i);
25 sortArrayMap.add(hash,value); 27 sortArrayMap.add(hash,value);
@@ -23,6 +23,9 @@ public class TreeMapConsistentHash extends AbstractConsistentHash { @@ -23,6 +23,9 @@ public class TreeMapConsistentHash extends AbstractConsistentHash {
23 23
24 @Override 24 @Override
25 public void add(long key, String value) { 25 public void add(long key, String value) {
  26 +
  27 + // fix https://github.com/crossoverJie/cim/issues/79
  28 + treeMap.clear();
26 for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) { 29 for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
27 Long hash = super.hash("vir" + key + i); 30 Long hash = super.hash("vir" + key + i);
28 treeMap.put(hash,value); 31 treeMap.put(hash,value);
@@ -10,7 +10,6 @@ import org.springframework.stereotype.Component; @@ -10,7 +10,6 @@ import org.springframework.stereotype.Component;
10 import java.util.ArrayList; 10 import java.util.ArrayList;
11 import java.util.List; 11 import java.util.List;
12 import java.util.Map; 12 import java.util.Map;
13 -import java.util.concurrent.atomic.AtomicLong;  
14 13
15 /** 14 /**
16 * Function: 服务器节点缓存 15 * Function: 服务器节点缓存
@@ -30,9 +29,6 @@ public class ServerCache { @@ -30,9 +29,6 @@ public class ServerCache {
30 @Autowired 29 @Autowired
31 private ZKit zkUtil; 30 private ZKit zkUtil;
32 31
33 - private AtomicLong index = new AtomicLong();  
34 -  
35 -  
36 public void addCache(String key) { 32 public void addCache(String key) {
37 cache.put(key, key); 33 cache.put(key, key);
38 } 34 }
@@ -41,12 +37,12 @@ public class ServerCache { @@ -41,12 +37,12 @@ public class ServerCache {
41 /** 37 /**
42 * 更新所有缓存/先删除 再新增 38 * 更新所有缓存/先删除 再新增
43 * 39 *
44 - * @param currentChilds 40 + * @param currentChildren
45 */ 41 */
46 - public void updateCache(List<String> currentChilds) { 42 + public void updateCache(List<String> currentChildren) {
47 cache.invalidateAll(); 43 cache.invalidateAll();
48 - for (String currentChild : currentChilds) {  
49 - // currentChild=ip-127.0.0.1:11212:9082 or 127.0.0.1:11212:9082 44 + for (String currentChild : currentChildren) {
  45 + // currentChildren=ip-127.0.0.1:11212:9082 or 127.0.0.1:11212:9082
50 String key ; 46 String key ;
51 if (currentChild.split("-").length == 2){ 47 if (currentChild.split("-").length == 2){
52 key = currentChild.split("-")[1]; 48 key = currentChild.split("-")[1];
@@ -7,6 +7,8 @@ import com.google.common.cache.CacheLoader; @@ -7,6 +7,8 @@ import com.google.common.cache.CacheLoader;
7 import com.google.common.cache.LoadingCache; 7 import com.google.common.cache.LoadingCache;
8 import okhttp3.OkHttpClient; 8 import okhttp3.OkHttpClient;
9 import org.I0Itec.zkclient.ZkClient; 9 import org.I0Itec.zkclient.ZkClient;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration; 14 import org.springframework.context.annotation.Configuration;
@@ -28,6 +30,8 @@ import java.util.concurrent.TimeUnit; @@ -28,6 +30,8 @@ import java.util.concurrent.TimeUnit;
28 @Configuration 30 @Configuration
29 public class BeanConfig { 31 public class BeanConfig {
30 32
  33 + private static Logger logger = LoggerFactory.getLogger(BeanConfig.class);
  34 +
31 @Autowired 35 @Autowired
32 private AppConfiguration appConfiguration; 36 private AppConfiguration appConfiguration;
33 37
@@ -83,6 +87,7 @@ public class BeanConfig { @@ -83,6 +87,7 @@ public class BeanConfig {
83 public RouteHandle buildRouteHandle() throws Exception { 87 public RouteHandle buildRouteHandle() throws Exception {
84 String routeWay = appConfiguration.getRouteWay(); 88 String routeWay = appConfiguration.getRouteWay();
85 RouteHandle routeHandle = (RouteHandle) Class.forName(routeWay).newInstance(); 89 RouteHandle routeHandle = (RouteHandle) Class.forName(routeWay).newInstance();
  90 + logger.info("Current route algorithm is [{}]", routeHandle.getClass().getSimpleName());
86 if (routeWay.contains("ConsistentHash")) { 91 if (routeWay.contains("ConsistentHash")) {
87 //一致性 hash 算法 92 //一致性 hash 算法
88 Method method = Class.forName(routeWay).getMethod("setHash", AbstractConsistentHash.class); 93 Method method = Class.forName(routeWay).getMethod("setHash", AbstractConsistentHash.class);
@@ -154,6 +154,8 @@ public class RouteController { @@ -154,6 +154,8 @@ public class RouteController {
154 154
155 // check server available 155 // check server available
156 String server = routeHandle.routeServer(serverCache.getServerList(),String.valueOf(loginReqVO.getUserId())); 156 String server = routeHandle.routeServer(serverCache.getServerList(),String.valueOf(loginReqVO.getUserId()));
  157 + LOGGER.info("userName=[{}] route server info=[{}]", loginReqVO.getUserName(), server);
  158 +
157 RouteInfo routeInfo = RouteInfoParseUtil.parse(server); 159 RouteInfo routeInfo = RouteInfoParseUtil.parse(server);
158 commonBizService.checkServerAvailable(routeInfo); 160 commonBizService.checkServerAvailable(routeInfo);
159 161
@@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component;
12 import java.util.List; 12 import java.util.List;
13 13
14 /** 14 /**
15 - * Function: Zookeeper 工具 15 + * Function: Zookeeper kit
16 * 16 *
17 * @author crossoverJie 17 * @author crossoverJie
18 * Date: 2018/8/19 00:33 18 * Date: 2018/8/19 00:33
@@ -39,11 +39,11 @@ public class ZKit { @@ -39,11 +39,11 @@ public class ZKit {
39 public void subscribeEvent(String path) { 39 public void subscribeEvent(String path) {
40 zkClient.subscribeChildChanges(path, new IZkChildListener() { 40 zkClient.subscribeChildChanges(path, new IZkChildListener() {
41 @Override 41 @Override
42 - public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {  
43 - logger.info("Clear or update local cache parentPath=[{}],currentChilds=[{}]", parentPath,currentChilds.toString()); 42 + public void handleChildChange(String parentPath, List<String> currentChildren) throws Exception {
  43 + logger.info("Clear and update local cache parentPath=[{}],currentChildren=[{}]", parentPath,currentChildren.toString());
44 44
45 - //更新所有缓存/先删除 再新增  
46 - serverCache.updateCache(currentChilds) ; 45 + //update local cache, delete and save.
  46 + serverCache.updateCache(currentChildren) ;
47 } 47 }
48 }); 48 });
49 49
@@ -52,12 +52,12 @@ public class ZKit { @@ -52,12 +52,12 @@ public class ZKit {
52 52
53 53
54 /** 54 /**
55 - * 获取所有注册节点 55 + * get all server node from zookeeper
56 * @return 56 * @return
57 */ 57 */
58 public List<String> getAllNode(){ 58 public List<String> getAllNode(){
59 List<String> children = zkClient.getChildren("/route"); 59 List<String> children = zkClient.getChildren("/route");
60 - logger.info("查询所有节点成功=【{}】", JSON.toJSONString(children)); 60 + logger.info("Query all node =[{}] success.", JSON.toJSONString(children));
61 return children; 61 return children;
62 } 62 }
63 63
@@ -22,19 +22,19 @@ app.zk.connect.timeout=15000 @@ -22,19 +22,19 @@ app.zk.connect.timeout=15000
22 app.zk.root=/route 22 app.zk.root=/route
23 23
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 #路由策略,随机 27 #路由策略,随机
28 #app.route.way=com.crossoverjie.cim.common.route.algorithm.random.RandomHandle 28 #app.route.way=com.crossoverjie.cim.common.route.algorithm.random.RandomHandle
29 29
30 #路由策略,一致性 hash 30 #路由策略,一致性 hash
31 -#app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle 31 +app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle
32 32
33 #一致性 hash 算法具体实现--自定义有序 map 33 #一致性 hash 算法具体实现--自定义有序 map
34 #app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.SortArrayMapConsistentHash 34 #app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.SortArrayMapConsistentHash
35 35
36 #一致性 hash 算法具体实现--TreeMap 36 #一致性 hash 算法具体实现--TreeMap
37 -#app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.TreeMapConsistentHash 37 +app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.TreeMapConsistentHash
38 38
39 # Redis 配置 39 # Redis 配置
40 spring.redis.host=xx 40 spring.redis.host=xx