springboot2.1+redis多数据源的配置
springboot系列学习笔记全部文章请移步值博主专栏**: spring boot 2.X/spring cloud Greenwich。
由于是一系列文章,所以后面的文章可能会使用到前面文章的项目。springboot系列代码全部上传至GitHub:https://github.com/liubenlong/springboot2_demo
本系列环境:Java11;springboot 2.1.1.RELEASE;springcloud Greenwich.RELEASE;MySQL 8.0.5;
文章目录
多数据源配置
有时候会遇到在一个项目中访问多个redis数据源的情况,这里就模拟一下springboot2.1+redis多数据源的配置
多数据源配置
这里采用不同的datebase模拟多数据源。
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
lettuce:
pool:
max-active: 200 #连接池最大连接数(使用负值表示没有限制)
max-idle: 20 # 连接池中的最大空闲连接
min-idle: 5 #连接池中的最小空闲连接
# 这里在IDEA中显示红色错误,不用管,因为org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool.setMaxWait(Duration maxWait)
# 方法参数是Duration,IDEA无法识别,但是会自动注入成功
max-wait: 2000 # 当连接池耗尽时, 抛出异常之前,连接分配被阻塞的时间,也就是连接池满后最长等待时间,负值表示无限期等待
redis2:
host: 127.0.0.1
port: 6379
database: 1
由于使用了多数据源,固不可以使用默认的springboot进行加载初始化了,这里需要使用自定义config来实现。
这里需要配置lettuce连接池–> LettuceConnectionFactory–>RedisTemplate
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration
public class SpringbootConfig {
/**
* 配置lettuce连接池
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
public GenericObjectPoolConfig redisPool() {
return new GenericObjectPoolConfig<>();
}
/**
* 配置第一个数据源的
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public RedisStandaloneConfiguration redisConfig() {
return new RedisStandaloneConfiguration();
}
/**
* 配置第二个数据源
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis2")
public RedisStandaloneConfiguration redisConfig2() {
return new RedisStandaloneConfiguration();
}
/**
* 配置第一个数据源的连接工厂
* 这里注意:需要添加@Primary 指定bean的名称,目的是为了创建两个不同名称的LettuceConnectionFactory
*
* @param config
* @param redisConfig
* @return
*/
@Bean("factory")
@Primary
public LettuceConnectionFactory factory(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig) {
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
return new LettuceConnectionFactory(redisConfig, clientConfiguration);
}
@Bean("factory2")
public LettuceConnectionFactory factory2(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig2) {
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
return new LettuceConnectionFactory(redisConfig2, clientConfiguration);
}
/**
* 配置第一个数据源的RedisTemplate
* 注意:这里指定使用名称=factory 的 RedisConnectionFactory
* 并且标识第一个数据源是默认数据源 @Primary
*
* @param factory
* @return
*/
@Bean("redisTemplate")
@Primary
public RedisTemplate<String, String> redisTemplate(@Qualifier("factory") RedisConnectionFactory factory) {
return getStringStringRedisTemplate(factory);
}
/**
* 配置第一个数据源的RedisTemplate
* 注意:这里指定使用名称=factory2 的 RedisConnectionFactory
*
* @param factory2
* @return
*/
@Bean("redisTemplate2")
public RedisTemplate<String, String> redisTemplate2(@Qualifier("factory2") RedisConnectionFactory factory2) {
return getStringStringRedisTemplate(factory2);
}
/**
* 设置序列化方式 (这一步不是必须的)
*
* @param factory2
* @return
*/
private RedisTemplate<String, String> getStringStringRedisTemplate(RedisConnectionFactory factory2) {
StringRedisTemplate template = new StringRedisTemplate(factory2);
template.setKeySerializer(RedisSerializer.string());
template.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
template.setHashKeySerializer(RedisSerializer.string());
template.setHashValueSerializer(new FastJsonRedisSerializer<>(Object.class));
return template;
}
}
使用也非常简单:
/**
* 不写默认使用带有@Primary的RedisTemplate
*/
@Autowired
private RedisTemplate redisTemplate;
@Autowired
@Qualifier("redisTemplate2")
private RedisTemplate redisTemplate2;
原文:https://blog.csdn.net/liubenlong007/article/details/86477692
springboot2.1+redis多数据源的配置的更多相关文章
- SpringBoot2 + Druid + Mybatis 多数据源动态配置
在大数据高并发的应用场景下,为了更快的响应用户请求,读写分离是比较常见的应对方案.读写分离会使用多数据源的使用.下面记录如何搭建SpringBoot2 + Druid + Mybatis 多数据源配 ...
- SpringBoot2整合Redis多数据源
配置文件属性 spring: redis: database: 1 host: 192.168.50.144 port: 6379 password: timeout: 600 #Springboot ...
- Spring Boot 2.x Redis多数据源配置(jedis,lettuce)
Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...
- 在Springboot2.0项目中使用Druid配置多数据源
在Springboot出现之前配置数据源以及相关的事物,缓存等内容一直是个繁琐的工作,但是Springboot出现后这些基本都可以靠默认配置搞定,就变得很轻松了.这就是现在推崇模板>配置的原因, ...
- springboot redis多数据源
springboot中默认的redis配置是只能对单个redis库进行操作的. 那么我们需要多个库操作的时候这个时候就可以采用redis多数据源. 本代码参考RedisAutoConfiguratio ...
- 20. Spring Boot 默认、自定义数据源 、配置多个数据源 jdbcTemplate操作DB
Spring-Boot-2.0.0-M1版本将默认的数据库连接池从tomcat jdbc pool改为了hikari,这里主要研究下hikari的默认配置 0. 创建Spring Boot项目,选中 ...
- springboot2.0+mybatis多数据源集成
最近在学springboot,把学的记录下来.主要有springboot2.0+mybatis多数据源集成,logback日志集成,springboot单元测试. 一.代码结构如下 二.pom.xml ...
- SpringBoot2整合Redis缓存
遵循SpringBoot三板斧 第一步加依赖 <!-- Redis --> <dependency> <groupId>org.springframework.bo ...
- Spring, MyBatis 多数据源的配置和管理
同一个项目有时会涉及到多个数据库,也就是多数据源.多数据源又可以分为两种情况: 1)两个或多个数据库没有相关性,各自独立,其实这种可以作为两个项目来开发.比如在游戏开发中一个数据库是平台数据库,其它还 ...
随机推荐
- Anaconda入门教程【快速掌握】
Anaconda 使用指南 概述 很多学习python的初学者甚至学了有一段时间的人接触到anaconda或者其他虚拟环境工具时觉得无从下手, 其主要原因就是不明白这些工具究竟有什么用, 是用来做什么 ...
- multer 文件后缀名
我的代码是这样写的. var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uplo ...
- gradle 多模块Springboot项目 compile project引用其他模块的坑
本来以为子项目中compile project(':xxx'),就能引用其他模块了,因为之后idea也没在引用时候标红 然而我gradle build的时候,居然各种找不到引用模块的类 最后在stac ...
- Java-100天知识进阶-JVM内存-知识铺(三)
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. Java内存模型(JMM) JVM内存模式是JVM的内存分区 Java内存模式是一种虚 ...
- C# 爬虫相关的、可供参考的开源项目
1. Abots https://github.com/sjdirect/abot/ 2. DotnetSpider https://github.com/dotnetcore/DotnetSpide ...
- .NET Core 学习笔记
1. System.Composition – Using Import Attributes 截图: 2. System.Composition – Using Reflection 截图: 以后会 ...
- Python sorted 函数
Python sorted 函数 sorted 可以对所有可迭代的对象进行排序操作,sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作.从新排序列表. sorted 语法: ...
- Microsoft.Windows.Controls.Ribbon.RibbonWindow 碰到 AvalonDock出现的诡异现象
部分一 14年底进入目前公司时,领导准备开发一款新软件平台以取代原有平台.原平台采用C++Build开发界面(window c/s客户端) .Visual Studio(封装dll模块).过完年,领导 ...
- ASP.NET Web API 2 的返回结果
HttpResponseMessage IHttpActionResult void 某些其他类型 总结归纳 原文地址:https://www.cnblogs.com/xgzh/p/11208611. ...
- WPF控件模板(6)
什么是ControlTemplate? ControlTemplate(控件模板)不仅是用于来定义控件的外观.样式, 还可通过控件模板的触发器(ControlTemplate.Triggers)修改控 ...