Search in sources :

Example 16 with LettuceConnectionFactory

use of org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory in project spring-cloud-connectors by spring-cloud.

the class RedisConnectionFactoryCreator method create.

@Override
public RedisConnectionFactory create(RedisServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig) {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(serviceInfo.getHost());
    configuration.setPort(serviceInfo.getPort());
    configuration.setPassword(RedisPassword.of(serviceInfo.getPassword()));
    if (hasClass(JEDIS_CLASS_NAME)) {
        JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder();
        RedisJedisClientConfigurer clientConfigurer = new RedisJedisClientConfigurer();
        if (serviceConnectorConfig instanceof RedisConnectionFactoryConfig) {
            clientConfigurer.configure(builder, (RedisConnectionFactoryConfig) serviceConnectorConfig);
        } else {
            clientConfigurer.configure(builder, (PooledServiceConnectorConfig) serviceConnectorConfig);
        }
        if (connectionIsSecure(serviceInfo)) {
            builder.useSsl();
        }
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration, builder.build());
        connectionFactory.afterPropertiesSet();
        return connectionFactory;
    } else if (hasClass(LETTUCE_CLASS_NAME)) {
        LettuceClientConfigurationBuilder builder;
        if (serviceConnectorConfig != null && ((PooledServiceConnectorConfig) serviceConnectorConfig).getPoolConfig() != null) {
            if (!hasClass(APACHE_COMMONS_POOL_CLASS_NAME)) {
                throw new ServiceConnectorCreationException(String.format("Failed to create cloud Redis " + "connection factory for %s service. Apache Commons Pool must be available on " + "the classpath if pooling parameters are provided.", serviceInfo.getId()));
            }
            builder = LettucePoolingClientConfiguration.builder();
        } else {
            builder = LettuceClientConfiguration.builder();
        }
        if (connectionIsSecure(serviceInfo)) {
            builder.useSsl();
        }
        RedisLettuceClientConfigurer clientConfigurer = new RedisLettuceClientConfigurer();
        if (serviceConnectorConfig instanceof RedisConnectionFactoryConfig) {
            clientConfigurer.configure(builder, (RedisConnectionFactoryConfig) serviceConnectorConfig);
        } else {
            clientConfigurer.configure(builder, (PooledServiceConnectorConfig) serviceConnectorConfig);
        }
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
        connectionFactory.afterPropertiesSet();
        return connectionFactory;
    } else {
        throw new ServiceConnectorCreationException(String.format("Failed to create cloud Redis " + "connection factory for %s service. No client implementation classes " + " of Jedis or Lettuce clients implementation (%s, %s) not found", serviceInfo.getId(), JEDIS_CLASS_NAME, LETTUCE_CLASS_NAME));
    }
}
Also used : JedisClientConfigurationBuilder(org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder) ServiceConnectorCreationException(org.springframework.cloud.service.ServiceConnectorCreationException) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) PooledServiceConnectorConfig(org.springframework.cloud.service.PooledServiceConnectorConfig) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) LettuceClientConfigurationBuilder(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder)

Example 17 with LettuceConnectionFactory

use of org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory in project spring-cloud-connectors by spring-cloud.

the class SpringData1RedisConnectionFactoryCreator method create.

@Override
public RedisConnectionFactory create(RedisServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig) {
    if (hasClass(JEDIS_CLASS_NAME)) {
        SpringData1RedisConnectionFactoryConfigurer configurer = new SpringData1RedisConnectionFactoryConfigurer();
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setHostName(serviceInfo.getHost());
        connectionFactory.setPort(serviceInfo.getPort());
        connectionFactory.setPassword(serviceInfo.getPassword());
        if (serviceConnectorConfig instanceof RedisConnectionFactoryConfig) {
            configurer.configure(connectionFactory, (RedisConnectionFactoryConfig) serviceConnectorConfig);
        } else {
            configurer.configure(connectionFactory, (PooledServiceConnectorConfig) serviceConnectorConfig);
        }
        connectionFactory.afterPropertiesSet();
        return connectionFactory;
    } else if (hasClass(LETTUCE_CLASS_NAME)) {
        SpringData1RedisLettuceConnectionFactoryConfigurer configurer = new SpringData1RedisLettuceConnectionFactoryConfigurer();
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
        connectionFactory.setHostName(serviceInfo.getHost());
        connectionFactory.setPort(serviceInfo.getPort());
        connectionFactory.setPassword(serviceInfo.getPassword());
        configurer.configure(connectionFactory, (RedisConnectionFactoryConfig) serviceConnectorConfig);
        connectionFactory.afterPropertiesSet();
        return connectionFactory;
    } else {
        throw new ServiceConnectorCreationException(String.format("Failed to create cloud Redis connection factory " + "for %s service. No client implementation classes " + " of jedis or lettuce clients implementation (%s, %s) not found", serviceInfo.getId(), JEDIS_CLASS_NAME, LETTUCE_CLASS_NAME));
    }
}
Also used : ServiceConnectorCreationException(org.springframework.cloud.service.ServiceConnectorCreationException) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)

Example 18 with LettuceConnectionFactory

use of org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory in project spring-boot by spring-projects.

the class RedisAutoConfigurationTests method testRedisConfigurationDisabledPool.

@Test
void testRedisConfigurationDisabledPool() {
    this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:false").run((context) -> {
        LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
        assertThat(cf.getHostName()).isEqualTo("foo");
        assertThat(cf.getClientConfiguration()).isNotInstanceOf(LettucePoolingClientConfiguration.class);
    });
}
Also used : LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 19 with LettuceConnectionFactory

use of org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory in project spring-boot by spring-projects.

the class RedisAutoConfigurationTests method testPasswordInUrlStartsWithColon.

@Test
void testPasswordInUrlStartsWithColon() {
    this.contextRunner.withPropertyValues("spring.redis.url:redis://user::pass:word@example:33").run((context) -> {
        LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
        assertThat(cf.getHostName()).isEqualTo("example");
        assertThat(cf.getPort()).isEqualTo(33);
        assertThat(getUserName(cf)).isEqualTo("user");
        assertThat(cf.getPassword()).isEqualTo(":pass:word");
    });
}
Also used : LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 20 with LettuceConnectionFactory

use of org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory in project spring-boot by spring-projects.

the class RedisAutoConfigurationTests method testRedisConfigurationWithDefaultTimeouts.

@Test
void testRedisConfigurationWithDefaultTimeouts() {
    this.contextRunner.withPropertyValues("spring.redis.host:foo").run((context) -> {
        LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
        assertThat(cf.getHostName()).isEqualTo("foo");
        assertThat(cf.getTimeout()).isEqualTo(60000);
        assertThat(cf.getClientConfiguration().getClientOptions().get().getSocketOptions().getConnectTimeout().toMillis()).isEqualTo(10000);
    });
}
Also used : LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Test(org.junit.jupiter.api.Test)

Aggregations

LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)27 Test (org.junit.jupiter.api.Test)16 JedisConnectionFactory (org.springframework.data.redis.connection.jedis.JedisConnectionFactory)5 Bean (org.springframework.context.annotation.Bean)4 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)3 Test (org.junit.Test)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 ServiceConnectorCreationException (org.springframework.cloud.service.ServiceConnectorCreationException)2 LettuceClientConfiguration (org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration)2 LettuceClientConfigurationBuilder (org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder)2 LoadingCacheTest (com.alicp.jetcache.LoadingCacheTest)1 RefreshCacheTest (com.alicp.jetcache.RefreshCacheTest)1 AbstractExternalCacheTest (com.alicp.jetcache.test.external.AbstractExternalCacheTest)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 ConfigurationPropertyFactoryBean (org.hisp.dhis.external.conf.ConfigurationPropertyFactoryBean)1 Pool (org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool)1 Cloud (org.springframework.cloud.Cloud)1 CloudFactory (org.springframework.cloud.CloudFactory)1 PooledServiceConnectorConfig (org.springframework.cloud.service.PooledServiceConnectorConfig)1 Profile (org.springframework.context.annotation.Profile)1