|
|
|
package com.crossoverjie.cim.route.config;
|
|
|
|
|
|
|
|
import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
|
|
|
|
import com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle;
|
|
|
|
import com.crossoverjie.cim.common.route.algorithm.consistenthash.AbstractConsistentHash;
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
|
...
|
...
|
@@ -15,28 +15,29 @@ import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function:
|
|
|
|
*
|
|
|
|
* @author crossoverJie
|
|
|
|
* Date: 2018/12/23 00:25
|
|
|
|
* Date: 2018/12/23 00:25
|
|
|
|
* @since JDK 1.8
|
|
|
|
*/
|
|
|
|
@Configuration
|
|
|
|
public class BeanConfig {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AppConfiguration appConfiguration ;
|
|
|
|
private AppConfiguration appConfiguration;
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public ZkClient buildZKClient(){
|
|
|
|
public ZkClient buildZKClient() {
|
|
|
|
return new ZkClient(appConfiguration.getZkAddr(), appConfiguration.getZkConnectTimeout());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public LoadingCache<String,String> buildCache(){
|
|
|
|
public LoadingCache<String, String> buildCache() {
|
|
|
|
return CacheBuilder.newBuilder()
|
|
|
|
.build(new CacheLoader<String, String>() {
|
|
|
|
@Override
|
|
...
|
...
|
@@ -49,6 +50,7 @@ public class BeanConfig { |
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis bean
|
|
|
|
*
|
|
|
|
* @param factory
|
|
|
|
* @return
|
|
|
|
*/
|
|
...
|
...
|
@@ -64,6 +66,7 @@ public class BeanConfig { |
|
|
|
|
|
|
|
/**
|
|
|
|
* http client
|
|
|
|
*
|
|
|
|
* @return okHttp
|
|
|
|
*/
|
|
|
|
@Bean
|
|
...
|
...
|
@@ -71,13 +74,26 @@ public class BeanConfig { |
|
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
|
|
|
builder.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.readTimeout(10, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(10,TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(10, TimeUnit.SECONDS)
|
|
|
|
.retryOnConnectionFailure(true);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public RouteHandle buildRouteHandle(){
|
|
|
|
return new ConsistentHashHandle() ;
|
|
|
|
public RouteHandle buildRouteHandle() throws Exception {
|
|
|
|
String routeWay = appConfiguration.getRouteWay();
|
|
|
|
RouteHandle routeHandle = (RouteHandle) Class.forName(routeWay).newInstance();
|
|
|
|
if (routeWay.contains("ConsistentHash")) {
|
|
|
|
//一致性 hash 算法
|
|
|
|
Method method = Class.forName(routeWay).getMethod("setHash", AbstractConsistentHash.class);
|
|
|
|
AbstractConsistentHash consistentHash = (AbstractConsistentHash) Class.forName(appConfiguration.getConsistentHashWay()).newInstance();
|
|
|
|
method.invoke(routeHandle,consistentHash) ;
|
|
|
|
return routeHandle ;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return routeHandle;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|