作者 crossoverJie

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

@@ -12,6 +12,13 @@ @@ -12,6 +12,13 @@
12 <artifactId>netty-action-zk</artifactId> 12 <artifactId>netty-action-zk</artifactId>
13 13
14 14
  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 +
15 <dependencies> 22 <dependencies>
16 <dependency> 23 <dependency>
17 <groupId>com.crossoverjie.netty</groupId> 24 <groupId>com.crossoverjie.netty</groupId>
@@ -22,68 +22,71 @@ public class ServerCache { @@ -22,68 +22,71 @@ public class ServerCache {
22 22
23 23
24 @Autowired 24 @Autowired
25 - private LoadingCache<String,String> cache ; 25 + private LoadingCache<String, String> cache;
26 26
27 @Autowired 27 @Autowired
28 - private ZKUtil zkUtil ; 28 + private ZKUtil zkUtil;
29 29
30 - private AtomicLong index = new AtomicLong() ; 30 + private AtomicLong index = new AtomicLong();
31 31
32 32
33 - public void addCache(String key){ 33 + public void addCache(String key) {
34 cache.put(key, key); 34 cache.put(key, key);
35 } 35 }
36 36
37 37
38 /** 38 /**
39 * 更新所有缓存/先删除 再新增 39 * 更新所有缓存/先删除 再新增
  40 + *
40 * @param currentChilds 41 * @param currentChilds
41 */ 42 */
42 - public void updateCache(List<String> currentChilds){  
43 - cache.invalidateAll() ; 43 + public void updateCache(List<String> currentChilds) {
  44 + cache.invalidateAll();
44 for (String currentChild : currentChilds) { 45 for (String currentChild : currentChilds) {
45 - String key = currentChild.split("-")[1] ;  
46 - addCache(key) ; 46 + String key = currentChild.split("-")[1];
  47 + addCache(key);
47 } 48 }
48 } 49 }
49 50
50 51
51 /** 52 /**
52 * 获取所有的服务列表 53 * 获取所有的服务列表
  54 + *
53 * @return 55 * @return
54 */ 56 */
55 - public List<String> getAll(){ 57 + public List<String> getAll() {
56 58
57 - List<String> list = new ArrayList<>() ; 59 + List<String> list = new ArrayList<>();
58 60
59 - if (cache.size() == 0){ 61 + if (cache.size() == 0) {
60 List<String> allNode = zkUtil.getAllNode(); 62 List<String> allNode = zkUtil.getAllNode();
61 for (String node : allNode) { 63 for (String node : allNode) {
62 - String key = node.split("-")[1] ;  
63 - addCache(key) ; 64 + String key = node.split("-")[1];
  65 + addCache(key);
64 } 66 }
65 } 67 }
66 for (Map.Entry<String, String> entry : cache.asMap().entrySet()) { 68 for (Map.Entry<String, String> entry : cache.asMap().entrySet()) {
67 list.add(entry.getKey()); 69 list.add(entry.getKey());
68 } 70 }
69 - return list ; 71 + return list;
70 72
71 } 73 }
72 74
73 /** 75 /**
74 * 选取服务器 76 * 选取服务器
  77 + *
75 * @return 78 * @return
76 */ 79 */
77 - public String selectServer(){ 80 + public String selectServer() {
78 List<String> all = getAll(); 81 List<String> all = getAll();
79 - if (all.size() == 0){  
80 - throw new RuntimeException("路由列表为空") ; 82 + if (all.size() == 0) {
  83 + throw new RuntimeException("路由列表为空");
81 } 84 }
82 Long position = index.incrementAndGet() % all.size(); 85 Long position = index.incrementAndGet() % all.size();
83 - if (position < 0){  
84 - position = 0L ; 86 + if (position < 0) {
  87 + position = 0L;
85 } 88 }
86 89
87 - return all.get(position.intValue()) ; 90 + return all.get(position.intValue());
88 } 91 }
89 } 92 }
@@ -2,13 +2,16 @@ package com.crossoverjie.netty.action.zk.controller; @@ -2,13 +2,16 @@ package com.crossoverjie.netty.action.zk.controller;
2 2
3 import com.crossoverjie.netty.action.common.enums.StatusEnum; 3 import com.crossoverjie.netty.action.common.enums.StatusEnum;
4 import com.crossoverjie.netty.action.common.res.BaseResponse; 4 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;
6 import io.swagger.annotations.ApiOperation; 6 import io.swagger.annotations.ApiOperation;
  7 +import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Controller; 8 import org.springframework.stereotype.Controller;
8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestMethod; 10 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.ResponseBody; 11 import org.springframework.web.bind.annotation.ResponseBody;
11 12
  13 +import java.util.List;
  14 +
12 /** 15 /**
13 * Function: 16 * Function:
14 * 17 *
@@ -21,19 +24,42 @@ import org.springframework.web.bind.annotation.ResponseBody; @@ -21,19 +24,42 @@ import org.springframework.web.bind.annotation.ResponseBody;
21 public class IndexController { 24 public class IndexController {
22 25
23 26
  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 +
24 /** 46 /**
25 * 获取所有路由节点 47 * 获取所有路由节点
26 * @return 48 * @return
27 */ 49 */
28 @ApiOperation("获取所有路由节点") 50 @ApiOperation("获取所有路由节点")
29 - @RequestMapping(value = "getRoute",method = RequestMethod.POST) 51 + @RequestMapping(value = "getOneOfRoute",method = RequestMethod.POST)
30 @ResponseBody() 52 @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);
33 res.setCode(StatusEnum.SUCCESS.getCode()) ; 57 res.setCode(StatusEnum.SUCCESS.getCode()) ;
34 - res.setMessage("127.0.0.1:8080") ; 58 + res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
35 return res ; 59 return res ;
36 } 60 }
37 61
38 62
  63 +
  64 +
39 } 65 }
@@ -14,7 +14,7 @@ logging.level.root=info @@ -14,7 +14,7 @@ logging.level.root=info
14 app.zk.switch=true 14 app.zk.switch=true
15 15
16 # zk 地址 16 # zk 地址
17 -app.zk.addr=10.1.241.103:2181 17 +app.zk.addr=47.98.194.60:2181
18 18
19 # zk 注册根节点 19 # zk 注册根节点
20 app.zk.root=/route 20 app.zk.root=/route