springboot redis多数据源设置
RedisSpringBoot 设置 数据源
2023-09-14 08:59:55 时间
遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库、开发库和线上库。
项目使用的是spring boot集成redis,实现如下:
1. 引入依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.多数据源设置
application.yml设置(application.properties同理):
spring:
redis:
database: 0
pool:
max-active: 8
max-idle: 9
max-wait: -1
min-idle: 0
redis-dev:
host: 填redis的ip地址
prot: 填redis的端口号
password: 填redis的密码
testOnBorrow: fals
redis-test:
host:
prot:
password:
testOnBorrow: false
redis-online:
host:
prot:
password:
testOnBorrow: false
针对每个数据源写一个配置类:
这里就只列举其中一个,不同的数据源设置不同的@Bean和@Value注解即可
@Configuration
public class RedisDevConfiguration {
@Bean(name = "redisDevTemplate")
public StringRedisTemplate redisTemplate(@Value("${spring.redis-dev.host}") String hostName,
@Value("${spring.redis-dev.port}") int port, @Value("${spring.redis-dev.password}") String password,
@Value("${spring.redis-dev.testOnBorrow}") boolean testOnBorrow,
@Value("${spring.redis.pool.max-idle}") int maxIdle, @Value("${spring.redis.pool.max-active}") int maxTotal,
@Value("${spring.redis.database}") int index, @Value("${spring.redis.pool.max-wait}") long maxWaitMillis) {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(
connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
return temple;
}
public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
JedisConnectionFactory jedis = new JedisConnectionFactory();
jedis.setHostName(hostName);
jedis.setPort(port);
if (StringUtils.isNotEmpty(password)) {
jedis.setPassword(password);
}
if (index != 0) {
jedis.setDatabase(index);
}
jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
// 初始化连接pool
jedis.afterPropertiesSet();
RedisConnectionFactory factory = jedis;
return factory;
}
public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
JedisPoolConfig poolCofig = new JedisPoolConfig();
poolCofig.setMaxIdle(maxIdle);
poolCofig.setMaxTotal(maxTotal);
poolCofig.setMaxWaitMillis(maxWaitMillis);
poolCofig.setTestOnBorrow(testOnBorrow);
return poolCofig;
}
}
3.构造redis实例
写一个redis实例抽象类
public abstract class AbRedisConfiguration {
protected StringRedisTemplate temple;
public void setData(String key, String value) {
getTemple().opsForValue().set(key, value);
}
public String getData(String key) {
return getTemple().opsForValue().get(key);
}
public StringRedisTemplate getTemple() {
return temple;
}
}
继成抽象类实现操作类,这里只列举一个
@Component
public class RedisDev extends AbRedisConfiguration {
@Resource(name = "redisDevTemplate")
private StringRedisTemplate temple;
public StringRedisTemplate getTemple() {
return temple;
}
}
4.使用
在service中注入redis操作类
@Autowired
private RedisDev redisDev;
@Autowired
private RedisTest redisTest;
@Autowired
private RedisTest redisOnline;
调用不同的操作类即可使用不同的redis数据源。
相关文章
- 【Redis】yum安装redis
- 使用redis和fastjson做应用和mysql之间的缓存
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- Redis学习(8)-redis持久化
- Redis学习(8)-redis其他特性
- redis 简单整理——哨兵部署业务图[二十九]
- redis 简单整理——慢查询[八]
- windows x64安装与测试redis
- Atititt java redis jedis 使用 Redis体系 Redis 命令 Redis 命令Redis 键(key)Redis 字符串(String)Redis 哈希(Hash)R
- Springboot怎么快速集成Redis?
- win7 安装Redis
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
- Python -- Redis 发布订阅
- 白话Redis分布式锁
- 008-spring cache-缓存实现-03-springboot redis单机测试、lettuce链接池集群配置、jedis连接池集群配置
- SpringBoot中使用Redis
- redis面试题汇总
- 遇到问题之-redis安装配置及调优
- 结合实践总结Docker安装Redis
- Redis 主从复制-服务器搭建【薪火相传/哨兵模式】
- ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
- springboot+jwt+shiro+vue+elementUI+axios+redis+mysql简易博客项目
- Nginx Lua Redis 实现灰度发布