use of io.lettuce.core.cluster.ClusterTopologyRefreshOptions 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 io.lettuce.core.cluster.ClusterTopologyRefreshOptions in project lettuce-core by lettuce-io.
the class TopologyRefreshIntegrationTests method adaptiveTriggerOnMoveRedirection.
@Test
void adaptiveTriggerOnMoveRedirection() {
ClusterTopologyRefreshOptions topologyRefreshOptions = //
ClusterTopologyRefreshOptions.builder().enableAdaptiveRefreshTrigger(//
ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT).build();
clusterClient.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());
StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
RedisAdvancedClusterAsyncCommands<String, String> clusterConnection = connection.async();
Partitions partitions = connection.getPartitions();
RedisClusterNode node1 = partitions.getPartitionBySlot(0);
RedisClusterNode node2 = partitions.getPartitionBySlot(12000);
List<Integer> slots = node2.getSlots();
slots.addAll(node1.getSlots());
node2.setSlots(slots);
node1.setSlots(Collections.emptyList());
partitions.updateCache();
assertThat(clusterClient.getPartitions().getPartitionByNodeId(node1.getNodeId()).getSlots()).hasSize(0);
assertThat(clusterClient.getPartitions().getPartitionByNodeId(node2.getNodeId()).getSlots()).hasSize(16384);
// slot 3300
connection.reactive().set("b", value).toFuture();
Wait.untilEquals(12000, () -> clusterClient.getPartitions().getPartitionByNodeId(node1.getNodeId()).getSlots().size()).waitOrTimeout();
assertThat(clusterClient.getPartitions().getPartitionByNodeId(node1.getNodeId()).getSlots()).hasSize(12000);
assertThat(clusterClient.getPartitions().getPartitionByNodeId(node2.getNodeId()).getSlots()).hasSize(4384);
clusterConnection.getStatefulConnection().close();
}
use of io.lettuce.core.cluster.ClusterTopologyRefreshOptions in project lettuce-core by lettuce-io.
the class TopologyRefreshIntegrationTests method runReconnectTest.
private void runReconnectTest(BiFunction<RedisAdvancedClusterAsyncCommands<String, String>, RedisClusterNode, BaseRedisAsyncCommands> function) {
ClusterTopologyRefreshOptions topologyRefreshOptions = //
ClusterTopologyRefreshOptions.builder().refreshTriggersReconnectAttempts(//
0).enableAllAdaptiveRefreshTriggers().build();
clusterClient.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());
RedisAdvancedClusterAsyncCommands<String, String> clusterConnection = clusterClient.connect().async();
RedisClusterNode node = clusterClient.getPartitions().getPartition(0);
BaseRedisAsyncCommands closeable = function.apply(clusterConnection, node);
clusterClient.getPartitions().clear();
closeable.quit();
Wait.untilTrue(() -> {
return !clusterClient.getPartitions().isEmpty();
}).waitOrTimeout();
if (closeable instanceof RedisAdvancedClusterCommands) {
((RedisAdvancedClusterCommands) closeable).getStatefulConnection().close();
}
clusterConnection.getStatefulConnection().close();
}
use of io.lettuce.core.cluster.ClusterTopologyRefreshOptions in project lettuce-core by lettuce-io.
the class TopologyRefreshIntegrationTests method adaptiveTopologyUpdateUsesTimeout.
@Test
void adaptiveTopologyUpdateUsesTimeout() {
ClusterTopologyRefreshOptions topologyRefreshOptions = //
ClusterTopologyRefreshOptions.builder().adaptiveRefreshTriggersTimeout(500, //
TimeUnit.MILLISECONDS).refreshTriggersReconnectAttempts(//
0).enableAllAdaptiveRefreshTriggers().build();
clusterClient.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());
RedisAdvancedClusterAsyncCommands<String, String> clusterConnection = clusterClient.connect().async();
clusterConnection.quit();
Delay.delay(Duration.ofMillis(700));
Wait.untilTrue(() -> {
return !clusterClient.getPartitions().isEmpty();
}).waitOrTimeout();
clusterClient.getPartitions().clear();
clusterConnection.quit();
Wait.untilTrue(() -> {
return !clusterClient.getPartitions().isEmpty();
}).waitOrTimeout();
clusterConnection.getStatefulConnection().close();
}
use of io.lettuce.core.cluster.ClusterTopologyRefreshOptions in project lettuce-core by lettuce-io.
the class TopologyRefreshIntegrationTests method changeTopologyWhileOperations.
@Test
void changeTopologyWhileOperations() {
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enablePeriodicRefresh(//
true).refreshPeriod(1, //
TimeUnit.SECONDS).build();
clusterClient.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());
RedisAdvancedClusterAsyncCommands<String, String> clusterConnection = clusterClient.connect().async();
clusterClient.getPartitions().clear();
Wait.untilTrue(() -> {
return !clusterClient.getPartitions().isEmpty();
}).waitOrTimeout();
clusterConnection.getStatefulConnection().close();
}
Aggregations