Search in sources :

Example 1 with ClusterTopologyRefreshOptions

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);
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) HashMap(java.util.HashMap) ClientResources(io.lettuce.core.resource.ClientResources) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) SocketOptions(io.lettuce.core.SocketOptions) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) MapPropertySource(org.springframework.core.env.MapPropertySource) LettuceClientConfiguration(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Scope(org.springframework.context.annotation.Scope) Conditional(org.springframework.context.annotation.Conditional) Bean(org.springframework.context.annotation.Bean)

Example 2 with ClusterTopologyRefreshOptions

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();
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) Test(org.junit.jupiter.api.Test)

Example 3 with ClusterTopologyRefreshOptions

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();
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisAdvancedClusterCommands(io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) BaseRedisAsyncCommands(io.lettuce.core.api.async.BaseRedisAsyncCommands)

Example 4 with ClusterTopologyRefreshOptions

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();
}
Also used : ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) Test(org.junit.jupiter.api.Test)

Example 5 with ClusterTopologyRefreshOptions

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();
}
Also used : ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterTopologyRefreshOptions (io.lettuce.core.cluster.ClusterTopologyRefreshOptions)11 Test (org.junit.jupiter.api.Test)8 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)3 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)2 Partitions (io.lettuce.core.cluster.models.partitions.Partitions)2 ClientOptions (io.lettuce.core.ClientOptions)1 SocketOptions (io.lettuce.core.SocketOptions)1 BaseRedisAsyncCommands (io.lettuce.core.api.async.BaseRedisAsyncCommands)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 RedisAdvancedClusterCommands (io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands)1 ClientResources (io.lettuce.core.resource.ClientResources)1 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)1 HashMap (java.util.HashMap)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 Lettuce (org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce)1 Bean (org.springframework.context.annotation.Bean)1 Conditional (org.springframework.context.annotation.Conditional)1 Scope (org.springframework.context.annotation.Scope)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 RedisClusterConfiguration (org.springframework.data.redis.connection.RedisClusterConfiguration)1