Spring boot如何快速的配置多个Redis数据源
作者:木子山禾 时间:2023-05-12 18:27:32
简介
redis 多数据源主要的运用场景是在需要使用多个redis服务器或者使用多个redis库,本文采用的是fastdep依赖集成框架,快速集成Redis多数据源并集成lettuce连接池,只需引入依赖后在yaml文件中配置多数据源连接信息即可。
源码地址
希望大家可以star支持一下,后续还会加入其它依赖的简易整合。
https://github.com/louislivi/fastdep
引入依赖
Maven
<dependency>
<groupId>com.louislivi.fastdep</groupId>
<artifactId>fastdep-redis</artifactId>
<version>1.0.1</version>
</dependency>
Gradle
compile group: 'com.louislivi.fastdep', name: 'fastdep-redis', version: '1.0.1'
配置文件
fastdep:
redis:
redis1: #连接名称
database: 0
host: 192.168.12.88
port: 6379
lettuce: #下面为连接池的补充设置
shutdown-timeout: 100 # 关闭超时时间
pool:
max-active: 18 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
max-wait: 30 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
redis2: #连接名称
database: 1
host: 192.168.12.88
port: 6379
lettuce: #下面为连接池的补充设置
shutdown-timeout: 100 # 关闭超时时间
pool:
max-active: 18 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
max-wait: 30 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
运用
@Autowired
private StringRedisTemplate redis1StringRedisTemplate;
// 注入时 redis1 代表配置文件中的连接名称 StringRedisTemplate 为固定注入redis对象类型,
// 会自动根据注入的变量名进行匹配
@Autowired
private StringRedisTemplate redis2StringRedisTemplate;
@GetMapping("redis")
public void redis() {
System.out.println(redis1StringRedisTemplate.opsForValue().get("test"));
System.out.println(redis2StringRedisTemplate.opsForValue().get("test"));
}
扩展
有时候需要自定义redisTemplate序列化和增加一些额外的配置,这时候我们可以封装一个redis工具类来实现
package com.louislivi.fastdep.test.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
/**
* RedisUtil
*
* @author : louislivi
*/
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redis1StringRedisTemplate;
@Autowired
private StringRedisTemplate redis2StringRedisTemplate;
@Autowired
private RedisTemplate redis2RedisTemplate;
@Autowired
private RedisTemplate redis1RedisTemplate;
public RedisTemplate redisTemplate(String name) {
RedisTemplate redisTemplate;
switch (name) {
case "redis2":
redisTemplate = redis2RedisTemplate;
break;
default:
redisTemplate = redis1RedisTemplate;
break;
}
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(stringRedisSerializer);
return redisTemplate;
}
public StringRedisTemplate stringRedisTemplate(String name) {
StringRedisTemplate stringRedisTemplate;
switch (name) {
case "redis2":
stringRedisTemplate = redis2StringRedisTemplate;
break;
default:
stringRedisTemplate = redis1StringRedisTemplate;
break;
}
stringRedisTemplate.setEnableTransactionSupport(true);
return stringRedisTemplate;
}
}
@Autowired
private RedisUtil redisUtil;
@GetMapping("redis")
public void redis() {
System.out.println(redisUtil.redisTemplate("redis1").opsForValue().get("test"));
System.out.println(redisUtil.stringRedisTemplate("redis2").opsForValue().get("test"));
}
原理
使用ImportBeanDefinitionRegistrar BeanDefinitionBuilder.genericBeanDefinition动态注入Bean其实很简单有兴趣可以去看看源码,这样的依赖集成是不是简单了很多呢?
希望大家能够支持开源,给个小星星,后续还会继续开发其他依赖的整合,甚至兼容其他框架使用。fastdep让java整合依赖更简单。在此也招募有志同道合的coder共同完善这个项目。
来源:https://www.cnblogs.com/muzishanhe/p/11935839.html
标签:Springboot,Redis,数据源
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#启动进程的几种常用方法
2023-06-18 04:13:48
Java使用easyExcel导出excel数据案例
2022-02-21 19:39:27
![](https://img.aspxhome.com/file/2023/0/62330_0s.png)
MySQL+SSM+Ajax上传图片问题
2023-01-26 04:22:52
![](https://img.aspxhome.com/file/2023/4/62974_0s.png)
Java java.sql.Timestamp时间戳案例详解
2023-11-10 13:50:47
SpringBoot如何进行对象复制的实践
2023-11-23 03:40:19
![](https://img.aspxhome.com/file/2023/5/59935_0s.jpg)
Java的idea连接mongodb数据库的详细教程
2023-11-19 08:07:58
![](https://img.aspxhome.com/file/2023/1/59711_0s.png)
15个顶级Java多线程面试题(附答案)
2023-11-24 01:53:44
C++实现LeetCode(159.最多有两个不同字符的最长子串)
2023-06-20 22:39:46
Mybatis的几种传参方式详解
2021-12-08 15:35:53
idea mybatis配置log4j打印sql语句的示例
2023-11-25 10:32:39
springboot整合微信支付sdk过程解析
2021-12-30 22:47:29
![](https://img.aspxhome.com/file/2023/6/59886_0s.jpg)
java 如何判断是否是26个英文字母
2023-11-29 15:51:59
Spring Cloud Eureka 服务上下线监控的实现
2022-02-18 21:06:15
5种Android数据存储方式汇总
2023-08-06 06:49:04
Flutter控制组件显示和隐藏三种方式详解
2023-07-07 03:03:30
Java对zip,rar,7z文件带密码解压实例详解
2023-11-29 05:08:32
![](https://img.aspxhome.com/file/2023/4/60674_0s.png)
Java多线程之ThreadLocal浅析
2023-06-19 19:55:37
![](https://img.aspxhome.com/file/2023/0/57500_0s.webp)
基于selenium-java封装chrome、firefox、phantomjs实现爬虫
2022-04-07 19:04:28
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
![](https://img.aspxhome.com/file/2023/9/58909_0s.png)
浅谈一下Java中的悲观锁和乐观锁
2023-08-12 05:54:27