java redis 切换db_Springboot 实现Redis 同数据源动态切换db

默认redis使用的是db 0,而我们自己在配置连接的时候可以设置默认使用db ,如:

72227d19d1ecc1ea87214a92218674cd.png

那么怎么去实现动态 去切换自己想使用的db呢?

LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();

connectionFactory.setDatabase(num);

依赖

org.springframework.boot

spring-boot-starter-data-redis

org.apache.commons

commons-pool2

redis.clients

jedis

3.1.0

yml

redis:

host: 192.168.222.157

port: 7002

# password: xatgood

database: 0

timeout: 6000 # 连接超时时间(毫秒)默认是2000ms

lettuce:

pool:

max-active: 200 # 连接池最大连接数(使用负值表示没有限制)

max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)

max-idle: 100 # 连接池中的最大空闲连接

min-idle: 50 # 连接池中的最小空闲连接

shutdown-timeout: 500

# sentinel: # 哨兵模式

# master: mymaster

# nodes: 192.168.222.155:26379,192.168.222.155:26380,192.168.222.155:26381

# cluster: #集群模式

# nodes:

# - 192.168.222.157:6379

# - 192.168.222.157:6380

# - 192.168.222.157:6381

# - 192.168.222.157:6389

# - 192.168.222.157:6390

# - 192.168.222.157:6391

max-redirects: 3 # 获取失败 最大重定向次数

新建RedisConfig配置类

第一类:

/**

* Redis配置类

*/

@Slf4j

@Configuration

public class RedisConfig {

@Resource

private RedisTemplate redisTemplate;

public RedisTemplate setDataBase(int num) {

LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();

if (connectionFactory != null && num != connectionFactory.getDatabase()) {

connectionFactory.setDatabase(num);

this.redisTemplate.setConnectionFactory(connectionFactory);

connectionFactory.resetConnection();

}

return redisTemplate;

}

/**

* 序列化注入spring容器的RedisTemplate

*

* @return

*/

@Bean

public RedisTemplate getRedisTemplate() {

Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper mapper = new ObjectMapper();

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

serializer.setObjectMapper(mapper);

//使用StringRedisSerializer来序列化和反序列化redis的key值

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setValueSerializer(serializer);

redisTemplate.setHashKeySerializer(new StringRedisSerializer());

redisTemplate.setHashValueSerializer(new StringRedisSerializer());

redisTemplate.afterPropertiesSet();

// redisTemplate.setEnableTransactionSupport(true);

return redisTemplate;

}

}

第二类

packagecom.zdyl.wxapplet.config;importcom.fasterxml.jackson.annotation.JsonAutoDetect;importcom.fasterxml.jackson.annotation.PropertyAccessor;importcom.fasterxml.jackson.databind.ObjectMapper;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.pool2.impl.GenericObjectPoolConfig;importorg.springframework.boot.autoconfigure.data.redis.RedisProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Scope;import org.springframework.data.redis.connection.*;importorg.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;importorg.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;importorg.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;importorg.springframework.data.redis.serializer.StringRedisSerializer;importorg.springframework.util.Assert;importorg.springframework.util.StringUtils;importjavax.annotation.PostConstruct;importjavax.annotation.Resource;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/*** Redis配置类*/@Slf4j

@Configurationpublic classRedisConfig {

@ResourceprivateRedisProperties redisProperties;

@ResourceprivateRedisTemplate redisTemplate;public static Map> redisTemplateMap = new HashMap<>();

@PostConstructpublic void initRedisTemp() throwsException {for (int i = 0; i <= 15; i++) {

redisTemplateMap.put(i, getRedisTemplate(i));

}

}/*** 获取redisTemplate实例

*

*@paramdb

*@return

*/

private RedisTemplate getRedisTemplate(intdb) {final RedisTemplate redisTemplate = new RedisTemplate<>();

LettuceConnectionFactory factory=factory();

factory.setDatabase(db);

redisTemplate.setConnectionFactory(factory);returnserializer(redisTemplate);

}/*** redis单机配置

*

*@return

*/

privateRedisStandaloneConfiguration redisConfiguration() {

RedisStandaloneConfiguration redisStandaloneConfiguration= newRedisStandaloneConfiguration();

redisStandaloneConfiguration.setHostName(redisProperties.getHost());

redisStandaloneConfiguration.setPort(redisProperties.getPort());//设置密码

if (redisProperties.getPassword() != null) {

redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));

}returnredisStandaloneConfiguration;

}/*** redis哨兵配置

*

*@return

*/

privateRedisSentinelConfiguration getSentinelConfiguration() {

RedisProperties.Sentinel sentinel=redisProperties.getSentinel();if (sentinel != null) {

RedisSentinelConfiguration config= newRedisSentinelConfiguration();

config.setMaster(sentinel.getMaster());if (!StringUtils.isEmpty(redisProperties.getPassword())) {

config.setPassword(RedisPassword.of(redisProperties.getPassword()));

}

config.setSentinels(createSentinels(sentinel));returnconfig;

}return null;

}/*** 获取哨兵节点

*

*@paramsentinel

*@return

*/

private ListcreateSentinels(RedisProperties.Sentinel sentinel) {

List nodes = new ArrayList<>();for(String node : sentinel.getNodes()) {

String[] parts= StringUtils.split(node, ":");

Assert.state(parts.length== 2, "redis哨兵地址配置不合法!");

nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));

}returnnodes;

}/*** redis集群配置

*

*@return

*/

privateRedisClusterConfiguration getRedisClusterConfiguration() {

RedisProperties.Cluster cluster=redisProperties.getCluster();if (cluster != null) {

RedisClusterConfiguration config= newRedisClusterConfiguration();

config.setClusterNodes(createCluster(cluster));if (!StringUtils.isEmpty(redisProperties.getPassword())) {

config.setPassword(RedisPassword.of(redisProperties.getPassword()));

}

config.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());returnconfig;

}return null;

}/*** 获取集群节点

*

*@paramcluster

*@return

*/

private ListcreateCluster(RedisProperties.Cluster cluster) {

List nodes = new ArrayList<>();for(String node : cluster.getNodes()) {

String[] parts= StringUtils.split(node, ":");

Assert.state(parts.length== 2, "redis哨兵地址配置不合法!");

nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));

}returnnodes;

}/*** 连接池配置

*

*@return

*/

privateGenericObjectPoolConfig redisPool() {

GenericObjectPoolConfig genericObjectPoolConfig=

newGenericObjectPoolConfig();

genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());

genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());

genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());

genericObjectPoolConfig.setTestOnBorrow(true);

genericObjectPoolConfig.setTestWhileIdle(true);

genericObjectPoolConfig.setTestOnReturn(false);

genericObjectPoolConfig.setMaxWaitMillis(5000);returngenericObjectPoolConfig;

}/*** redis客户端配置

*

*@return

*/

privateLettuceClientConfiguration clientConfiguration() {

LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder=LettucePoolingClientConfiguration.builder();

builder.commandTimeout(redisProperties.getLettuce().getShutdownTimeout());

builder.shutdownTimeout(redisProperties.getLettuce().getShutdownTimeout());

builder.poolConfig(redisPool());

LettuceClientConfiguration lettuceClientConfiguration=builder.build();returnlettuceClientConfiguration;

}/*** redis获取连接工厂

*

*@return

*/@Scope(scopeName= "prototype")privateLettuceConnectionFactory factory() {//根据配置和客户端配置创建连接

LettuceConnectionFactory lettuceConnectionFactory = null;if (redisProperties.getSentinel() == null && redisProperties.getCluster() == null) { //单机模式

lettuceConnectionFactory = newLettuceConnectionFactory(redisConfiguration(), clientConfiguration());

lettuceConnectionFactory.afterPropertiesSet();

}else if (redisProperties.getCluster() == null) { //哨兵模式

lettuceConnectionFactory = newLettuceConnectionFactory(getSentinelConfiguration(), clientConfiguration());

lettuceConnectionFactory.afterPropertiesSet();

}else { //集群模式

lettuceConnectionFactory = newLettuceConnectionFactory(getRedisClusterConfiguration(), clientConfiguration());

lettuceConnectionFactory.afterPropertiesSet();

}returnlettuceConnectionFactory;

}/*** 序列化

*

*@paramredisTemplate

*@return

*/

private RedisTemplateserializer(RedisTemplate redisTemplate) {

Jackson2JsonRedisSerializer serializer= new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper mapper= newObjectMapper();

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);//mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

serializer.setObjectMapper(mapper);//使用StringRedisSerializer来序列化和反序列化redis的key值

redisTemplate.setKeySerializer(newStringRedisSerializer());

redisTemplate.setValueSerializer(serializer);

redisTemplate.setHashKeySerializer(newStringRedisSerializer());

redisTemplate.setHashValueSerializer(newStringRedisSerializer());

redisTemplate.afterPropertiesSet();//redisTemplate.setEnableTransactionSupport(true);

returnredisTemplate;

}

}

Redis工具类

/**

* redis工具类

*/

@Slf4j

@Component

public class RedisUtils {

@Resource

private RedisConfig redisConfig;

/**

* 根据db获取对应的redisTemplate实例

*

* @param db

* @return redisTemplate实例

*/

public RedisTemplate getRedisTemplateByDb(final int db) {

return redisConfig.setDataBase(db);

}

/**

* 设置缓存

*

* @param key 缓存key

* @param value 缓存value

*/

public void setString(String key, String value, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

redisTemplate.opsForValue().set(key, value);

log.debug("RedisUtil:set cache key={},value={}", key, value);

}

/**

* 设置缓存,并且自己指定过期时间

*

* @param key

* @param value

* @param expireTime 过期时间

*/

public void setString(String key, String value, int expireTime, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);

log.debug("RedisUtil:set cache key={},value={}", key, value, expireTime, TimeUnit.SECONDS);

}

/**

* 设置缓存对象,可指定DB

*

* @param key 缓存key

* @param obj 缓存value

*/

public void setObject(String key, T obj, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

redisTemplate.opsForValue().set(key, obj);

}

/**

* 新增hashMap值

*

* @param key

* @param hashKey

* @param hashValue

* @param db

* @return void

* @author WangJing

* @date 2019年10月26日 9:22

*/

public void hashPutString(Serializable key, Serializable hashKey, String hashValue, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

operations.put(key, hashKey, hashValue);

}

/**

* 以map集合的形式添加键值对

*

* @param key

* @param maps

* @param db

* @return void

* @author Sunhj

* @date 2019年10月26日 9:56

*/

public void hashPutAll(String key, Map maps, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

operations.putAll(key, maps);

}

/**

* 获取变量中的键值对

* {key3=value3, key1=value1, key5=value5, key4=value4, key2=value2}

*

* @param db

* @param key

* @return java.util.Map

* @author wangj

* @date 2019年10月26日 8:47

*/

public Map hashGetAll(int db, Serializable key) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.opsForHash().entries(key);

}

/**

* 判断key是否存在

*

* @param key

* @param db

* @return java.util.Map

* @author wangj

* @date 2019年10月26日 8:47

*/

public Boolean hashHasKey(Serializable key, Serializable hahsKey, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.opsForHash().hasKey(key, hahsKey);

}

/**

* 获取hash表中存在的所有的键

*

* @param key

* @param db

* @return java.util.List

* @author Sunhj

* @date 2019年10月26日 10:58

*/

public Set hashGetAllHashKeys(Serializable key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.opsForHash().keys(key);

}

/**

* 获取hash表中存在的所有的值

*

* @param key

* @param db

* @return java.util.List

* @author Sunhj

* @date 2019年10月26日 10:58

*/

public List hashGetAllHashValues(Serializable key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.opsForHash().values(key);

}

/**

* 根据key,hashKey

* 获取hash表中存在的单个值

*

* @param key

* @param db

* @return java.util.List

* @author Sunhj

* @date 2019年10月26日 10:58

*/

public Object hashGetObject(Serializable key, Serializable hashKey, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.opsForHash().get(key, hashKey);

}

/**

* 删除一个或者多个hash表字段

*

* @param key

* @param db

* @param fields

* @return java.lang.Long

* @author Sunhj

* @date 2019年10月26日 10:15

*/

public Long hashDelete(Serializable key, int db, Serializable... fields) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

return operations.delete(key, (Object) fields);

}

/**

* 删除一个hash表字段

*

* @param key

* @param db

* @param fields

* @return java.lang.Long

* @author Sunhj

* @date 2019年10月26日 10:15

*/

public boolean hashDeleteOne(Serializable key, String fields, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

return operations.delete(key, fields) == 1;

}

/**

* 设置缓存对象

*

* @param key 缓存key

* @param obj 缓存value

*/

public void setObject(String key, T obj, int expireTime, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

redisTemplate.opsForValue().set(key, obj, expireTime, TimeUnit.SECONDS);

}

/**

* 获取指定key的缓存

*

* @param key---JSON.parseObject(value, User.class);

*/

public Object getObject(String key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.opsForValue().get(key);

}

/**

* 判断当前key值 是否存在

*

* @param key

*/

public boolean hasKey(String key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

return redisTemplate.hasKey(key);

}

/**

* 获取指定key的缓存

*

* @param key

*/

public String getString(String key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

String value = (String) redisTemplate.opsForValue().get(key);

log.debug("RedisUtil:get cache key={},value={}", key, value);

return value;

}

/**

* 删除指定key的缓存

*

* @param key

*/

public void delete(String from, String key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

redisTemplate.delete(key);

log.info("RedisUtil:delete " + from + " cache key={}", key);

}

/**

* @param key

* @throws

* @Title: expire

* @Description: 更新key的失效时间

*/

public Boolean expire(String key, int seconds, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

log.debug("RedisUtil:expire cache key={}", key);

return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);

}

/**

* 移除并获取列表中第一个元素

*

* @param key

* @param db

* @return void

* @author sunhj

* @date 2019年10月26日 14:35

*/

public String listLeftPop(Serializable key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

Object leftPop = redisTemplate.opsForList().leftPop(key);

if (leftPop == null) {

return null;

}

return JSON.toJSONString(leftPop);

}

/**

* 移除并获取列表最后一个元素

*

* @param key

* @param db

* @return java.lang.Object

* @author sunhj

* @date 2019年10月26日 14:40

*/

public String listRightPop(Serializable key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

ListOperations operations = redisTemplate.opsForList();

Object x = operations.rightPop(key);

if (x == null) {

return null;

}

return JSON.toJSONString(x);

}

/**

* 获取变量中的指定map键是否有值,如果存在该map键则获取值,没有则返回null。

*

* @param key

* @param field

* @param db

* @return T

* @author Sunhj

* @date 2019年10月26日 8:41

*/

public T hashGet(Serializable key, Serializable field, Class t, int db) {

try {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

//String x = Objects.requireNonNull(operations.get(key, field)).toString();

//T y = JSON.parseObject(Objects.requireNonNull(operations.get(key, field)).toString(), t);

//T y = JSON.parseObject(x , t);

return JSON.parseObject(Objects.requireNonNull(operations.get(key, field)).toString(), t);

} catch (Exception e) {

return null;

}

}

/**

* 获取变量中的指定map键是否有值,如果存在该map键则获取值(String格式),没有则返回null。

*

* @param key

* @param field

* @param db

* @return T

* @author Sunhj

* @date 2019年10月26日 8:41

*/

public String hashGetString(Serializable key, Serializable field, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

try {

Serializable serializable = operations.get(key, field);

if (serializable != null) {

return serializable.toString();

}

return null;

} catch (Exception e) {

log.error("hashGetString方法异常", e);

return null;

}

}

/**

* 获取变量中的键值对 ??

*

* @param key

* @param db

* @return java.util.Map

* @author Sunhj

* @date 2019年10月26日 8:47

*/

public Map hashGetAll(Serializable key, Class t, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

Map x = operations.entries(key);

Map map = new HashMap<>();

try {

for (Serializable xa : x.keySet()) {

String keyValue = x.get(xa).toString();

map.put(xa.toString(), JSON.parseObject(keyValue, t));

}

return map;

} catch (Exception e) {

return null;

}

}

/**

* 新增hashMap值

*

* @param key

* @param hashKey

* @param hashValue

* @param db

* @return void

* @author Sunhj

* @date 2019年10月26日 9:22

*/

public boolean hashPut(Serializable key, Serializable hashKey, T hashValue, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

operations.put(key, hashKey, JSON.toJSONString(hashValue, SerializerFeature.WriteMapNullValue));

return true;

}

/**

* 查看hash表中指定字段是否存在

*

* @param key

* @param field

* @param db

* @return boolean

* @author Sunhj

* @date 2019年10月26日 10:32

*/

public boolean hashExists(Serializable key, Serializable field, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

HashOperations operations = redisTemplate.opsForHash();

return operations.hasKey(key, field);

}

/**

* 存储在list的头部,即添加一个就把它放在最前面的索引处

*

* @param key

* @param value

* @param db

* @return java.lang.Long

* @author sunhj

* @date 2019年10月26日 14:03

*/

public Long listLeftPush(Serializable key, Serializable value, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

ListOperations operations = redisTemplate.opsForList();

return operations.leftPush(key, value);

}

/**

* 获取所有的KEY

*

* @param key

*/

public List getHashKeys(String key, int db) {

RedisTemplate redisTemplate = getRedisTemplateByDb(db);

List values = redisTemplate.opsForHash().values(key);

return values;

}

}

注意!!!

注意!!!

注意!!!

注意!!!

LettuceConnectionFactory 是 在springboot  2.X版本使用,

但是

springboot 版本 spring-boot-starter-data-redis 的版本对这个redis切换db非常不友好!

亲测 ,使用 springboot 2.1.3.RELEASE   springboot 2.1.4.RELEASE springboot 2.1.5.RELEASE 可以成功切换。

但是从springboot 2.1.6.RELEASE 开始 到springboot 2.2.0.RELEASE 都是有问题的。如果有人找到方法了留言告诉一声,我也学习一下!

ok,最后简单的切换使用演示:

@Slf4j

@RunWith(SpringRunner.class)

@SpringBootTestpublic classShaobingApplicationTests {

@ResourceprivateRedisUtils redisUtils;

@ResourceprivateRedisTemplate redisTemplate;

@Testpublic void testStringRedisTemplate() throwsInterruptedException {

redisUtils.setObject("1","测试1",1);

redisUtils.setObject("2","测试2",2);

redisUtils.setObject("3","测试3",3);

}

}

ac10fecad40dc3bb6e5ef79ac984dd07.png

ok,该篇教程就暂且到此结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值