SpringBoot配置Redis多数据源,或同一数据源切换database

   在编码过程中经常会遇到需要配置多个redis数据源,或者在同一数据源下切换database的场景。

        今天遇到了需要动态切换database的业务需求,找了几个方法,虽然能切换,但是存在线程安全问题。最终我选择了配置多数据源来切换database。也就是重新生成一个 redisTemplate,通过修改JedisConnectionFactory连接信息的方法。这种方法通用与MongoDB的多数据源配置,也是通过配置bean工厂实现。且是线程安全的。

 1.配置文件如下

# redis配置
  redis:
    # Redis数据库索引
    database: 0
    # Redis服务器地址
    host:  
    # Redis服务器连接端口
    port:  
    # Redis服务器连接密码(默认为空)
    password:  
#redis数据源2
  redis2:
    database: 2
    host:  
    port:  
    password:  
2.redis配置类

@Configuration
public class Redis2Configs {
    @Value("${spring.redis2.database}")
    private int database;
    @Value("${spring.redis2.host}")
    private String host;
    @Value("${spring.redis2.password}")
    private String password;
    @Value("${spring.redis2.port}")
    private int port;
 
 
    @Bean(name={"redisTemplate2"})//容器注入名称
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redis2ConnectionFactory());
        redisTemplate.setKeySerializer(keySerializer());
        redisTemplate.setHashKeySerializer(keySerializer());
        redisTemplate.setValueSerializer(valueSerializer());
        redisTemplate.setHashValueSerializer(valueSerializer());
        return redisTemplate;
    }
 
    private RedisSerializer<String> keySerializer() {
        return new StringRedisSerializer();
    }
 
    private RedisSerializer<Object> valueSerializer() {
        return new GenericFastJsonRedisSerializer();
    }
 
    private JedisConnectionFactory redis2ConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(database);
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        JedisClientConfiguration clientConfig = JedisClientConfiguration.builder()
                .build();
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration,clientConfig);
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }
PS:为了简便我没有配置连接池,需要的可以添加配置信息

3.注入需要的数据源进行使用

    @Autowired
    private RedisTemplate redisTemplate;//默认配置
 
    @Resource(name = "redisTemplate2")
    private RedisTemplate redisTemplate2;
Boolean hasKey = redisTemplate2.hasKey("key");
 
Set keys = redisTemplate.opsForHash().keys("key");

参考

SpringBoot配置Redis多数据源,或同一数据源切换database_redis切换database_保利万的博客-CSDN博客

Spring Boot实现Redis多数据源动态切换 | Spring Cloud 32_redis动态切换数据源_gmHappy的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中集成多个Redis数据源需要进行以下步骤: 1. 添加依赖:在pom.xml文件中添加Spring Boot Redis和Jedis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> ``` 2. 配置多个数据源:在application.properties或application.yml文件中配置多个Redis数据源的连接信息。示例配置如下: ```properties # 第一个数据源 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 # 第二个数据源 spring.redis.second.host=127.0.0.1 spring.redis.second.port=6380 spring.redis.second.password= spring.redis.second.database=1 ``` 3. 创建多个RedisTemplate:在配置类中创建多个RedisTemplate,每个Template对应一个Redis数据源。示例代码如下: ```java @Configuration public class RedisConfig { @Bean @Primary public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 设置key和value的序列化器 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } @Bean("secondRedisTemplate") public RedisTemplate<String, Object> secondRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 设置key和value的序列化器 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 4. 使用多个数据源:在需要使用Redis的地方,注入对应的RedisTemplate即可。示例代码如下: ```java @RestController public class RedisController { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired @Qualifier("secondRedisTemplate") private RedisTemplate<String, Object> secondRedisTemplate; @RequestMapping("/set") public String set() { redisTemplate.opsForValue().set("key1", "value1"); secondRedisTemplate.opsForValue().set("key2", "value2"); return "success"; } @RequestMapping("/get") public String get() { String value1 = (String) redisTemplate.opsForValue().get("key1"); String value2 = (String) secondRedisTemplate.opsForValue().get("key2"); return "value1: " + value1 + ", value2: " + value2; } } ``` 以上就是在Spring Boot中集成多个Redis数据源的简要步骤。通过配置不同的数据源和创建对应的RedisTemplate,可以实现对多个Redis实例的访问和操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值