SpringBoot2整合Redis多数据源
配置文件属性
spring:
redis:
database: 1
host: 192.168.50.144
port: 6379
password:
timeout: 600 #Springboot2.0 不能设置为0
lettuce:
pool:
max-active: 50
max-wait: -1
max-idle: 8
min-idle: 0
redis2:
database: 2
host: 192.168.50.144
port: 6379
password:
timeout: 600
配置类
@EnableCaching
@Configuration
public class RedisDevConfiguration {
@Bean(name = "redisDevTemplate")
public StringRedisTemplate redisTemplate(@Value("${spring.redis.host}") String hostName,
@Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password,
@Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle, @Value("${spring.redis.lettuce.pool.max-active}") int maxTotal,
@Value("${spring.redis.database}") int index, @Value("${spring.redis.lettuce.pool.max-wait}") long maxWaitMillis,@Value("${spring.redis.lettuce.pool.min-idle}") int minIdle) {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(
connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis,minIdle));
return temple;
}
@Bean(name = "redisUatTemplate")
public StringRedisTemplate redisUatTemplate(@Value("${spring.redis2.host}") String hostName,
@Value("${spring.redis2.port}") int port, @Value("${spring.redis2.password}") String password,
@Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle, @Value("${spring.redis.lettuce.pool.max-active}") int maxTotal,
@Value("${spring.redis2.database}") int index, @Value("${spring.redis.lettuce.pool.max-wait}") long maxWaitMillis,@Value("${spring.redis.lettuce.pool.min-idle}") int minIdle) {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(
connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis,minIdle));
return temple;
}
public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
int maxTotal, int index, long maxWaitMillis,int minIdle) {
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,minIdle));
// 初始化连接pool
jedis.afterPropertiesSet();
RedisConnectionFactory factory = jedis;
return factory;
}
public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis,int minIdle) {
JedisPoolConfig poolCofig = new JedisPoolConfig();
poolCofig.setMaxIdle(maxIdle);
poolCofig.setMaxTotal(maxTotal);
poolCofig.setMaxWaitMillis(maxWaitMillis);
poolCofig.setMinIdle(minIdle);
return poolCofig;
}
}
如何使用
@Resource(name = "redisDevTemplate")
private StringRedisTemplate template;
@Resource(name = "redisUatTemplate")
private StringRedisTemplate lockTemplate;
SpringBoot2整合Redis多数据源的更多相关文章
- springboot2.1+redis多数据源的配置
springboot系列学习笔记全部文章请移步值博主专栏**: spring boot 2.X/spring cloud Greenwich. 由于是一系列文章,所以后面的文章可能会使用到前面文 ...
- SpringBoot2整合Redis缓存
遵循SpringBoot三板斧 第一步加依赖 <!-- Redis --> <dependency> <groupId>org.springframework.bo ...
- springboot2 整合redis
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- SpringBoot2整合Redis
pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 手把手教你SpringBoot2整合Redis
此文仅为初学java的同学学习,大佬请勿喷,文末我会附上完整代码包供大家参考 redis的搭建教程此处略过,大家自行百度,本文的教程开始: 一.先在pom.xml中添加相关依赖 <!--redi ...
- SpringBoot2.x整合Redis实战 4节课
1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download 2.新手 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解
笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...
- SpringBoot2 整合JTA组件,多数据源事务管理
本文源码:GitHub·点这里 || GitEE·点这里 一.JTA组件简介 1.JTA基本概念 JTA即Java-Transaction-API,JTA允许应用程序执行分布式事务处理,即在两个或多个 ...
- springboot2.2.2企业级项目整合redis与redis 工具类大全
1.springboot2.2.2整合redis教程很多,为此编写了比较完整的redis工具类,符合企业级开发使用的工具类 2.springboot与redis maven相关的依赖 <depe ...
随机推荐
- <JZOJ5937&luogu3944>斩杀计划&肮脏的牧师
第一次写桶排相关题.... #include<cstdio> #include<iostream> #define rint register int template < ...
- unittest(11)- get_data自定义取某几条测试数据
在get_data中定义取全部用例和取部分用例两种模式 # 1. http_request.py import requests class HttpRequest: def http_request ...
- 如何设计一个LRU Cache
如何设计一个LRU Cache? Google和百度的面试题都出现了设计一个Cache的题目,什么是Cache,如何设计简单的Cache,通过搜集资料,本文给出个总结. 通常的问题描述可以是这样: Q ...
- 吴裕雄--天生自然 R语言数据分析:火箭发射的地点、日期/时间和结果分析
dfS = read.csv("F:\\kaggleDataSet\\spacex-missions\\database.csv") library(dplyr) library( ...
- Selenium自动化测试实例-基于python
一.Selenium介绍 Selenium是一个Web开源自动化测试框架,具有页面级操作.模拟用户真实操作.API从系统层面触发事件等特点. 1.版本 Selenium 1.0 Sever/Clie ...
- linux系统文件被删的几种恢复方法
参考链接:https://www.cnblogs.com/276815076/p/5703796.html 1.几种恢复方法,这里只是记录别的博客提到的方法,本人并未亲自验证. ext3用ext3gr ...
- Jennifer Chayes: 生活始终在你手中
Jennifer Chayes 听到Mark Kac.Freeman J. Dyson.林家翘,或者是David I. Gottlie.BerndSturmfels和Sir John Ball等 ...
- pattern space and hold space of sed
Copied from: stackoverflow When sed reads a file line by line, the line that has been currently read ...
- SetTimeout()多次运行函数后越来越快的问题
问题原因很简单,但是由于代码逻辑问题,一直没有考虑到: 网上有个帖子说的很明白:原帖入口 假如你在0时刻点击了一下按钮,那么500ms时数字会跳一下,1000ms会再跳一下,依次类推,1500,200 ...
- SQL注入攻击浅谈
原理 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据 ...