Redis多数据源配置,支持不同redis服务及不同database库
第一步 设置Redis数据源参数
############################################################
#
# 默认REDIS 配置
#
############################################################
#Redis数据库索引
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.11.11
# Redis服务器连接端口
spring.redis.port=6379
# Redis连接密码
spring.redis.password=111111
# 连接池最大连接数
spring.redis.jedis.pool.max-active=1000
# 连接池中最大空闲连接
spring.redis.jedis.pool.max-idle=200
# 连接池中最小空闲连接
spring.redis.jedis.pool.min-idle=20
#############################################################
#
#第二个Redis服务地址
#
############################################################
#第一个database库
spring.redis.database1=1
#第二个database库
spring.redis.database2=2
# 第二个Redis服务器地址
spring.redis.host2=192.168.22.22
# 第二个Redis服务器端口
spring.redis.port2=6379
# 第二个Redis连接密码
spring.redis.password2=2222
#############################################################
第二步 Redis数据源切换配置
package com.xuexi.common.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis配置类
* @author
* @date
*/
@Configuration
public class RedisConfig {
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.host2}")
private String host2;
@Value("${spring.redis.port2}")
private int port2;
@Value("${spring.redis.password2}")
private String password2;
@Value("${spring.redis.database1}")
private int database1;
@Value("${spring.redis.database2}")
private int database2;
/**
* 自动装配的redis
* @param factory
* @return
*/
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 创建RedisTemplate<String,Object>对象
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
// 配置连接工厂
template.setConnectionFactory(factory);
// 设置序列化方式
template = buildRedisTemplate(template);
return template;
}
/**
* 第二个redis
* @return
*/
@Bean(name = "redisTemplateTwo")
public RedisTemplate<String, Object> redisTemplateTwo() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(redisConnectionFactoryTwo());
template = buildRedisTemplate(template);
return template;
}
/**
* 设置database库
* @return
*/
public RedisConnectionFactory redisConnectionFactoryTwo(){
JedisConnectionFactory jedisConnectionFactory = buildJedisConnectionFactory();
jedisConnectionFactory.setDatabase(database1);
return jedisConnectionFactory;
}
/**
* 第三个redis
* @return
*/
@Bean(name = "redisTemplateThree")
public RedisTemplate<String, Object> redisTemplateThree() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(redisConnectionFactoryThree());
template = buildRedisTemplate(template);
return template;
}
/**
* 设置database库
* @return
*/
public RedisConnectionFactory redisConnectionFactoryThree(){
JedisConnectionFactory jedisConnectionFactory = buildJedisConnectionFactory();
jedisConnectionFactory.setDatabase(database2);
return jedisConnectionFactory;
}
/**
* 设置第redis库连接参数
* @return
*/
public JedisConnectionFactory buildJedisConnectionFactory() {
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setNumTestsPerEvictionRun(10);
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(60000);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
jedisConnectionFactory.setHostName(host2);
if(!password2.isEmpty()){
jedisConnectionFactory.setPassword(password2);
}
jedisConnectionFactory.setPort(port2);
return jedisConnectionFactory;
}
/**
* 设置序列化方式
* @param template
* @return
*/
public RedisTemplate<String, Object> buildRedisTemplate(RedisTemplate<String, Object> template) {
// 定义jackson2JsonRedisSerializer序列化对象
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会报异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// redis key 序列化方式使用StringSerial
template.setKeySerializer(stringRedisSerializer);
// redis hash key 序列化方式使用StringSerial
template.setHashKeySerializer(stringRedisSerializer);
// redis value 序列化方式使用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// redis hash value 序列化方式使用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
第三步 Redis数据源切换测试
package com.xuexi.common.redis;
import com.xuexi.common.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {
@Qualifier("redisTemplate")
@Autowired
RedisTemplate redisTemplateOne;
@Qualifier("redisTemplateTwo")
@Autowired
RedisTemplate redisTemplateTwo;
@Qualifier("redisTemplateThree")
@Autowired
RedisTemplate redisTemplateThree;
@RequestMapping("/setMultRedis")
public JSONResult setMultRedis(){
redisTemplateOne.opsForValue().set("redis1", "redis1");
redisTemplateTwo.opsForValue().set("redis2", "redis2");
redisTemplateThree.opsForValue().set("redis3", "redis3");
return JSONResult.ok();
}
}
版权声明:本文为u010782875原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。