Search in sources :

Example 16 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project Sermant by huaweicloud.

the class RedisConfig method lettuceConnectionFactoryUvPv.

/**
 * 设置重试
 *
 * @return lettuce工厂
 * @throws Exception
 */
@Bean(destroyMethod = "destroy")
public LettuceConnectionFactory lettuceConnectionFactoryUvPv() throws Exception {
    List<String> clusterNodes = redisProperties.getCluster().getNodes();
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
    if (clusterNodes != null && !clusterNodes.isEmpty()) {
        clusterConfiguration.setClusterNodes(setClusterNodes(clusterNodes));
        clusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
        // 判断是否设置了指定参数,没有设置添加默认值
        Integer maxRedirects = redisProperties.getCluster().getMaxRedirects();
        clusterConfiguration.setMaxRedirects(maxRedirects == null ? MAX_REDIRECTS_DEFAULT : maxRedirects);
        int maxIdle = redisProperties.getLettuce().getPool().getMaxIdle();
        poolConfig.setMaxIdle(maxIdle != 0 ? maxIdle : MAX_IDLE_DEFAULT);
        int minIdle = redisProperties.getLettuce().getPool().getMinIdle();
        poolConfig.setMinIdle(minIdle != 0 ? minIdle : MIN_IDLE_DEFAULT);
        int maxTotal = redisProperties.getLettuce().getPool().getMaxActive();
        poolConfig.setMaxTotal(maxTotal != 0 ? maxTotal : MAX_TOTAL_DEFAULT);
        return new LettuceConnectionFactory(clusterConfiguration, getLettuceClientConfiguration(poolConfig));
    } else {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setPassword(redisProperties.getPassword());
        redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
        redisStandaloneConfiguration.setPort(redisProperties.getPort());
        redisStandaloneConfiguration.setHostName(redisProperties.getHost());
        return new LettuceConnectionFactory(redisStandaloneConfiguration, getLettuceClientConfiguration(poolConfig));
    }
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) Bean(org.springframework.context.annotation.Bean)

Example 17 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project jag-file-submission by bcgov.

the class CacheConfiguration method jedisConnectionFactory.

/**
 * Configure the JedisConnectionFactory
 * @param properties The redis properties
 * @return a JedisConnectionFactory
 */
@Bean
public JedisConnectionFactory jedisConnectionFactory(RedisProperties properties) {
    if (properties.getCluster() != null) {
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(properties.getCluster().getNodes());
        redisClusterConfiguration.setPassword(properties.getPassword());
        if (properties.getCluster().getMaxRedirects() != null)
            redisClusterConfiguration.setMaxRedirects(properties.getCluster().getMaxRedirects());
        return new JedisConnectionFactory(redisClusterConfiguration);
    }
    if (properties.getSentinel() != null) {
        RedisSentinelConfiguration redisSantinelConfiguration = new RedisSentinelConfiguration();
        redisSantinelConfiguration.setMaster(properties.getSentinel().getMaster());
        redisSantinelConfiguration.setSentinels(createSentinels(properties.getSentinel()));
        redisSantinelConfiguration.setPassword(properties.getPassword());
        return new JedisConnectionFactory(redisSantinelConfiguration);
    }
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(properties.getHost());
    redisStandaloneConfiguration.setPort(properties.getPort());
    redisStandaloneConfiguration.setPassword(properties.getPassword());
    return new JedisConnectionFactory(redisStandaloneConfiguration);
}
Also used : RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) Bean(org.springframework.context.annotation.Bean)

Example 18 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project formplayer by dimagi.

the class WebAppContext method jedisConnFactory.

@Bean
public JedisConnectionFactory jedisConnFactory() {
    if (redisClusterString != null) {
        List<String> nodeList = Arrays.asList(redisClusterString.split(","));
        RedisClusterConfiguration config = new RedisClusterConfiguration(nodeList);
        config.setPassword(redisPassword);
        return new JedisConnectionFactory(config);
    } else {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHostName);
        config.setPassword(redisPassword);
        return new JedisConnectionFactory(config);
    }
}
Also used : JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) Bean(org.springframework.context.annotation.Bean)

Aggregations

RedisClusterConfiguration (org.springframework.data.redis.connection.RedisClusterConfiguration)18 Bean (org.springframework.context.annotation.Bean)10 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)8 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)7 RedisSentinelConfiguration (org.springframework.data.redis.connection.RedisSentinelConfiguration)6 RedisNode (org.springframework.data.redis.connection.RedisNode)5 JedisConnectionFactory (org.springframework.data.redis.connection.jedis.JedisConnectionFactory)5 HashMap (java.util.HashMap)4 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 MapPropertySource (org.springframework.core.env.MapPropertySource)4 ClientOptions (io.lettuce.core.ClientOptions)3 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)3 ClusterTopologyRefreshOptions (io.lettuce.core.cluster.ClusterTopologyRefreshOptions)3 SocketOptions (io.lettuce.core.SocketOptions)2 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)2 Duration (java.time.Duration)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.jupiter.api.Test)2