Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2203

Unable to create Redis Sentinel Lettuce more than one connection using Spring boot

$
0
0

So currently the requirement is as such.

  1. Two separate Redis Sentinel servers
  2. one to be used as a database and one as cache

The problem is only with sentinel

I am able to create configuration for both database and cache.But the official RedisSentinelConfiguration class has following fields hardcoded.Hence the cache connection is also pointing to the database connection.

enter image description here

following is my redis config files

@Configurationpublic class RedisConfig {    @Value("${redis.type}")    String redisType;    @Value("${redis.commandTimeOut}")    Duration commandTimeOut;    @Bean(name = "jasyptStringEncryptor")    public StringEncryptor stringEncryptor() {        byte[] decodedBytes = Base64.getUrlDecoder().decode("aGVsbG8=");        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();        encryptor.setPassword(new String(decodedBytes));        encryptor.setPoolSize(1);        encryptor.setProvider(new BouncyCastleProvider());        encryptor.setAlgorithm("PBEWITHSHA256AND256BITAES-CBC-BC");        encryptor.setKeyObtentionIterations(1000);        encryptor.setSaltGenerator(new org.jasypt.salt.RandomSaltGenerator());        return encryptor;    }    protected LettuceConnectionFactory clusterConnectionFactory(final RedisProperties redisProperties) {        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());        redisClusterConfiguration.setPassword(redisProperties.getPassword());        final LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration                .builder().commandTimeout(redisProperties.getTimeout());        if (redisProperties.isSsl()) {            lettuceClientConfigurationBuilder.useSsl();        }        final LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().commandTimeout(commandTimeOut).build();        return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);    }    protected LettuceConnectionFactory standaloneConnectionFactory(final RedisProperties redisProperties) {        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();        if (!DigitalApiUtils.isBlankStr(redisProperties.getPassword())) {            redisStandaloneConfiguration.setPassword(redisProperties.getPassword());        }        redisProperties.setPort(redisProperties.getPort());        redisStandaloneConfiguration.setHostName(redisProperties.getHost());        final LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration                .builder().commandTimeout(redisProperties.getTimeout());        if (redisProperties.isSsl()) {            lettuceClientConfigurationBuilder.useSsl();        }        final LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().commandTimeout(commandTimeOut).build();        return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);    }    protected LettuceConnectionFactory sentinelConnectionFactory(final RedisProperties redisProperties) {        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()                .master(redisProperties.getSentinel().getMaster());        redisProperties.getSentinel().getNodes().forEach(s -> sentinelConfig.sentinel(s, redisProperties.getPort()));        sentinelConfig.setPassword(redisProperties.getPassword());        sentinelConfig.setSentinelPassword(redisProperties.getPassword());        return new LettuceConnectionFactory(sentinelConfig, LettuceClientConfiguration.builder().commandTimeout(commandTimeOut).build());    }    @Bean    @Qualifier("redisConnectionFactory")    @Primary    protected LettuceConnectionFactory redisConnectionFactory(final RedisProperties redisProperties) {        LettuceConnectionFactory connectionFactory = null;        switch (redisType) {            case "STANDALONE":                connectionFactory = standaloneConnectionFactory(redisProperties);                break;            case "SENTINEL":                connectionFactory = sentinelConnectionFactory(redisProperties);                break;            case "CLUSTER":                connectionFactory = clusterConnectionFactory(redisProperties);                break;            default:                connectionFactory = standaloneConnectionFactory(redisProperties);                break;        }        return connectionFactory;    }    @Bean    @Qualifier("stringRedisTemplate")    @Primary    StringRedisTemplate stringRedisTemplate(final RedisProperties redisProperties) {        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory(redisProperties));        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());        stringRedisTemplate.setValueSerializer(new StringRedisSerializer());        return stringRedisTemplate;    }}
@Configurationpublic class RedisCacheConfig {    @Value("${redis.cache.type}")    String redisCacheType;    @Value("${redis.commandTimeOut}")    Duration commandTimeOut;    protected LettuceConnectionFactory clusterConnectionFactory(final RedisCacheProperties redisProperties) {        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());        redisClusterConfiguration.setPassword(redisProperties.getPassword());        final LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration                .builder().commandTimeout(redisProperties.getTimeout());        if (redisProperties.isSsl()) {            lettuceClientConfigurationBuilder.useSsl();        }        final LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().commandTimeout(commandTimeOut).build();        return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);    }    protected LettuceConnectionFactory standaloneConnectionFactory(final RedisCacheProperties redisProperties) {        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();        if(!DigitalApiUtils.isBlankStr(redisProperties.getPassword())){            redisStandaloneConfiguration.setHostName(redisProperties.getHost());        }        redisStandaloneConfiguration.setPassword(redisProperties.getPassword());        redisProperties.setPort(redisProperties.getPort());        final LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration                .builder().commandTimeout(redisProperties.getTimeout());        if (redisProperties.isSsl()) {            lettuceClientConfigurationBuilder.useSsl();        }        final LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().commandTimeout(commandTimeOut).build();        return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);    }    protected LettuceConnectionFactory sentinelConnectionFactory(final RedisCacheProperties redisProperties) {        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()                .master(redisProperties.getSentinel().getMaster());        redisProperties.getSentinel().getNodes().forEach(s -> sentinelConfig.sentinel(s, redisProperties.getPort()));        sentinelConfig.setPassword(redisProperties.getPassword());        sentinelConfig.setSentinelPassword(redisProperties.getPassword());        return new LettuceConnectionFactory(sentinelConfig, LettuceClientConfiguration.builder().commandTimeout(commandTimeOut).build());    }    @Bean    @Qualifier("redisCacheConnectionFactory")    protected LettuceConnectionFactory redisCacheConnectionFactory(final RedisCacheProperties redisProperties) {        LettuceConnectionFactory connectionFactory = null;        switch (redisCacheType) {            case "STANDALONE":                connectionFactory = standaloneConnectionFactory(redisProperties);                break;            case "SENTINEL":                connectionFactory = sentinelConnectionFactory(redisProperties);                break;            case "CLUSTER":                connectionFactory = clusterConnectionFactory(redisProperties);                break;            default:                connectionFactory = standaloneConnectionFactory(redisProperties);                break;        }        return connectionFactory;    }    @Bean    @Qualifier("redisCacheStringTemplate")    StringRedisTemplate redisCacheStringTemplate(final RedisCacheProperties redisProperties) {        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisCacheConnectionFactory(redisProperties));        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());        stringRedisTemplate.setValueSerializer(new StringRedisSerializer());        return stringRedisTemplate;    }    @Bean    @Qualifier("cacheHashOperations")    public HashOperations cacheHashOperations(final RedisCacheProperties redisProperties) {        return redisCacheStringTemplate(redisProperties).opsForHash();    }}

Application properties are

redis.type=SENTINELspring.redis.port=26379spring.redis.sentinel.master=mymasterspring.redis.sentinel.nodes={IP1}spring.redis.password=passwordspring.redis.timeout=PT20.345Sredis.commandTimeOut=3000msspring.cache.port=26379spring.cache.sentinel.master=mymastercachespring.cache.sentinel.nodes={IP2}spring.cache.password=passwordspring.cache.timeout=PT20.345Sredis.cache=3000ms

Viewing all articles
Browse latest Browse all 2203

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>