简体   繁体   中英

How to set database number for redis, when using redis sentinel

I have a redis cluster of 1 master, 1 slave and 3 redis sentinel and springboot application connecting to redis through sentinel.

Application is able to talk to redis through redis sentinel. as we know by default redis instance has 16 databases numbered from 0 - 15, by default connections are made to db0, but in my project i need to connect to db4, because there are other projects which are using 0, 1, 2 etc db4 is assigned to my project.

Jedisconnection factory is being used in the project and i tried setting db from redis properties have a look at the code below

spring.redis.sentinel.master=mymaster
spring.redis.password=${REDIS_PASSWORD}
spring.redis.sentinel.nodes=localhost:26379,localhost:26380,localhost:26381
spring.redis.database=4
public class RedisConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        redisSentinelConfiguration.setDatabase(redisProperties.getDatabase());
        redisSentinelConfiguration.master(redisProperties.getSentinel().getMaster());
        redisSentinelConfiguration.setPassword(redisProperties.getPassword());
        for(String node : redisProperties.getSentinel().getNodes()) {
            String[] props = node.split(":");
            redisSentinelConfiguration.sentinel(props[0], Integer.parseInt(props[1]));
        }
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration);
        jedisConnectionFactory.setDatabase(redisProperties.getDatabase());
        return jedisConnectionFactory;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(jedisConnectionFactory());
        return stringRedisTemplate;
    }
}

Even after doing this, the connection getting created here is using db0.

Moreover the setDatabase() method of jedisConnectionFactory is showing deprecated.

Please help me with the correct way of doing this, if my approach is right then when am i making mistake.

Note: Without redis sentinel configuration i could achieve connection to specific db4 by setting the database in jedisConeectionFactory.

I could solve this issue, by having spring properties:

spring.redis.sentinel.master=mymaster
spring.redis.password=${REDIS_PASSWORD}
spring.redis.sentinel.nodes=localhost:26379,localhost:26380,localhost:26381
spring.redis.database=4

and below config

public class RedisConfig {
    
    @Autowired
    private RedisProperties redisProperties;

    public RedisConfig() {
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        redisSentinelConfiguration.master(this.redisProperties.getSentinel().getMaster());
        redisSentinelConfiguration.setPassword(this.redisProperties.getPassword());
        Iterator var2 = this.redisProperties.getSentinel().getNodes().iterator();

        while(var2.hasNext()) {
            String node = (String)var2.next();
            String[] props = node.split(":");
            redisSentinelConfiguration.sentinel(props[0], Integer.parseInt(props[1]));
        }

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration);
        jedisConnectionFactory.setDatabase(this.redisProperties.getDatabase());
        return jedisConnectionFactory;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(this.jedisConnectionFactory());
        return stringRedisTemplate;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM