- 前言
- 多数据源,多数据库配置
- Yml配置
- 读取对应配置
- 生成对应Bean
- 对应工具类
- 对应工具类
- 测试
官方对应2.0连接Redis需要不停的刷新,重新加载配置,如下代码
public static RedisTemplate多数据源,多数据库配置 Yml配置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; }
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生成对应Beanconfig = 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; } }
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")); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)