在实际开发中,经常会引入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. 【C++】常见易犯错误之数值类型取值溢出与截断(2)

    本节内容紧接上节,解决红色字体遗留问题.本节所有例子运行环境: win10 + VS2015 + X64 + debug 在上节例子中,查看变量 c .d .d+1 的类型. //// Console ...

  2. [JavaWeb基础] 026.JAVA中使用Axis搭建webservice-环境搭建(一)

    在实际的项目开发过程中,我们经常会使用第三方交互,特别是在前后端语言不一致的情况下,相信webservice这个第三方控件大家都清楚.后面会慢慢带大家来学习它的使用方式,下面就先讲讲它的工具搭建. 一 ...

  3. 【转】shell的反引号、单引号、双引号的作用

    Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...

  4. scikit-learn中文文档

    http://sklearn.apachecn.org/cn/0.19.0/tutorial/statistical_inference/supervised_learning.html

  5. keras常见问题

    问题:AttributeError: 'CRF' object has no attribute '_outbound_nodes' 解答:这个一般情况下是keras版本的问题,将其改为keras== ...

  6. conda虚拟环境安装

    一.背景 需要学习mxnet,建一个conda虚拟软件环境. 二.步骤 1.下载anaconda安装文件:https://mirrors.tuna.tsinghua.edu.cn/anaconda/m ...

  7. Chisel3 - model - Hardware Model

    https://mp.weixin.qq.com/s/x6j7LZg7i7i_KcNEA8YCQw   Chisel作为领域专用语言(DSL),用于构建硬件模型.待硬件模型建立后,再基于模型进行仿真. ...

  8. Entity FrameWork 实现分页

    SQl语句进行分页 SQL语句进行分页主要是应用Entity FrameWork的SqlQuery()传入SQL语句进行查询时分页. 效果展示. 页面代码展示,显示是用Repeater控件进行动态显示 ...

  9. Java实现 蓝桥杯VIP 算法提高 质因数2

    算法提高 质因数2 时间限制:1.0s 内存限制:256.0MB 将一个正整数N(1<N<32768)分解质因数,把质因数按从小到大的顺序输出.最后输出质因数的个数. 输入格式 一行,一个 ...

  10. 使用Python爬取网页的相关内容(图片,文字,链接等等)

    代码解释的很详细了,有不明白的欢迎评论 ~~~滑稽 import requests from bs4 import BeautifulSoup # #获取图片 输入网址 req=requests.ge ...