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

Fix issue 79 #90

Merged
merged 2 commits into from
Apr 20, 2020
Merged
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
Expand Up @@ -103,6 +103,11 @@ public int size() {
return size;
}

public void clear(){
buckets = new Node[DEFAULT_SIZE];
size = 0 ;
}

/**
* 数据节点
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
* Function: 服务器节点缓存
Expand All @@ -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);
}
Expand All @@ -41,12 +37,12 @@ public void addCache(String key) {
/**
* 更新所有缓存/先删除 再新增
*
* @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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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;
Expand All @@ -28,6 +30,8 @@
@Configuration
public class BeanConfig {

private static Logger logger = LoggerFactory.getLogger(BeanConfig.class);

@Autowired
private AppConfiguration appConfiguration;

Expand Down Expand Up @@ -83,6 +87,7 @@ public OkHttpClient okHttpClient() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) th

// 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;

/**
* Function: Zookeeper 工具
* Function: Zookeeper kit
*
* @author crossoverJie
* Date: 2018/8/19 00:33
Expand All @@ -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) ;
}
});

Expand All @@ -52,12 +52,12 @@ public void handleChildChange(String parentPath, List<String> currentChilds) thr


/**
* 获取所有注册节点
* 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;
}

Expand Down
6 changes: 3 additions & 3 deletions cim-forward-route/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down