如何设置Redis数据库号在Spring Boot中使用

问题描述

在我的应用程序中,我希望所有密钥都转到特定的数据库编号。

我检查了Redis文档,发现Redis默认有0-15个数据库。

假设我要使用数据库3。

如何在java spring-boot应用程序中做到这一点?

解决方法

您可以像这样在 application.properties 文件中进行指定。

spring.redis.database=3

此后,在创建Jedisconnection工厂时,将此数据库设置为jedisconnectionfactory。

    @Autowired
    private RedisProperties redisProperties;


    @Bean
    JedisConnectionFactory jedisConnectionFactory() {

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...