Spring boot配置多个Redis数据源操作实例
0 前言平时都是使用本地环境的单Redis情况比较多,在集群环境的情况下连接多个Redis数据库是很正常的情况。最近小强遇到了一个问题,在SpringBoot项目中整合了两个Redis的操...
·
0 前言
平时都是使用本地环境的单Redis情况比较多,在集群环境的情况下连接多个Redis数据库是很正常的情况。
最近小强遇到了一个问题,在SpringBoot项目中整合了两个Redis的操作实例,今天的数据产生后,需要完成离线数据和实时数据的隔离,用两个redis去分别保存昨天和今天的数据作为离线数据和实时数据。
1 环境
基于Maven3.0搭建
spring1.5.9.RELEASE
JDK1.8
2 添加依赖
使用的springboot提供的spring-boot-starter-data-redis工具包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3 application.yml配置文件
spring:
redis:
database: 6 # Redis数据库索引(默认为0)
host: redis.lilian.com # Redis服务器地址
port: 7481 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 0 # 连接超时时间(毫秒)
pool:
max-active: -1 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
redis2:
database: 6 # Redis数据库索引(默认为0)
host: redis.lilian.com # Redis服务器地址
port: 7480 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 0 # 连接超时时间(毫秒)
4 RedisConfig类
import com.xiaoqiang
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.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
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;
import java.lang.reflect.Method;
/**
* spring-boot-data-packing 设置Redis多实例的基类
*/
@EnableCaching
@Configuration
public class RedisConfig {
@Value("${spring.redis.pool.max-active}")
private int redisPoolMaxActive;
@Value("${spring.redis.pool.max-wait}")
private int redisPoolMaxWait;
@Value("${spring.redis.pool.max-idle}")
private int redisPoolMaxIdle;
@Value("${spring.redis.pool.min-idle}")
private int redisPoolMinIdle;
/**
* 配置Key的生成方式
*/
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(o.getClass().getName())
.append(method.getName());
for (Object object : objects) {
stringBuilder.append(object.toString());
}
return stringBuilder.toString();
}
};
}
/**
* 创建redis连接工厂
*/
public JedisConnectionFactory createJedisConnectionFactory(int dbIndex, String host, int port, String password, int timeout) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setDatabase(dbIndex);
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(port);
jedisConnectionFactory.setPassword(password);
jedisConnectionFactory.setTimeout(timeout);
jedisConnectionFactory.setPoolConfig(setPoolConfig(redisPoolMaxIdle, redisPoolMinIdle, redisPoolMaxActive, redisPoolMaxWait, true));
return jedisConnectionFactory;
}
/**
* 配置CacheManager
*/
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
return redisCacheManager;
}
/**
* 设置连接池属性
*/
public JedisPoolConfig setPoolConfig(int maxIdle, int minIdle, int maxActive, int maxWait, boolean testOnBorrow) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxWaitMillis(maxWait);
poolConfig.setTestOnBorrow(testOnBorrow);
return poolConfig;
}
/**
* 设置RedisTemplate的序列化方式
*/
public void setSerializer(RedisTemplate redisTemplate) {
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//设置键(key)的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置值(value)的序列化方式
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
}
}
5 使用Java类注入多个数据源
数据源一
import com.xiaoqiang
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* llld-parent 配置默认Redis操作实例 到Spring中
*/
@Configuration
@EnableCaching
public class DefaultRedisConfig extends RedisConfig {
@Value("${spring.redis.database}")
private int dbIndex;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
/**
* 配置redis连接工厂
*/
@Bean
public RedisConnectionFactory defaultRedisConnectionFactory() {
return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
}
/**
* 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
*/
@Bean(name = "defaultRedisTemplate")
public RedisTemplate defaultRedisTemplate() {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(defaultRedisConnectionFactory());
setSerializer(template);
template.afterPropertiesSet();
return template;
}
}
数据源二
import com.xiaoqiang
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* llld-parent 配置缓存Redis操作实例 到Spring中
*/
@Configuration
@EnableCaching
public class CacheRedisConfig extends RedisConfig {
@Value("${spring.redis2.database}")
private int dbIndex;
@Value("${spring.redis2.host}")
private String host;
@Value("${spring.redis2.port}")
private int port;
@Value("${spring.redis2.password}")
private String password;
@Value("${spring.redis2.timeout}")
private int timeout;
/**
* 配置redis连接工厂
*
* @return
*/
@Primary
@Bean
public RedisConnectionFactory cacheRedisConnectionFactory() {
return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
}
/**
* 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
*
* @return
*/
@Bean(name = "cacheRedisTemplate")
public RedisTemplate cacheRedisTemplate() {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(cacheRedisConnectionFactory());
setSerializer(template);
template.afterPropertiesSet();
return template;
}
}
6 实体类
import com.xiaoqiang
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* jpa-demo
*/
@Data
@AllArgsConstructor
public class Person {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 地址
*/
private String address;
/**
* 邮箱
*/
private String email;
/**
* 手机号码
*/
private String phoneNum;
}
7 测试
import com.xiaoqinag
import com.lilian.entity.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* spring-boot-data-packing
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MultiRedisTest {
@Resource(name = "defaultRedisTemplate")
private RedisTemplate<String, Object> redisTemplate;
@Resource(name = "cacheRedisTemplate")
private RedisTemplate<String, Object> redisTemplate1;
@Test
public void stringRedisTest() {
redisTemplate.opsForValue().set("xiaoqiang", "111111");
redisTemplate1.opsForValue().set("xiaoqiang", "222222");
}
@Test
public void objectRedisTest() {
redisTemplate.opsForValue().set("person", new Person("小强", 20, "上海", "xiaoqiang@ali.com", "1324567891"));
redisTemplate1.opsForValue().set("person", new Person("小强", 35, "北京", "xiaoqiang@ali.com", "1324567891"));
}
}以上就是springboot配置多个redis数据源的demo,小伙伴可以动动手指敲敲代码。写完这个demo的心情如图:
????????????戳二维码关注小强哦~????????????
更多推荐
已为社区贡献2条内容
所有评论(0)