SpringBoot 2.6.X Redis多数据源,多数据库连接

SpringBoot 2.6.X Redis多数据源,多数据库连接,第1张

SpringBoot 2.6.X Redis多数据源,多数据库连接

文章目录
  • 前言
  • 多数据源,多数据库配置
      • Yml配置
      • 读取对应配置
      • 生成对应Bean
      • 对应工具类
      • 对应工具类
      • 测试

前言

官方对应2.0连接Redis需要不停的刷新,重新加载配置,如下代码

    
    public static RedisTemplate changeRedisDatabase(RedisTemplate redisTemplate, int database) {
        LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory)redisTemplate.getConnectionFactory();
        if (lettuceConnectionFactory == null) {
            LOGGER.error("Redis连接工厂为空,未能切换数据库");
            return redisTemplate;
        }
        lettuceConnectionFactory.setDatabase(database);
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        lettuceConnectionFactory.resetConnection();
        lettuceConnectionFactory.afterPropertiesSet();
        return redisTemplate;
    }
多数据源,多数据库配置 Yml配置
redis:
  config:
    - key: redis-0
      hostName: 172.16.54.209
      port: 6379
      database:
        - 0
    - key: redis-1
      hostName: 172.16.54.209
      port: 6380
      database:
        - 0
        - 1
        - 2
        - 3
        - 4
读取对应配置
package com.lihan.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Data
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfigProperties {

    private List config = new ArrayList<>();

    @Data
    public static class RedisProperties {
        
        private String key;
        
        private String hostName;

        
        private Integer port;

        
        private Integer[] database;

        
        private String username;

        
        private String password;
    }
}

生成对应Bean
package com.lihan.config;

import com.lihan.properties.RedisConfigProperties;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class RedisConfigUp {

    
    @Bean
    public Map>> redisTemplateDatabase(RedisConfigProperties redisConfigProperties) {
        Map>> redisTemplateDatabase = new HashMap<>();
        List redisProperties = redisConfigProperties.getConfig();
        for (RedisConfigProperties.RedisProperties redisPropertiesTemp : redisProperties) {

            Map> redisTemplateMap = new HashMap<>();

            for (Integer database : redisPropertiesTemp.getDatabase()) {
                // 1. 配置连接
                RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
                redisStandaloneConfiguration.setHostName(redisPropertiesTemp.getHostName());
                redisStandaloneConfiguration.setPort(redisPropertiesTemp.getPort());
                if (redisPropertiesTemp.getUsername() != null && redisPropertiesTemp.getUsername().length() > 0) {
                    redisStandaloneConfiguration.setUsername(redisPropertiesTemp.getUsername());
                }
                if (redisPropertiesTemp.getPassword() != null && redisPropertiesTemp.getPassword().length() > 0) {
                    redisStandaloneConfiguration.setPassword(redisPropertiesTemp.getPassword());
                }
                // 2.配置数据库
                redisStandaloneConfiguration.setDatabase(database);

                // 3.设置连接池
                GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig<>();
                LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig).build();
                LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);
                lettuceConnectionFactory.afterPropertiesSet();
                RedisTemplate template = new RedisTemplate<>();
                template.setConnectionFactory(lettuceConnectionFactory);
                // key采用String的序列化方式
                template.setKeySerializer(new StringRedisSerializer());
                // value序列化方式采用jdk
                template.setValueSerializer(new StringRedisSerializer());
                template.afterPropertiesSet();
                // Key 数据库名,value redis *** 作类
                redisTemplateMap.put(String.valueOf(database), template);
            }
            redisTemplateDatabase.put(redisPropertiesTemp.getKey(), redisTemplateMap);
        }
        return redisTemplateDatabase;
    }
}

对应工具
package com.lihan.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RedisUtils {

    private static Map>> redisTemplateDatabase;

    
    public static void set(String redisKey, String database, String key, Object Value) {
        Map> stringRedisTemplateMap = redisTemplateDatabase.get(redisKey);
        stringRedisTemplateMap.get(database).opsForValue().set(key, Value);
    }

    
    public static Object get(String redisKey, String database, String key) {
        Map> stringRedisTemplateMap = redisTemplateDatabase.get(redisKey);
        return stringRedisTemplateMap.get(database).opsForValue().get(key);
    }

    public RedisUtils(Map>> redisTemplateDatabase) {
        RedisUtils.redisTemplateDatabase = redisTemplateDatabase;
    }
}
对应工具类
package com.lihan.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RedisUtils {

    private static Map>> redisTemplateDatabase;

    
    public static void set(String redisKey, String database, String key, Object Value) {
        Map> stringRedisTemplateMap = redisTemplateDatabase.get(redisKey);
        stringRedisTemplateMap.get(database).opsForValue().set(key, Value);
    }

    
    public static Object get(String redisKey, String database, String key) {
        Map> stringRedisTemplateMap = redisTemplateDatabase.get(redisKey);
        return stringRedisTemplateMap.get(database).opsForValue().get(key);
    }

    public RedisUtils(Map>> redisTemplateDatabase) {
        RedisUtils.redisTemplateDatabase = redisTemplateDatabase;
    }
}
测试
    @Test
    public void test02() {
        RedisUtils.set("redis-1", "0", "test-0", "test-0");
        RedisUtils.set("redis-1", "1", "test-1", "test-1");
        RedisUtils.set("redis-1", "2", "test-2", "test-2");
        RedisUtils.set("redis-1", "3", "test-3", "test-3");
        RedisUtils.set("redis-1", "4", "test-4", "test-4");

    }

    @Test
    public void test03() {
        System.out.println(RedisUtils.get("redis-1", "0", "test-0"));
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5672159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存