use of org.springframework.data.redis.connection.RedisClusterConfiguration in project LinkAgent by shulieTech.
the class Key method create.
LettuceConnectionFactory create() {
RedisServerMatchStrategy matcher = null;
if (biz == null) {
return null;
}
Object standaloneConfiguration = biz.getStandaloneConfiguration();
Object clusterConfiguration = biz.getClusterConfiguration();
Object sentinelConfiguration = biz.getSentinelConfiguration();
if (clusterConfiguration != null) {
matcher = CLUSTER_MODE_MATCHER;
Set<RedisNode> nodes = ((RedisClusterConfiguration) clusterConfiguration).getClusterNodes();
String password = null;
try {
char[] passwdbyte = ((RedisClusterConfiguration) clusterConfiguration).getPassword().get();
password = new String(passwdbyte);
} catch (NoSuchElementException t) {
// ignore
}
List<Key> keys = new ArrayList<Key>();
Iterator<RedisNode> iterator = nodes.iterator();
while (iterator.hasNext()) {
RedisNode node = iterator.next();
keys.add(new Key(node.getHost(), node.getPort(), 0, password));
}
ShadowRedisConfig shadowRedisConfig = matcher.getConfig(keys);
String shadowPassword = shadowRedisConfig.getPassword();
/**
* 创建影子配置
*/
RedisClusterConfiguration shadowRedisClusterConfiguration = new RedisClusterConfiguration();
/**
* 填充密码
*/
if (!StringUtil.isEmpty(shadowPassword)) {
shadowRedisClusterConfiguration.setPassword(shadowPassword);
}
/**
* 填充连接地址
*/
for (String configNode : shadowRedisConfig.getNodes().split(",")) {
shadowRedisClusterConfiguration.addClusterNode(RedisNode.newRedisNode().listeningAt(configNode.split(":")[0], Integer.parseInt(configNode.split(":")[1])).build());
}
/**
* 构建连接
*/
LettuceConnectionFactory shadowConnectionFactory = new LettuceConnectionFactory(shadowRedisClusterConfiguration);
/**
* 初始化连接
*/
shadowConnectionFactory.afterPropertiesSet();
return shadowConnectionFactory;
} else if (sentinelConfiguration != null) {
RedisSentinelConfiguration redisSentinelConfiguration = (RedisSentinelConfiguration) sentinelConfiguration;
String masterName = redisSentinelConfiguration.getMaster().getName();
String password = null;
try {
char[] passwdbyte = redisSentinelConfiguration.getPassword().get();
password = new String(passwdbyte);
} catch (NoSuchElementException e) {
//
}
Integer database = Integer.parseInt(String.valueOf(redisSentinelConfiguration.getDatabase()));
Set<RedisNode> nodes = redisSentinelConfiguration.getSentinels();
} else /**
*standalone肯定不为空 放后面 因为可能为localhost
*/
if (standaloneConfiguration != null && !("localhost".equals(((RedisStandaloneConfiguration) standaloneConfiguration).getHostName()))) {
matcher = SINGLE_MODE_MATCHER;
RedisStandaloneConfiguration configuration = (RedisStandaloneConfiguration) standaloneConfiguration;
String host = configuration.getHostName();
Integer port = configuration.getPort();
Integer db = configuration.getDatabase();
String password = null;
try {
char[] passwdbyte = configuration.getPassword().get();
password = new String(passwdbyte);
} catch (NoSuchElementException e) {
//
}
ShadowRedisConfig shadowRedisConfig = matcher.getConfig(new Key(host, port, db, password));
if (shadowRedisConfig == null) {
return null;
} else {
String[] spilter = shadowRedisConfig.getNodes().split(":");
String shadowHost = spilter[0];
Integer shadowPort = Integer.valueOf(spilter[1]);
Integer shadowDb = shadowRedisConfig.getDatabase();
String shadowPassword = shadowRedisConfig.getPassword();
LettuceConnectionFactory shadowConnectionFactory = new LettuceConnectionFactory();
shadowConnectionFactory.setHostName(shadowHost);
shadowConnectionFactory.setPort(shadowPort);
/**
* 填充密码
*/
if (shadowPassword != null) {
shadowConnectionFactory.setPassword(shadowPassword);
}
/**
* 填充db
*/
if (shadowDb != null) {
shadowConnectionFactory.setDatabase(shadowDb);
} else {
shadowConnectionFactory.setDatabase(biz.getDatabase());
}
/**
* 填充额外属性
*/
extraProperties(biz, shadowConnectionFactory);
// 初始化
shadowConnectionFactory.afterPropertiesSet();
/* DefaultListableBeanFactory defaultListableBeanFactory = PradarSpringUtil.getBeanFactory();*/
return shadowConnectionFactory;
}
}
return null;
}
use of org.springframework.data.redis.connection.RedisClusterConfiguration in project incubator-shenyu by apache.
the class RateLimiterPluginDataHandlerTest method redisRedisClusterConfigurationPropertiesTest.
/**
* redisStandaloneConfiguration property test case.
*/
@Test
public void redisRedisClusterConfigurationPropertiesTest() {
RedisClusterConfiguration configuration = ReflectionTestUtils.invokeMethod(rateLimiterPluginDataHandler, "redisClusterConfiguration", generateRateLimiterConfig("localhost:2181;localhost:2182"));
assertNotNull(configuration);
assertEquals(RedisPassword.of(PASSWORD_TEST_VALUE), configuration.getPassword());
assertEquals(Collections.unmodifiableSet(Sets.newHashSet(generateRedisNode(PORT_TEST_VALUE_1), generateRedisNode(PORT_TEST_VALUE_2))), configuration.getClusterNodes());
}
use of org.springframework.data.redis.connection.RedisClusterConfiguration in project incubator-shenyu by apache.
the class RateLimiterPluginDataHandler method redisClusterConfiguration.
private RedisClusterConfiguration redisClusterConfiguration(final RateLimiterConfig rateLimiterConfig) {
RedisClusterConfiguration config = new RedisClusterConfiguration();
config.setClusterNodes(createRedisNode(rateLimiterConfig.getUrl()));
if (rateLimiterConfig.getPassword() != null) {
config.setPassword(RedisPassword.of(rateLimiterConfig.getPassword()));
}
return config;
}
use of org.springframework.data.redis.connection.RedisClusterConfiguration in project Sermant by huaweicloud.
the class RedisConnCluster method getConnectionFactory.
/**
* 往IOC容器中注册RedisConnectionFactory对象
* <p>
* 主要是针对redis集群的配置,暂时不实现,只实现单机版
*
* @return LettuceConnectionFactory
*/
@Bean(destroyMethod = "destroy")
@Conditional(value = { RedisClusterCondition.class })
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public LettuceConnectionFactory getConnectionFactory() {
// 集群配置
Map<String, Object> source = new HashMap<>();
source.put("spring.redis.cluster.nodes", nodes);
source.put("spring.redis.timeout", timeout);
source.put("spring.redis.database", database);
source.put("spring.redis.password", password);
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
redisClusterConfiguration.setMaxRedirects(redirects);
// 连接池配置
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);
genericObjectPoolConfig.setMaxWaitMillis(MAX_WAIT_MS);
genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(EVICTION_RUNS_MS);
// 开启自适应集群拓扑刷新和周期拓扑刷新
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enableAllAdaptiveRefreshTriggers().adaptiveRefreshTriggersTimeout(Duration.ofSeconds(REFRESH_TRIGGERS_TIMEOUT)).enablePeriodicRefresh(Duration.ofSeconds(PERIODIC_REFRESH)).build();
ClientResources clientResources = DefaultClientResources.builder().dnsResolver(DnsResolvers.JVM_DEFAULT).build();
final SocketOptions socketOptions = SocketOptions.builder().connectTimeout(Duration.ofMillis(timeout)).build();
final ClientOptions clientOptions = ClusterClientOptions.builder().socketOptions(socketOptions).autoReconnect(true).topologyRefreshOptions(clusterTopologyRefreshOptions).build();
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig).clientOptions(clientOptions).clientResources(clientResources).commandTimeout(Duration.ofMillis(timeout)).build();
return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);
}
use of org.springframework.data.redis.connection.RedisClusterConfiguration in project olive by ClareTung.
the class RedisConfig method redisClusterConfig.
/**
* 配置第一个数据源的
*/
@Bean("redisClusterConfig")
@Primary
public RedisClusterConfiguration redisClusterConfig() {
Map<String, Object> source = new HashMap<>(8);
source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));
RedisClusterConfiguration redisClusterConfiguration;
redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
redisClusterConfiguration.setPassword(environment.getProperty("spring.redis.password"));
return redisClusterConfiguration;
}
Aggregations