作者 crossoverJie

:recycle: Refactoring code.

@@ -45,8 +45,8 @@ cim.clear.route.request.url=http://45.78.28.220:8083/offLine @@ -45,8 +45,8 @@ cim.clear.route.request.url=http://45.78.28.220:8083/offLine
45 #cim.clear.route.request.url=http://localhost:8083/offLine 45 #cim.clear.route.request.url=http://localhost:8083/offLine
46 46
47 # 客户端唯一ID 47 # 客户端唯一ID
48 -cim.user.id=1551152636573  
49 -cim.user.userName=zhangsan 48 +cim.user.id=1551267098213
  49 +cim.user.userName=test3
50 50
51 # 回调线程队列大小 51 # 回调线程队列大小
52 cim.callback.thread.queue.size = 2 52 cim.callback.thread.queue.size = 2
@@ -14,7 +14,8 @@ public interface RouteHandle { @@ -14,7 +14,8 @@ public interface RouteHandle {
14 /** 14 /**
15 * 再一批服务器里进行路由 15 * 再一批服务器里进行路由
16 * @param values 16 * @param values
  17 + * @param key
17 * @return 18 * @return
18 */ 19 */
19 - String routeServer(List<String> values) ; 20 + String routeServer(List<String> values,String key) ;
20 } 21 }
@@ -33,9 +33,7 @@ public abstract class AbstractConsistentHash { @@ -33,9 +33,7 @@ public abstract class AbstractConsistentHash {
33 */ 33 */
34 protected abstract String getFirstNodeValue(String value); 34 protected abstract String getFirstNodeValue(String value);
35 35
36 - public String process(List<String> values){  
37 - String key = values.get(0);  
38 - values.remove(0) ; 36 + public String process(List<String> values,String key){
39 37
40 for (String value : values) { 38 for (String value : values) {
41 add(hash(value), value); 39 add(hash(value), value);
@@ -15,7 +15,7 @@ public class ConsistentHashHandle implements RouteHandle { @@ -15,7 +15,7 @@ public class ConsistentHashHandle implements RouteHandle {
15 private AbstractConsistentHash hash = new SortArrayMapConsistentHash(); 15 private AbstractConsistentHash hash = new SortArrayMapConsistentHash();
16 16
17 @Override 17 @Override
18 - public String routeServer(List<String> values) {  
19 - return hash.process(values); 18 + public String routeServer(List<String> values, String key) {
  19 + return hash.process(values, key);
20 } 20 }
21 } 21 }
@@ -16,7 +16,7 @@ public class LoopHandle implements RouteHandle { @@ -16,7 +16,7 @@ public class LoopHandle implements RouteHandle {
16 private AtomicLong index = new AtomicLong(); 16 private AtomicLong index = new AtomicLong();
17 17
18 @Override 18 @Override
19 - public String routeServer(List<String> values) { 19 + public String routeServer(List<String> values,String key) {
20 if (values.size() == 0) { 20 if (values.size() == 0) {
21 throw new RuntimeException("CIM 服务器可用服务列表为空"); 21 throw new RuntimeException("CIM 服务器可用服务列表为空");
22 } 22 }
@@ -15,11 +15,11 @@ public class SortArrayMapConsistentHashTest { @@ -15,11 +15,11 @@ public class SortArrayMapConsistentHashTest {
15 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 15 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
16 16
17 List<String> strings = new ArrayList<String>(); 17 List<String> strings = new ArrayList<String>();
18 - strings.add("zhangsan") ; 18 + strings.add("") ;
19 for (int i = 0; i < 10; i++) { 19 for (int i = 0; i < 10; i++) {
20 strings.add("127.0.0." + i) ; 20 strings.add("127.0.0." + i) ;
21 } 21 }
22 - String process = map.process(strings); 22 + String process = map.process(strings,"zhangsan");
23 System.out.println(process); 23 System.out.println(process);
24 Assert.assertEquals("127.0.0.8",process); 24 Assert.assertEquals("127.0.0.8",process);
25 25
@@ -30,11 +30,10 @@ public class SortArrayMapConsistentHashTest { @@ -30,11 +30,10 @@ public class SortArrayMapConsistentHashTest {
30 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 30 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
31 31
32 List<String> strings = new ArrayList<String>(); 32 List<String> strings = new ArrayList<String>();
33 - strings.add("zhangsan2");  
34 for (int i = 0; i < 10; i++) { 33 for (int i = 0; i < 10; i++) {
35 strings.add("127.0.0." + i) ; 34 strings.add("127.0.0." + i) ;
36 } 35 }
37 - String process = map.process(strings); 36 + String process = map.process(strings,"zhangsan2");
38 System.out.println(process); 37 System.out.println(process);
39 38
40 Assert.assertEquals("127.0.0.4",process); 39 Assert.assertEquals("127.0.0.4",process);
@@ -45,11 +44,10 @@ public class SortArrayMapConsistentHashTest { @@ -45,11 +44,10 @@ public class SortArrayMapConsistentHashTest {
45 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 44 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
46 45
47 List<String> strings = new ArrayList<String>(); 46 List<String> strings = new ArrayList<String>();
48 - strings.add("1551253899106") ;  
49 for (int i = 0; i < 10; i++) { 47 for (int i = 0; i < 10; i++) {
50 strings.add("127.0.0." + i) ; 48 strings.add("127.0.0." + i) ;
51 } 49 }
52 - String process = map.process(strings); 50 + String process = map.process(strings,"1551253899106");
53 51
54 System.out.println(process); 52 System.out.println(process);
55 Assert.assertEquals("127.0.0.6",process); 53 Assert.assertEquals("127.0.0.6",process);
@@ -61,11 +59,10 @@ public class SortArrayMapConsistentHashTest { @@ -61,11 +59,10 @@ public class SortArrayMapConsistentHashTest {
61 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 59 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
62 60
63 List<String> strings = new ArrayList<String>(); 61 List<String> strings = new ArrayList<String>();
64 - strings.add("1551253899106") ;  
65 strings.add("45.78.28.220:9000:8081") ; 62 strings.add("45.78.28.220:9000:8081") ;
66 strings.add("45.78.28.220:9100:9081") ; 63 strings.add("45.78.28.220:9100:9081") ;
67 64
68 - String process = map.process(strings); 65 + String process = map.process(strings,"1551253899106");
69 66
70 System.out.println(process); 67 System.out.println(process);
71 Assert.assertEquals("45.78.28.220:9000:8081",process); 68 Assert.assertEquals("45.78.28.220:9000:8081",process);
@@ -75,12 +72,11 @@ public class SortArrayMapConsistentHashTest { @@ -75,12 +72,11 @@ public class SortArrayMapConsistentHashTest {
75 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 72 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
76 73
77 List<String> strings = new ArrayList<String>(); 74 List<String> strings = new ArrayList<String>();
78 - strings.add("1551253899106") ;  
79 strings.add("45.78.28.220:9000:8081") ; 75 strings.add("45.78.28.220:9000:8081") ;
80 strings.add("45.78.28.220:9100:9081") ; 76 strings.add("45.78.28.220:9100:9081") ;
81 strings.add("45.78.28.220:9100:10081") ; 77 strings.add("45.78.28.220:9100:10081") ;
82 78
83 - String process = map.process(strings); 79 + String process = map.process(strings,"1551253899106");
84 80
85 System.out.println(process); 81 System.out.println(process);
86 Assert.assertEquals("45.78.28.220:9100:10081",process); 82 Assert.assertEquals("45.78.28.220:9100:10081",process);
@@ -91,12 +87,11 @@ public class SortArrayMapConsistentHashTest { @@ -91,12 +87,11 @@ public class SortArrayMapConsistentHashTest {
91 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 87 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
92 88
93 List<String> strings = new ArrayList<String>(); 89 List<String> strings = new ArrayList<String>();
94 - strings.add("1551253899106") ;  
95 strings.add("45.78.28.220:9000:8081") ; 90 strings.add("45.78.28.220:9000:8081") ;
96 strings.add("45.78.28.220:9100:9081") ; 91 strings.add("45.78.28.220:9100:9081") ;
97 strings.add("45.78.28.220:9100:10081") ; 92 strings.add("45.78.28.220:9100:10081") ;
98 93
99 - String process = map.process(strings); 94 + String process = map.process(strings,"1551253899106");
100 95
101 System.out.println(process); 96 System.out.println(process);
102 Assert.assertEquals("45.78.28.220:9100:10081",process); 97 Assert.assertEquals("45.78.28.220:9100:10081",process);
@@ -106,15 +101,17 @@ public class SortArrayMapConsistentHashTest { @@ -106,15 +101,17 @@ public class SortArrayMapConsistentHashTest {
106 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 101 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
107 102
108 List<String> strings = new ArrayList<String>(); 103 List<String> strings = new ArrayList<String>();
109 - strings.add("1551253899106") ;  
110 strings.add("45.78.28.220:9000:8081") ; 104 strings.add("45.78.28.220:9000:8081") ;
111 strings.add("45.78.28.220:9100:9081") ; 105 strings.add("45.78.28.220:9100:9081") ;
112 strings.add("45.78.28.220:9100:10081") ; 106 strings.add("45.78.28.220:9100:10081") ;
113 strings.add("45.78.28.220:9100:00081") ; 107 strings.add("45.78.28.220:9100:00081") ;
114 108
115 - String process = map.process(strings); 109 + String process = map.process(strings,"1551253899106");
116 110
117 System.out.println(process); 111 System.out.println(process);
118 Assert.assertEquals("45.78.28.220:9100:10081",process); 112 Assert.assertEquals("45.78.28.220:9100:10081",process);
119 } 113 }
  114 +
  115 +
  116 +
120 } 117 }
@@ -13,11 +13,10 @@ public class TreeMapConsistentHashTest { @@ -13,11 +13,10 @@ public class TreeMapConsistentHashTest {
13 AbstractConsistentHash map = new TreeMapConsistentHash() ; 13 AbstractConsistentHash map = new TreeMapConsistentHash() ;
14 14
15 List<String> strings = new ArrayList<String>(); 15 List<String> strings = new ArrayList<String>();
16 - strings.add("zhangsan") ;  
17 for (int i = 0; i < 10; i++) { 16 for (int i = 0; i < 10; i++) {
18 strings.add("127.0.0." + i) ; 17 strings.add("127.0.0." + i) ;
19 } 18 }
20 - String process = map.process(strings); 19 + String process = map.process(strings,"zhangsan");
21 System.out.println(process); 20 System.out.println(process);
22 Assert.assertEquals("127.0.0.8",process); 21 Assert.assertEquals("127.0.0.8",process);
23 } 22 }
@@ -29,11 +28,10 @@ public class TreeMapConsistentHashTest { @@ -29,11 +28,10 @@ public class TreeMapConsistentHashTest {
29 AbstractConsistentHash map = new TreeMapConsistentHash() ; 28 AbstractConsistentHash map = new TreeMapConsistentHash() ;
30 29
31 List<String> strings = new ArrayList<String>(); 30 List<String> strings = new ArrayList<String>();
32 - strings.add("zhangsan2");  
33 for (int i = 0; i < 10; i++) { 31 for (int i = 0; i < 10; i++) {
34 strings.add("127.0.0." + i) ; 32 strings.add("127.0.0." + i) ;
35 } 33 }
36 - String process = map.process(strings); 34 + String process = map.process(strings,"zhangsan2");
37 System.out.println(process); 35 System.out.println(process);
38 36
39 Assert.assertEquals("127.0.0.4",process); 37 Assert.assertEquals("127.0.0.4",process);
@@ -45,11 +43,10 @@ public class TreeMapConsistentHashTest { @@ -45,11 +43,10 @@ public class TreeMapConsistentHashTest {
45 AbstractConsistentHash map = new TreeMapConsistentHash() ; 43 AbstractConsistentHash map = new TreeMapConsistentHash() ;
46 44
47 List<String> strings = new ArrayList<String>(); 45 List<String> strings = new ArrayList<String>();
48 - strings.add("1551253899106") ;  
49 for (int i = 0; i < 10; i++) { 46 for (int i = 0; i < 10; i++) {
50 strings.add("127.0.0." + i) ; 47 strings.add("127.0.0." + i) ;
51 } 48 }
52 - String process = map.process(strings); 49 + String process = map.process(strings,"1551253899106");
53 50
54 System.out.println(process); 51 System.out.println(process);
55 Assert.assertEquals("127.0.0.6",process); 52 Assert.assertEquals("127.0.0.6",process);