Search in sources :

Example 1 with ClientOptions

use of io.lettuce.core.ClientOptions in project spring-boot by spring-projects.

the class LettuceConnectionConfiguration method createClientOptions.

private ClientOptions createClientOptions() {
    ClientOptions.Builder builder = initializeClientOptionsBuilder();
    Duration connectTimeout = getProperties().getConnectTimeout();
    if (connectTimeout != null) {
        builder.socketOptions(SocketOptions.builder().connectTimeout(connectTimeout).build());
    }
    return builder.timeoutOptions(TimeoutOptions.enabled()).build();
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) Duration(java.time.Duration)

Example 2 with ClientOptions

use of io.lettuce.core.ClientOptions in project pay-publicapi by alphagov.

the class PublicApiModule method getRedisClient.

@Provides
@Singleton
public RedisClient getRedisClient() {
    RedisClient client = RedisClient.create(configuration.getRedisConfiguration().getUrl());
    SocketOptions socketOptions = SocketOptions.builder().connectTimeout(Duration.ofMillis(configuration.getRedisConfiguration().getConnectTimeout())).build();
    ClientOptions clientOptions = ClientOptions.builder().socketOptions(socketOptions).build();
    client.setOptions(clientOptions);
    client.setDefaultTimeout(Duration.ofMillis(configuration.getRedisConfiguration().getCommandTimeout()));
    return client;
}
Also used : RedisClient(io.lettuce.core.RedisClient) ClientOptions(io.lettuce.core.ClientOptions) SocketOptions(io.lettuce.core.SocketOptions) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 3 with ClientOptions

use of io.lettuce.core.ClientOptions 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 4 with ClientOptions

use of io.lettuce.core.ClientOptions in project lettuce-core by lettuce-io.

the class ClusterClientOptionsUnitTests method builderFromDefaultClientOptions.

@Test
void builderFromDefaultClientOptions() {
    ClientOptions clientOptions = ClientOptions.builder().build();
    ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder(clientOptions).build();
    assertThat(clusterClientOptions.getProtocolVersion()).isEqualTo(clusterClientOptions.getProtocolVersion());
    assertThat(clusterClientOptions.getDisconnectedBehavior()).isEqualTo(clusterClientOptions.getDisconnectedBehavior());
    assertThat(clusterClientOptions.getSslOptions()).isEqualTo(clusterClientOptions.getSslOptions());
    assertThat(clusterClientOptions.getTimeoutOptions()).isEqualTo(clusterClientOptions.getTimeoutOptions());
    assertThat(clusterClientOptions.getRequestQueueSize()).isEqualTo(clusterClientOptions.getRequestQueueSize());
    assertThat(clusterClientOptions.isAutoReconnect()).isEqualTo(clusterClientOptions.isAutoReconnect());
    assertThat(clusterClientOptions.isCloseStaleConnections()).isEqualTo(clusterClientOptions.isCloseStaleConnections());
    assertThat(clusterClientOptions.isCancelCommandsOnReconnectFailure()).isEqualTo(clusterClientOptions.isCancelCommandsOnReconnectFailure());
    assertThat(clusterClientOptions.isPublishOnScheduler()).isEqualTo(clusterClientOptions.isPublishOnScheduler());
    assertThat(clusterClientOptions.isSuspendReconnectOnProtocolFailure()).isEqualTo(clusterClientOptions.isSuspendReconnectOnProtocolFailure());
    assertThat(clusterClientOptions.getScriptCharset()).isEqualTo(clusterClientOptions.getScriptCharset());
    assertThat(clusterClientOptions.mutate()).isNotNull();
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) Test(org.junit.jupiter.api.Test)

Example 5 with ClientOptions

use of io.lettuce.core.ClientOptions in project pulsar by apache.

the class RedisSession method create.

public static RedisSession create(RedisSinkConfig config) {
    RedisSession redisSession;
    final RedisCodec<byte[], byte[]> codec = new ByteArrayCodec();
    final SocketOptions socketOptions = SocketOptions.builder().tcpNoDelay(config.isTcpNoDelay()).connectTimeout(Duration.ofMillis(config.getConnectTimeout())).keepAlive(config.isKeepAlive()).build();
    final ClientMode clientMode;
    try {
        clientMode = ClientMode.valueOf(config.getClientMode().toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Illegal Redis client mode, valid values are: " + Arrays.asList(ClientMode.values()));
    }
    List<RedisURI> redisURIs = redisURIs(config.getHostAndPorts(), config);
    if (clientMode == ClientMode.STANDALONE) {
        ClientOptions.Builder clientOptions = ClientOptions.builder().socketOptions(socketOptions).requestQueueSize(config.getRequestQueue()).autoReconnect(config.isAutoReconnect());
        final RedisClient client = RedisClient.create(redisURIs.get(0));
        client.setOptions(clientOptions.build());
        final StatefulRedisConnection<byte[], byte[]> connection = client.connect(codec);
        redisSession = new RedisSession(client, connection, connection.async());
    } else if (clientMode == ClientMode.CLUSTER) {
        ClusterClientOptions.Builder clientOptions = ClusterClientOptions.builder().requestQueueSize(config.getRequestQueue()).autoReconnect(config.isAutoReconnect());
        final RedisClusterClient client = RedisClusterClient.create(redisURIs);
        client.setOptions(clientOptions.build());
        final StatefulRedisClusterConnection<byte[], byte[]> connection = client.connect(codec);
        redisSession = new RedisSession(client, connection, connection.async());
    } else {
        throw new UnsupportedOperationException(String.format("%s is not supported", config.getClientMode()));
    }
    return redisSession;
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) RedisURI(io.lettuce.core.RedisURI) SocketOptions(io.lettuce.core.SocketOptions) ClientMode(org.apache.pulsar.io.redis.RedisAbstractConfig.ClientMode) ByteArrayCodec(io.lettuce.core.codec.ByteArrayCodec) RedisClient(io.lettuce.core.RedisClient) AbstractRedisClient(io.lettuce.core.AbstractRedisClient) StatefulRedisClusterConnection(io.lettuce.core.cluster.api.StatefulRedisClusterConnection) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient)

Aggregations

ClientOptions (io.lettuce.core.ClientOptions)8 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)4 SocketOptions (io.lettuce.core.SocketOptions)3 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 RedisClient (io.lettuce.core.RedisClient)2 Duration (java.time.Duration)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Provides (com.google.inject.Provides)1 Singleton (com.google.inject.Singleton)1 AbstractRedisClient (io.lettuce.core.AbstractRedisClient)1 RedisURI (io.lettuce.core.RedisURI)1 ClusterTopologyRefreshOptions (io.lettuce.core.cluster.ClusterTopologyRefreshOptions)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)1 StringCodec (io.lettuce.core.codec.StringCodec)1 ClientResources (io.lettuce.core.resource.ClientResources)1 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)1