Skip to content

Commit eacf507

Browse files
committed
✨ Introducing new features.注册发现功能完成
1 parent 6ffd909 commit eacf507

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

netty-action-zk/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<artifactId>netty-action-zk</artifactId>
1313

1414

15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
1522
<dependencies>
1623
<dependency>
1724
<groupId>com.crossoverjie.netty</groupId>

netty-action-zk/src/main/java/com/crossoverjie/netty/action/zk/cache/ServerCache.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,68 +22,71 @@ public class ServerCache {
2222

2323

2424
@Autowired
25-
private LoadingCache<String,String> cache ;
25+
private LoadingCache<String, String> cache;
2626

2727
@Autowired
28-
private ZKUtil zkUtil ;
28+
private ZKUtil zkUtil;
2929

30-
private AtomicLong index = new AtomicLong() ;
30+
private AtomicLong index = new AtomicLong();
3131

3232

33-
public void addCache(String key){
33+
public void addCache(String key) {
3434
cache.put(key, key);
3535
}
3636

3737

3838
/**
3939
* 更新所有缓存/先删除 再新增
40+
*
4041
* @param currentChilds
4142
*/
42-
public void updateCache(List<String> currentChilds){
43-
cache.invalidateAll() ;
43+
public void updateCache(List<String> currentChilds) {
44+
cache.invalidateAll();
4445
for (String currentChild : currentChilds) {
45-
String key = currentChild.split("-")[1] ;
46-
addCache(key) ;
46+
String key = currentChild.split("-")[1];
47+
addCache(key);
4748
}
4849
}
4950

5051

5152
/**
5253
* 获取所有的服务列表
54+
*
5355
* @return
5456
*/
55-
public List<String> getAll(){
57+
public List<String> getAll() {
5658

57-
List<String> list = new ArrayList<>() ;
59+
List<String> list = new ArrayList<>();
5860

59-
if (cache.size() == 0){
61+
if (cache.size() == 0) {
6062
List<String> allNode = zkUtil.getAllNode();
6163
for (String node : allNode) {
62-
String key = node.split("-")[1] ;
63-
addCache(key) ;
64+
String key = node.split("-")[1];
65+
addCache(key);
6466
}
6567
}
6668
for (Map.Entry<String, String> entry : cache.asMap().entrySet()) {
6769
list.add(entry.getKey());
6870
}
69-
return list ;
71+
return list;
7072

7173
}
7274

7375
/**
7476
* 选取服务器
77+
*
7578
* @return
7679
*/
77-
public String selectServer(){
80+
public String selectServer() {
7881
List<String> all = getAll();
79-
if (all.size() == 0){
80-
throw new RuntimeException("路由列表为空") ;
82+
if (all.size() == 0) {
83+
throw new RuntimeException("路由列表为空");
8184
}
8285
Long position = index.incrementAndGet() % all.size();
83-
if (position < 0){
84-
position = 0L ;
86+
if (position < 0) {
87+
position = 0L;
8588
}
8689

87-
return all.get(position.intValue()) ;
90+
return all.get(position.intValue());
8891
}
8992
}

netty-action-zk/src/main/java/com/crossoverjie/netty/action/zk/controller/IndexController.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.crossoverjie.netty.action.common.enums.StatusEnum;
44
import com.crossoverjie.netty.action.common.res.BaseResponse;
5-
import com.crossoverjie.netty.action.common.res.NULLBody;
5+
import com.crossoverjie.netty.action.zk.cache.ServerCache;
66
import io.swagger.annotations.ApiOperation;
7+
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Controller;
89
import org.springframework.web.bind.annotation.RequestMapping;
910
import org.springframework.web.bind.annotation.RequestMethod;
1011
import org.springframework.web.bind.annotation.ResponseBody;
1112

13+
import java.util.List;
14+
1215
/**
1316
* Function:
1417
*
@@ -21,19 +24,42 @@
2124
public class IndexController {
2225

2326

27+
@Autowired
28+
private ServerCache serverCache ;
29+
30+
/**
31+
* 获取所有路由节点
32+
* @return
33+
*/
34+
@ApiOperation("获取所有路由节点")
35+
@RequestMapping(value = "getAllRoute",method = RequestMethod.POST)
36+
@ResponseBody()
37+
public BaseResponse<List<String>> getAllRoute(){
38+
BaseResponse<List<String>> res = new BaseResponse();
39+
List<String> all = serverCache.getAll();
40+
res.setDataBody(all);
41+
res.setCode(StatusEnum.SUCCESS.getCode()) ;
42+
res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
43+
return res ;
44+
}
45+
2446
/**
2547
* 获取所有路由节点
2648
* @return
2749
*/
2850
@ApiOperation("获取所有路由节点")
29-
@RequestMapping(value = "getRoute",method = RequestMethod.POST)
51+
@RequestMapping(value = "getOneOfRoute",method = RequestMethod.POST)
3052
@ResponseBody()
31-
public BaseResponse<NULLBody> getRoute(){
32-
BaseResponse<NULLBody> res = new BaseResponse();
53+
public BaseResponse<String> getOneOfRoute(){
54+
BaseResponse<String> res = new BaseResponse();
55+
String server = serverCache.selectServer();
56+
res.setDataBody(server);
3357
res.setCode(StatusEnum.SUCCESS.getCode()) ;
34-
res.setMessage("127.0.0.1:8080") ;
58+
res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
3559
return res ;
3660
}
3761

3862

63+
64+
3965
}

netty-action-zk/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ logging.level.root=info
1414
app.zk.switch=true
1515

1616
# zk 地址
17-
app.zk.addr=10.1.241.103:2181
17+
app.zk.addr=47.98.194.60:2181
1818

1919
# zk 注册根节点
2020
app.zk.root=/route

0 commit comments

Comments
 (0)