springboot 2.X 集成redis
在实际开发中,经常会引入redis中间件做缓存,这里介绍springboot2.X后如何配置redis
1 Maven中引入redis
springboot官方通过spring-boot-autoconfigure和redis的starter包来简化我们的配置工作。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2 yml中配置redis参数
springboot2.x后默认使用Lettuce,Lettuce的连接是基于netty的,满足多线程下的并发访问,也可以使用jedis,但是jedis在多线程下线程不安全,需要使用连接池。
2.1 Lettuce配置
spring:
#redis基础配置
redis:
host: 127.0.0.1
port: 6379
timeout: 3000
database: 1
password:
#lettuce连接池配置
lettuce:
pool:
min-idle: 0
max-idle: 5
max-active: 8
max-wait: -1
3 自定义RedisTemplate
springboot中默认提供了RedisTemplate<K,V>和StringRedisTemplate,RedisTemplate中默认使用JdkSerializationRedisSerializer来实现序列化,是通过字节数组来保存数据
StringRedisTemplate是集成了RedisTemplate,默认使用StringRedisSerializer来实现序列化。
@Configuration
public class LettuceRedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
到这里就配置完成了,我们就可以使用RedisTemplate<String, Object>来操作redis。
2.2 yml配置jedis
spring:
#redis基础配置
redis:
host: 127.0.0.1
port: 6379
timeout: 3000
database: 1
password:
#jedis 连接池配置
jedis:
pool:
# 连接池中的最小空闲连接 默认 0
min-idle: 0
# 连接池中的最大空闲连接 默认 8
max-idle: 5
# 连接池最大连接数(使用负值表示没有限制) 默认 8
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-wait: -1
3.jedis配置
/**
* 连接池配置信息
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大连接数
jedisPoolConfig.setMaxTotal(maxActive);
// 当池内没有可用连接时,最大等待时间
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// 最大空闲连接数
jedisPoolConfig.setMinIdle(maxIdle);
// 最小空闲连接数
jedisPoolConfig.setMinIdle(minIdle);
// 其他属性可以自行添加
return jedisPoolConfig;
}
/**
* Jedis 连接
*
* @param jedisPoolConfig
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
.poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
redisStandaloneConfiguration.setDatabase(database);
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
//开启事务,默认关闭,不涉及redis事务就不要开启,可能导致获取错误的存储信息
//redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
到这里就配置完成了,我们就可以使用RedisTemplate<String, Object>来操作redis。
实际开发中可以根据实际应用和熟悉程度来选择Jedis和Lettuce,
Jedis和Lettuce相关配置代码和单元测试代码已经提交到git上,需要自取。
springboot 2.X 集成redis的更多相关文章
- Springboot 2.0 - 集成redis
序 最近在入门SpringBoot,然后在感慨 SpringBoot较于Spring真的方便多时,顺便记录下自己在集成redis时的一些想法. 1.从springboot官网查看redis的依赖包 & ...
- SpringBoot(十)_springboot集成Redis
Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...
- 【转载】Springboot整合 一 集成 redis
原文:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html https://blog.csdn.net/plei_ ...
- Spring Boot从入门到精通(七)集成Redis实现Session共享
单点登录(SSO)是指在多个应用系统中,登录用户只需要登录验证一次就可以访问所有相互信任的应用系统,Redis Session共享是实现单点登录的一种方式.本文是通过Spring Boot框架集成Re ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- Windows环境下springboot集成redis的安装与使用
一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...
- Springboot集成Redis步骤
Spring boot 集成Redis的步骤如下: 1.在pom.xml中配置相关的jar依赖: <!--加载spring boot redis包 --> <dependency&g ...
随机推荐
- [SD心灵鸡汤]002.每月一则 - 2015.06
1.用最多的梦面对未来 2.自己要先看得起自己,别人才会看得起你 3.一个今天胜过两个明天 4.要铭记在心:每天都是一年中最美好的日子 5.乐观者在灾祸中看到机会:悲观者在机会中看到灾祸 6.有勇气并 ...
- 几种常见的app推广形式
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- SET运算符
-- SET操作符: -- union 联合去重 两个表的字段个数和类型都得一样,起别名 应该在第一个表上操作,,排序按照第一列从小到大来排的 select employee_id, depart ...
- 【Python】自己写日志功能
Python有自带的logging模块,用于日志记录,功能很强大,但不好用,使用挺麻烦的,而且发现了几个bug,调用了一个logger.warning()一次,结果日志文件中出现了n行记录,且逐渐变成 ...
- 内部服务器错误Internal server error解决方法
这里说的内部服务器错误是网站前台能正常访问,而后台程序在执行某项任务/功能时所出现的内部服务器错误解决方法: 此错误通常是超时导致的,像程序在执行采集.静态页面生成时所耗时间太长导致达到超时限制的: ...
- StackOverflow 创始人关于如何高效编程的清单.md
这是 StackOverflow 联合创始人 Jeff Atwood 注释的十戒.程序员普遍有很强的自尊心,都应该看看本文,打印下来时刻提醒自己. "无我编程"发生在开发阶段,表现 ...
- Windows平台搭建Git服务教程详解
引言 软件企业的核心就是代码,如何确保代码的安全?如何在团队开发中协同工作?为解决这些问题,我们需要采用相应的管理工具来满足管理的需求.探长从最初的VSS.SVN.TFS到现在的Git存储一路走来,感 ...
- Linux (四) 基础命令 下
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.查看文件内容 1.命令 cat 对应单词:concatenate 作用:查看文件内容 常用参数: ...
- Java实现 蓝桥杯VIP 算法提高 多项式输出
算法提高 多项式输出 时间限制:1.0s 内存限制:512.0MB 问题描述 一元n 次多项式可用如下的表达式表示: f(x)=a[n]xn+a[n-1]x(n-1)+-+a[1]x+a[0], a[ ...
- java实现第七届蓝桥杯阶乘位数
阶乘位数 阶乘位数 9的阶乘等于:362880 它的二进制表示为:1011000100110000000 这个数字共有19位. 请你计算,9999 的阶乘的二进制表示一共有多少位? 注意:需要提交的是 ...