作者 crossoverJie

:sparkles: Introducing new features.注册发现功能完成

... ... @@ -12,6 +12,13 @@
<artifactId>netty-action-zk</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.crossoverjie.netty</groupId>
... ...
... ... @@ -22,68 +22,71 @@ public class ServerCache {
@Autowired
private LoadingCache<String,String> cache ;
private LoadingCache<String, String> cache;
@Autowired
private ZKUtil zkUtil ;
private ZKUtil zkUtil;
private AtomicLong index = new AtomicLong() ;
private AtomicLong index = new AtomicLong();
public void addCache(String key){
public void addCache(String key) {
cache.put(key, key);
}
/**
* 更新所有缓存/先删除 再新增
*
* @param currentChilds
*/
public void updateCache(List<String> currentChilds){
cache.invalidateAll() ;
public void updateCache(List<String> currentChilds) {
cache.invalidateAll();
for (String currentChild : currentChilds) {
String key = currentChild.split("-")[1] ;
addCache(key) ;
String key = currentChild.split("-")[1];
addCache(key);
}
}
/**
* 获取所有的服务列表
*
* @return
*/
public List<String> getAll(){
public List<String> getAll() {
List<String> list = new ArrayList<>() ;
List<String> list = new ArrayList<>();
if (cache.size() == 0){
if (cache.size() == 0) {
List<String> allNode = zkUtil.getAllNode();
for (String node : allNode) {
String key = node.split("-")[1] ;
addCache(key) ;
String key = node.split("-")[1];
addCache(key);
}
}
for (Map.Entry<String, String> entry : cache.asMap().entrySet()) {
list.add(entry.getKey());
}
return list ;
return list;
}
/**
* 选取服务器
*
* @return
*/
public String selectServer(){
public String selectServer() {
List<String> all = getAll();
if (all.size() == 0){
throw new RuntimeException("路由列表为空") ;
if (all.size() == 0) {
throw new RuntimeException("路由列表为空");
}
Long position = index.incrementAndGet() % all.size();
if (position < 0){
position = 0L ;
if (position < 0) {
position = 0L;
}
return all.get(position.intValue()) ;
return all.get(position.intValue());
}
}
... ...
... ... @@ -2,13 +2,16 @@ package com.crossoverjie.netty.action.zk.controller;
import com.crossoverjie.netty.action.common.enums.StatusEnum;
import com.crossoverjie.netty.action.common.res.BaseResponse;
import com.crossoverjie.netty.action.common.res.NULLBody;
import com.crossoverjie.netty.action.zk.cache.ServerCache;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* Function:
*
... ... @@ -21,19 +24,42 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class IndexController {
@Autowired
private ServerCache serverCache ;
/**
* 获取所有路由节点
* @return
*/
@ApiOperation("获取所有路由节点")
@RequestMapping(value = "getAllRoute",method = RequestMethod.POST)
@ResponseBody()
public BaseResponse<List<String>> getAllRoute(){
BaseResponse<List<String>> res = new BaseResponse();
List<String> all = serverCache.getAll();
res.setDataBody(all);
res.setCode(StatusEnum.SUCCESS.getCode()) ;
res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
return res ;
}
/**
* 获取所有路由节点
* @return
*/
@ApiOperation("获取所有路由节点")
@RequestMapping(value = "getRoute",method = RequestMethod.POST)
@RequestMapping(value = "getOneOfRoute",method = RequestMethod.POST)
@ResponseBody()
public BaseResponse<NULLBody> getRoute(){
BaseResponse<NULLBody> res = new BaseResponse();
public BaseResponse<String> getOneOfRoute(){
BaseResponse<String> res = new BaseResponse();
String server = serverCache.selectServer();
res.setDataBody(server);
res.setCode(StatusEnum.SUCCESS.getCode()) ;
res.setMessage("127.0.0.1:8080") ;
res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
return res ;
}
}
... ...
... ... @@ -14,7 +14,7 @@ logging.level.root=info
app.zk.switch=true
# zk 地址
app.zk.addr=10.1.241.103:2181
app.zk.addr=47.98.194.60:2181
# zk 注册根节点
app.zk.root=/route
\ No newline at end of file
... ...