作者 crossoverJie
提交者 GitHub

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

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