Search in sources :

Example 1 with DefaultClientResources

use of io.lettuce.core.resource.DefaultClientResources in project lcache by long172066912.

the class LettuceConnectionFactory method getLettuceClusterPoolConnection.

/**
 * 获取Lettuce集群连接池
 *
 * @param lettuceClusterConnectSourceConfig
 * @return
 */
public GenericObjectPool getLettuceClusterPoolConnection(LettuceClusterConnectSourceConfig lettuceClusterConnectSourceConfig) {
    // 设置线程
    DefaultClientResources res = this.getDefaultClientResources(lettuceClusterConnectSourceConfig.getHosts().toString(), 0, lettuceClusterConnectSourceConfig.getCommonCacheConfig().getIoThreadPoolSize(), lettuceClusterConnectSourceConfig.getCommonCacheConfig().getComputationThreadPoolSize());
    List<RedisURI> nodeConfigs = new ArrayList<>(lettuceClusterConnectSourceConfig.getNodes().size());
    for (LettuceConnectSourceConfig redisSourceConfig : lettuceClusterConnectSourceConfig.getNodes()) {
        nodeConfigs.add(this.getRedisUri(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getPwd(), redisSourceConfig.getDatabase(), redisSourceConfig.getTimeout()));
    }
    RedisClusterClient client = RedisClusterClient.create(res, nodeConfigs);
    client.setDefaultTimeout(Duration.ofMillis(lettuceClusterConnectSourceConfig.getSoTimeout()));
    client.setOptions(CacheConfigBuildUtils.getClusterClientOptions(lettuceClusterConnectSourceConfig));
    return (GenericObjectPool) this.execute(() -> {
        return ConnectionPoolSupport.createGenericObjectPool(() -> client.connect(), CacheConfigBuildUtils.getJedisPoolConfig(lettuceClusterConnectSourceConfig.getCommonCacheConfig()));
    });
}
Also used : RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources)

Example 2 with DefaultClientResources

use of io.lettuce.core.resource.DefaultClientResources in project lcache by long172066912.

the class LettuceConnectionFactory method getLettuceConnectionByPool.

/**
 * 获取Lettuce连接池
 *
 * @param redisSourceConfig
 * @return
 */
public GenericObjectPool getLettuceConnectionByPool(LettuceConnectSourceConfig redisSourceConfig) {
    // 设置线程
    DefaultClientResources res = this.getDefaultClientResources(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getCommonCacheConfig().getIoThreadPoolSize(), redisSourceConfig.getCommonCacheConfig().getComputationThreadPoolSize());
    RedisClient client = RedisClient.create(res, this.getRedisUri(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getPwd(), redisSourceConfig.getDatabase(), redisSourceConfig.getTimeout()));
    client.setOptions(CacheConfigBuildUtils.getClientOptions(redisSourceConfig));
    client.setDefaultTimeout(Duration.ofMillis(redisSourceConfig.getSoTimeout()));
    return (GenericObjectPool) this.execute(() -> {
        return ConnectionPoolSupport.createGenericObjectPool(() -> client.connect(), CacheConfigBuildUtils.getJedisPoolConfig(redisSourceConfig.getCommonCacheConfig()));
    });
}
Also used : RedisClient(io.lettuce.core.RedisClient) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources)

Example 3 with DefaultClientResources

use of io.lettuce.core.resource.DefaultClientResources in project lettuce-core by lettuce-io.

the class RedisClientIntegrationTests method reuseClientConnectionsShutdownTwoClients.

@Test
void reuseClientConnectionsShutdownTwoClients() throws Exception {
    // given
    DefaultClientResources clientResources = DefaultClientResources.create();
    Map<Class<? extends EventExecutorGroup>, EventExecutorGroup> eventLoopGroups = getExecutors(clientResources);
    RedisClient redisClient1 = newClient(clientResources);
    RedisClient redisClient2 = newClient(clientResources);
    connectAndClose(redisClient1);
    connectAndClose(redisClient2);
    // when
    EventExecutorGroup executor = eventLoopGroups.values().iterator().next();
    redisClient1.shutdown(0, 0, TimeUnit.MILLISECONDS);
    assertThat(executor.isShutdown()).isFalse();
    connectAndClose(redisClient2);
    redisClient2.shutdown(0, 0, TimeUnit.MILLISECONDS);
    // then
    assertThat(eventLoopGroups).isEmpty();
    assertThat(executor.isShutdown()).isTrue();
    assertThat(clientResources.eventExecutorGroup().isShuttingDown()).isFalse();
    // cleanup
    TestFutures.awaitOrTimeout(clientResources.shutdown(0, 0, TimeUnit.MILLISECONDS));
    assertThat(clientResources.eventExecutorGroup().isShuttingDown()).isTrue();
}
Also used : EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultClientResources

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

the class RedisAutoConfigurationTests method testCustomizeClientResources.

@Test
void testCustomizeClientResources() {
    Tracing tracing = mock(Tracing.class);
    this.contextRunner.withBean(ClientResourcesBuilderCustomizer.class, () -> (builder) -> builder.tracing(tracing)).run((context) -> {
        DefaultClientResources clientResources = context.getBean(DefaultClientResources.class);
        assertThat(clientResources.tracing()).isEqualTo(tracing);
    });
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) Arrays(java.util.Arrays) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) LettuceClientConfiguration(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) ContextConsumer(org.springframework.boot.test.context.runner.ContextConsumer) RefreshTrigger(io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Duration(java.time.Duration) ClientOptions(io.lettuce.core.ClientOptions) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) Tracing(io.lettuce.core.tracing.Tracing) EnumSet(java.util.EnumSet) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) RedisNode(org.springframework.data.redis.connection.RedisNode) RedisOperations(org.springframework.data.redis.core.RedisOperations) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Collectors(java.util.stream.Collectors) Pool(org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) AssertableApplicationContext(org.springframework.boot.test.context.assertj.AssertableApplicationContext) LettuceClientConfigurationBuilder(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder) LettucePoolingClientConfiguration(org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) Bean(org.springframework.context.annotation.Bean) StringUtils(org.springframework.util.StringUtils) Mockito.mock(org.mockito.Mockito.mock) Tracing(io.lettuce.core.tracing.Tracing) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultClientResources

use of io.lettuce.core.resource.DefaultClientResources in project hazelcast-simulator by hazelcast.

the class LettuceCluster5Driver method startDriverInstance.

@Override
public void startDriverInstance() throws Exception {
    String workerType = get("WORKER_TYPE");
    if ("javaclient".equals(workerType)) {
        String[] uris = get("URI").split(",");
        DefaultClientResources clientResources = // 
        DefaultClientResources.builder().dnsResolver(// Does not cache DNS lookups
        new DirContextDnsResolver()).build();
        List<RedisURI> nodes = new LinkedList<>();
        for (String uri : uris) {
            nodes.add(RedisURI.create(uri));
        }
        // client = RedisClient.create();
        // 
        // StatefulRedisMasterReplicaConnection<String, String> connection = MasterReplica
        // .connect(client, new Utf8StringCodec(), nodes);
        // connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
        client = RedisClusterClient.create(nodes);
    // client = RedisClient.create(clientResources, get("URI"));
    }
}
Also used : DirContextDnsResolver(io.lettuce.core.resource.DirContextDnsResolver) RedisURI(io.lettuce.core.RedisURI) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) LinkedList(java.util.LinkedList)

Aggregations

DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)13 RedisURI (io.lettuce.core.RedisURI)5 RedisClient (io.lettuce.core.RedisClient)4 LettuceConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig)3 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)3 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 DirContextDnsResolver (io.lettuce.core.resource.DirContextDnsResolver)2 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)2 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)2 ClientOptions (io.lettuce.core.ClientOptions)1 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)1 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)1 ClusterTopologyRefreshOptions (io.lettuce.core.cluster.ClusterTopologyRefreshOptions)1 RefreshTrigger (io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 StatefulRedisClusterPubSubConnection (io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection)1 StatefulRedisPubSubConnection (io.lettuce.core.pubsub.StatefulRedisPubSubConnection)1 DefaultEventLoopGroupProvider (io.lettuce.core.resource.DefaultEventLoopGroupProvider)1 Tracing (io.lettuce.core.tracing.Tracing)1