在实际开发中,经常会引入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的更多相关文章

  1. Springboot 2.0 - 集成redis

    序 最近在入门SpringBoot,然后在感慨 SpringBoot较于Spring真的方便多时,顺便记录下自己在集成redis时的一些想法. 1.从springboot官网查看redis的依赖包 & ...

  2. SpringBoot(十)_springboot集成Redis

    Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...

  3. 【转载】Springboot整合 一 集成 redis

    原文:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html https://blog.csdn.net/plei_ ...

  4. Spring Boot从入门到精通(七)集成Redis实现Session共享

    单点登录(SSO)是指在多个应用系统中,登录用户只需要登录验证一次就可以访问所有相互信任的应用系统,Redis Session共享是实现单点登录的一种方式.本文是通过Spring Boot框架集成Re ...

  5. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  6. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  7. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  8. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  9. Springboot集成Redis步骤

    Spring boot 集成Redis的步骤如下: 1.在pom.xml中配置相关的jar依赖: <!--加载spring boot redis包 --> <dependency&g ...

随机推荐

  1. windows环境下Kubernetes及Docker安装(那些坑)

    k8s 和 Docker容器技术,当前非常流行的技术. 让人日狗的是,   这套技术栈对CN的donet 程序员不怎么友好.娓娓道来,1. 好多镜像都是需要梯子才能访问: 2. window程序员天生 ...

  2. [SD心灵鸡汤]007.每月一则 - 2015.11

    1.不要因为世界太过复杂,而背叛了你的单纯. 2.人的一生要疯狂一次,无论是为一个人,一段情,一段路途或一个梦想. 3.时间真的很神奇,你永远不知道它会如何改变你.换句话说:以前难吃的蔬菜.苦涩的啤酒 ...

  3. MRCTF 2020 WP

    MRCTF 2020 WP 引言 周末趁上课之余,做了一下北邮的CTF,这里记录一下做出来的几题的WP ez_bypass 知识点:MD5强类型比较,is_numeric()函数绕过 题目源码: I ...

  4. Parrot os安装nvidia失败恢复

    因为两种显卡,amd和nvidia,所以按照parrot官方文档安装驱动,结果可想而知,安装失败--- 内心万马奔腾,去国外论坛也发现很多求助的小伙伴,所以有了我这次随笔,如何恢复你的parrot 黑 ...

  5. Chisel3 - util - Mux

    https://mp.weixin.qq.com/s/TK1mHqvDpG9fbLJyNxJp-Q   Mux相关电路生成器.   参考链接: https://github.com/freechips ...

  6. Splay代码简化版

    皆さん.こんにちは.上一篇文章,我们讲了Splay如何实现.这一篇我们来让我们的伸展树短一点. 上一篇Splay讲解的链接:リンク. 首先还是变量的定义,在这里呢,我把一些小函数也用Define来实现 ...

  7. 如何获取CSDN的积分?

    个人感觉就是写博客就给积分 具体给多少? CSDN应该有自己的积分规则 总之一句话:写博客涨积分

  8. Java实现 LeetCode 611 有效三角形的个数(双指针)

    611. 有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 ( ...

  9. Java实现 蓝桥杯VIP 算法提高 5-3日历

    算法提高 5-3日历 时间限制:1.0s 内存限制:256.0MB 问题描述 已知2007年1月1日为星期一.设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印.为 ...

  10. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...