|
1
|
package com.crossoverjie.cim.route.config;
|
1
|
package com.crossoverjie.cim.route.config;
|
|
2
|
|
2
|
|
|
3
|
import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
|
3
|
import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
|
|
4
|
-import com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle;
|
4
|
+import com.crossoverjie.cim.common.route.algorithm.consistenthash.AbstractConsistentHash;
|
|
5
|
import com.google.common.cache.CacheBuilder;
|
5
|
import com.google.common.cache.CacheBuilder;
|
|
6
|
import com.google.common.cache.CacheLoader;
|
6
|
import com.google.common.cache.CacheLoader;
|
|
7
|
import com.google.common.cache.LoadingCache;
|
7
|
import com.google.common.cache.LoadingCache;
|
|
@@ -15,28 +15,29 @@ import org.springframework.data.redis.core.RedisTemplate; |
|
@@ -15,28 +15,29 @@ import org.springframework.data.redis.core.RedisTemplate; |
|
15
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
15
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
16
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
16
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
17
|
|
17
|
|
|
|
|
18
|
+import java.lang.reflect.Method;
|
|
18
|
import java.util.concurrent.TimeUnit;
|
19
|
import java.util.concurrent.TimeUnit;
|
|
19
|
|
20
|
|
|
20
|
/**
|
21
|
/**
|
|
21
|
* Function:
|
22
|
* Function:
|
|
22
|
*
|
23
|
*
|
|
23
|
* @author crossoverJie
|
24
|
* @author crossoverJie
|
|
24
|
- * Date: 2018/12/23 00:25
|
25
|
+ * Date: 2018/12/23 00:25
|
|
25
|
* @since JDK 1.8
|
26
|
* @since JDK 1.8
|
|
26
|
*/
|
27
|
*/
|
|
27
|
@Configuration
|
28
|
@Configuration
|
|
28
|
public class BeanConfig {
|
29
|
public class BeanConfig {
|
|
29
|
|
30
|
|
|
30
|
@Autowired
|
31
|
@Autowired
|
|
31
|
- private AppConfiguration appConfiguration ;
|
32
|
+ private AppConfiguration appConfiguration;
|
|
32
|
|
33
|
|
|
33
|
@Bean
|
34
|
@Bean
|
|
34
|
- public ZkClient buildZKClient(){
|
35
|
+ public ZkClient buildZKClient() {
|
|
35
|
return new ZkClient(appConfiguration.getZkAddr(), appConfiguration.getZkConnectTimeout());
|
36
|
return new ZkClient(appConfiguration.getZkAddr(), appConfiguration.getZkConnectTimeout());
|
|
36
|
}
|
37
|
}
|
|
37
|
|
38
|
|
|
38
|
@Bean
|
39
|
@Bean
|
|
39
|
- public LoadingCache<String,String> buildCache(){
|
40
|
+ public LoadingCache<String, String> buildCache() {
|
|
40
|
return CacheBuilder.newBuilder()
|
41
|
return CacheBuilder.newBuilder()
|
|
41
|
.build(new CacheLoader<String, String>() {
|
42
|
.build(new CacheLoader<String, String>() {
|
|
42
|
@Override
|
43
|
@Override
|
|
@@ -49,6 +50,7 @@ public class BeanConfig { |
|
@@ -49,6 +50,7 @@ public class BeanConfig { |
|
49
|
|
50
|
|
|
50
|
/**
|
51
|
/**
|
|
51
|
* Redis bean
|
52
|
* Redis bean
|
|
|
|
53
|
+ *
|
|
52
|
* @param factory
|
54
|
* @param factory
|
|
53
|
* @return
|
55
|
* @return
|
|
54
|
*/
|
56
|
*/
|
|
@@ -64,6 +66,7 @@ public class BeanConfig { |
|
@@ -64,6 +66,7 @@ public class BeanConfig { |
|
64
|
|
66
|
|
|
65
|
/**
|
67
|
/**
|
|
66
|
* http client
|
68
|
* http client
|
|
|
|
69
|
+ *
|
|
67
|
* @return okHttp
|
70
|
* @return okHttp
|
|
68
|
*/
|
71
|
*/
|
|
69
|
@Bean
|
72
|
@Bean
|
|
@@ -71,13 +74,26 @@ public class BeanConfig { |
|
@@ -71,13 +74,26 @@ public class BeanConfig { |
|
71
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
74
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
|
72
|
builder.connectTimeout(30, TimeUnit.SECONDS)
|
75
|
builder.connectTimeout(30, TimeUnit.SECONDS)
|
|
73
|
.readTimeout(10, TimeUnit.SECONDS)
|
76
|
.readTimeout(10, TimeUnit.SECONDS)
|
|
74
|
- .writeTimeout(10,TimeUnit.SECONDS)
|
77
|
+ .writeTimeout(10, TimeUnit.SECONDS)
|
|
75
|
.retryOnConnectionFailure(true);
|
78
|
.retryOnConnectionFailure(true);
|
|
76
|
return builder.build();
|
79
|
return builder.build();
|
|
77
|
}
|
80
|
}
|
|
78
|
|
81
|
|
|
79
|
@Bean
|
82
|
@Bean
|
|
80
|
- public RouteHandle buildRouteHandle(){
|
|
|
|
81
|
- return new ConsistentHashHandle() ;
|
83
|
+ public RouteHandle buildRouteHandle() throws Exception {
|
|
|
|
84
|
+ String routeWay = appConfiguration.getRouteWay();
|
|
|
|
85
|
+ RouteHandle routeHandle = (RouteHandle) Class.forName(routeWay).newInstance();
|
|
|
|
86
|
+ if (routeWay.contains("ConsistentHash")) {
|
|
|
|
87
|
+ //一致性 hash 算法
|
|
|
|
88
|
+ Method method = Class.forName(routeWay).getMethod("setHash", AbstractConsistentHash.class);
|
|
|
|
89
|
+ AbstractConsistentHash consistentHash = (AbstractConsistentHash) Class.forName(appConfiguration.getConsistentHashWay()).newInstance();
|
|
|
|
90
|
+ method.invoke(routeHandle,consistentHash) ;
|
|
|
|
91
|
+ return routeHandle ;
|
|
|
|
92
|
+ } else {
|
|
|
|
93
|
+
|
|
|
|
94
|
+ return routeHandle;
|
|
|
|
95
|
+
|
|
|
|
96
|
+ }
|
|
|
|
97
|
+
|
|
82
|
}
|
98
|
}
|
|
83
|
} |
99
|
} |