Spring boot配置多个Redis数据源操作实例
Posted 起个名字好难
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot配置多个Redis数据源操作实例相关的知识,希望对你有一定的参考价值。
原文:https://www.jianshu.com/p/c79b65b253fa
Spring boot配置多个Redis数据源操作实例
在SpringBoot是项目中整合了两个Redis的操作实例,可以增加多个;
一般在一个微服务生态群中是不会出现多个Redis中间件的,所以这种场景很少见,但也不可避免,但是不建议使用,个人建议,勿喷。
- 基于Maven3.0搭建,spring1.5.9.RELEASE和JDK1.8
1、新建SpringBoot项目,添加依赖
<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>
2、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 # 连接超时时间(毫秒)
3、新建RedisConfig类
package com.lilian.config;
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多实例的基类
*
* @Author 孙龙
* @Date 2018/8/13
*/
@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的生成方式
*
* @return
*/
@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连接工厂
*
* @param dbIndex
* @param host
* @param port
* @param password
* @param timeout
* @return
*/
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
*
* @param redisTemplate
* @return
*/
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
return redisCacheManager;
}
/**
* 设置连接池属性
*
* @param maxIdle
* @param minIdle
* @param maxActive
* @param maxWait
* @param testOnBorrow
* @return
*/
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的序列化方式
*
* @param 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();
}
}
4、使用Java类注入多个数据源
- 数据源一
package com.lilian.config;
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中
*
* @Author 孙龙
* @Date 2018/8/2
*/
@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连接工厂
*
* @return
*/
@Bean
public RedisConnectionFactory defaultRedisConnectionFactory() {
return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
}
/**
* 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
*
* @return
*/
@Bean(name = "defaultRedisTemplate")
public RedisTemplate defaultRedisTemplate() {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(defaultRedisConnectionFactory());
setSerializer(template);
template.afterPropertiesSet();
return template;
}
}
- 数据源二
package com.lilian.config;
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中
*
* @Author 孙龙
* @Date 2018/8/2
*/
@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;
}
}
- 数据源三同理。。。
5、随便定义一个实体类
package com.lilian.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* jpa-demo
*
* @Author 孙龙
* @Date 2018/7/3
*/
@Data
@AllArgsConstructor
public class Person {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 地址
*/
private String address;
/**
* 邮箱
*/
private String email;
/**
* 手机号码
*/
private String phoneNum;
}
6、测试方法
package com.lilian;
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
*
* @Author 孙龙
* @Date 2018/8/13
*/
@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("slzzzz", "111111");
redisTemplate1.opsForValue().set("slzzzz", "222222");
}
@Test
public void objectRedisTest() {
redisTemplate.opsForValue().set("person", new Person("李飞", 20, "临汾", "[email protected]", "1324567891"));
redisTemplate1.opsForValue().set("person", new Person("李大壮", 35, "西安", "[email protected]", "1324567891"));
}
}
7、结果
使用redis可视化工具查看是否成功;
以上是关于Spring boot配置多个Redis数据源操作实例的主要内容,如果未能解决你的问题,请参考以下文章