作者 crossoverJie

:sparkles: Introducing new features.配置自动切换

@@ -12,7 +12,11 @@ import java.util.List; @@ -12,7 +12,11 @@ import java.util.List;
12 * @since JDK 1.8 12 * @since JDK 1.8
13 */ 13 */
14 public class ConsistentHashHandle implements RouteHandle { 14 public class ConsistentHashHandle implements RouteHandle {
15 - private AbstractConsistentHash hash = new SortArrayMapConsistentHash(); 15 + private AbstractConsistentHash hash ;
  16 +
  17 + public void setHash(AbstractConsistentHash hash) {
  18 + this.hash = hash;
  19 + }
16 20
17 @Override 21 @Override
18 public String routeServer(List<String> values, String key) { 22 public String routeServer(List<String> values, String key) {
@@ -25,11 +25,17 @@ public class AppConfiguration { @@ -25,11 +25,17 @@ public class AppConfiguration {
25 25
26 @Value("${app.zk.connect.timeout}") 26 @Value("${app.zk.connect.timeout}")
27 private int zkConnectTimeout; 27 private int zkConnectTimeout;
28 - 28 +
  29 + @Value("${app.route.way}")
  30 + private String routeWay;
  31 +
  32 + @Value("${app.route.way.consitenthash}")
  33 + private String consistentHashWay;
  34 +
29 public int getZkConnectTimeout() { 35 public int getZkConnectTimeout() {
30 return zkConnectTimeout; 36 return zkConnectTimeout;
31 } 37 }
32 - 38 +
33 public int getPort() { 39 public int getPort() {
34 return port; 40 return port;
35 } 41 }
@@ -54,4 +60,19 @@ public class AppConfiguration { @@ -54,4 +60,19 @@ public class AppConfiguration {
54 this.zkAddr = zkAddr; 60 this.zkAddr = zkAddr;
55 } 61 }
56 62
  63 + public String getRouteWay() {
  64 + return routeWay;
  65 + }
  66 +
  67 + public void setRouteWay(String routeWay) {
  68 + this.routeWay = routeWay;
  69 + }
  70 +
  71 + public String getConsistentHashWay() {
  72 + return consistentHashWay;
  73 + }
  74 +
  75 + public void setConsistentHashWay(String consistentHashWay) {
  76 + this.consistentHashWay = consistentHashWay;
  77 + }
57 } 78 }
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 }
@@ -21,7 +21,14 @@ app.zk.connect.timeout=15000 @@ -21,7 +21,14 @@ app.zk.connect.timeout=15000
21 # zk 注册根节点 21 # zk 注册根节点
22 app.zk.root=/route 22 app.zk.root=/route
23 23
  24 +#路由策略,轮询
  25 +#app.route.way=com.crossoverjie.cim.common.route.algorithm.loop.LoopHandle
24 26
  27 +#路由策略,一致性 hash
  28 +app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle
  29 +
  30 +#一致性 hash 算法具体实现
  31 +app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.SortArrayMapConsistentHash
25 32
26 # Redis 配置 33 # Redis 配置
27 spring.redis.host=47.98.194.60 34 spring.redis.host=47.98.194.60