在Spring Boot应用程序中使用Redis缓存的步骤:

1、要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-starter-data-redis默认使用了Lettuce。要获得Redis的池化连接工厂,我们需要在类路径上提供commons-pool2,下面展示使用Lettuce情况下的Maven依赖项配置:

org.springframework.bootspring-boot-starter-data-redisorg.apache.commonscommons-pool2

2、在application.properties中配置spring.redis.*前缀的参数:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=7
spring.redis.lettuce.pool.max-idle=7
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.shutdown-timeout=200ms
spring.cache.redis.cache-null-values=false
spring.cache.redis.time-to-live=600000
spring.cache.redis.use-key-prefix=true

3、在 @SpringBootApplication类使用@EnableCaching激活Redis:

@SpringBootApplication
@EnableCaching
publicclassSpringBootAppStarter {
publicstaticvoidmain(String[] args) {
SpringApplication.run(SpringBootAppStarter.class, args);
}
}

如果想用Jedis 客户端,需要排除Lettuce,加入Jedis:

org.springframework.bootspring-boot-starter-data-redisio.lettucelettuce-coreredis.clientsjedis

jedis将自动在类路径上解决commons-pool2, application配置:

spring.redis.host=localhost 
spring.redis.port=6379
spring.redis.password= spring.redis.jedis.pool.max-active=7
spring.redis.jedis.pool.max-idle=7
spring.redis.jedis.pool.min-idle=2
spring.redis.jedis.pool.max-wait=-1ms

写在最后:欢迎留言讨论,加关注,持续更新!!!

阿里P7级教你如何在Spring Boot应用程序中使用Redis的更多相关文章

  1. Spring boot 2.x 中使用redis

    一.添加Maven  依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  2. 如何在Spring Boot中使用Cookies

    一. 导读 本文大纲 读取HTTP Cookie 设置HTTP Cookie 读取所有Cookie[] 为Cookie设置过期时间 Https与Cookie HttpOnly Cookie 删除Coo ...

  3. 阿里架构师的这一份Spring boot使用心得:网友看到都收藏了

    阿里架构师的这一份Spring boot使用心得: 这一份PDF将从Spring Boot的出现开始讲起,到基本的环境搭建,进而对Spring的IOC及AOP进行详细讲解.以此作为理论基础,接着进行数 ...

  4. 如何在Spring boot中修改默认端口

    文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...

  5. 如何在Spring Boot开启事务

    说到事务,那什么是事务呢? 事务(Transaction),一般是指要做的或所做的事情. 原子性(Atomicity):事务作为一个整体被执行,包含在其中的对数据库的操作要么全部被执行,要么都不执行. ...

  6. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

  7. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  8. 使用Maven插件构建Spring Boot应用程序镜像

    使用Maven插件构建Spring Boot应用程序的Docker镜像. 环境准备 1.Linux系统 2.安装JDK,Maven 3.安装Docker 应用实践 1.在应用程序根目录下添加Docke ...

  9. docker 部署Spring Boot:Docker化Spring Boot应用程序

    第一章 1.创建项目存放目录 mkdir /root/sproot -p 2.准备好Spring Boot应用程序 jar 包 testrest.jar 第二章 1. 安装docker 在所有节点执行 ...

随机推荐

  1. iOS 当键盘覆盖textFiled时简单的处理方法

    //方法1--- - (void)textFieldDidBeginEditing:(UITextField *)textField { if (iPhone5) { return; } else { ...

  2. 将任意音频格式文件转换成16K采样率16bit的wav文件

    此转换需要使用ffmpeg 假设有目录 d:\录音 目录有 张三.m4a, 李四.m4a xxx.m4a(其他任意格式音频触类旁通可以把 *.m4a改成*.*).批量转换成采样率16K,有符号,16b ...

  3. 简介Python中用于处理字符串的center()方法

    简介Python中用于处理字符串的center()方法 这篇文章主要介绍了简介Python中用于处理字符串的center()方法,是Python入门中的基础知识,需要的朋友可以参考下 center() ...

  4. centos6.8 ssh 问题

    xshell用ROOT不能登录 需要把 /etc/ssh/sshd_config 中的端口新建一个 不能用默认的

  5. git命令手册

    以下内容是我在学习和研究Git时,对Git操作的特性.重点和注意事项的提取.精练和总结,可以做为Git操作的字典,方便大家查阅: 备注:本文会不断更新完善: 目录 一. 语法格式描述 二. git环境 ...

  6. Java使用Apache Commons Net实现FTP功能

    maven依赖: <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...

  7. 印象笔记·剪藏 Chrome插件

    印象笔记·剪藏 Chrome插件 链接:https://pan.baidu.com/s/10nzrSk_3sLkOI29MIEPEBw  密码:p8n8

  8. 基于MSP430G2231实现多路数据采集器

    基于MSP430G2231实现多路数据采集器 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 系列博客说明:此系列博客属于作者在大三大四阶段所储备的关于电子电 ...

  9. IBM.WMQ订阅主题,连续获取消息解决办法

    去队列里面一直获取消息,一开始想到了两种解决方案: 第一:订阅一次获取一次消息,正常的话每次都能获取到,但是要及时去清理订阅并且时间粒度不好控制 第二:订阅一次,再获取消息这里加死循环,超时MQ已经做 ...

  10. windows服务器入门 mysql的安装

    钱的问题   只能安装mysql了 下载MySQL 5.5(其他的版本都可以的  只是在这里以mysql5.5为例)安装包(下载地址:http://dev.mysql.com/downloads/my ...