springboot整合redis详细教程
spring boot 整合 redis 详细教程
注意:本次实验环境 是将 redis 安装在 CentOS7.x-86_x64 中 教程中用到的软件(VMware Workstation Pro、postman、idea、Another Redis Desktop Manager、FinalShell(xshell也可)、DBeaver(数据库客户端就行))
一、下载并解压Redis
1、执行下面的命令下载redis:
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
2、解压redis:
tar xzf redis-6.2.6.tar.gz
3、移动redis目录,一般都会将redis目录放置到 /usr/local/redis目录:
mv redis-6.2.6 /usr/local/redis
二、编译并安装redis
1、进入redis安装目录,执行make命令编译redis:
cd /usr/local/redismake
等待make命令执行完成即可。
如果执行make命令报错:cc 未找到命令,原因是虚拟机系统中缺少gcc,执行下面命令安装gcc:
yum -y install gcc automake autoconf libtool make
如果执行make命令报错:致命错误:jemalloc/jemalloc.h: 没有那个文件或目录,则需要在make指定分配器为libc。执行下面命令即可正常编译:
make MALLOC=libc
make命令执行完,redis就编译完成了。
2、执行下面命令安装redis,并指定安装目录
make install PREFIX=/usr/local/redis
至此,redis即安装成功。
三、启动redis
1、进入redis安装目录,执行下面命令启动redis服务
./bin/redis-server redis.conf
此时,可以看到redis服务被成功启动:
但这种启动方式不能退出控制台,如果退出,那么redis服务也会停止。如果想要redis以后台方式运行,需要修改redis的配置文件:redis.conf。将该配置文件中的daemonize no改为daemonize yes即可:
修改完成后,重新执行启动命令启动redis,然后通过下面命令查看redis进程,可以发现redis服务已经被启动了:
ps -ef | grep redis
如图所示:
2、通过redis-cli测试redis是否可用,在redis安装目录执行下面命令:
在redis安装目录下执行命令
./bin/redis-cli
此处我们通过下面命令随便set一个字符串类型的值,key是test,value是hello:
set test hello
然后通过下面命令get出test这个key的value值:
get test
测试没有问题,至此,redis在我们的Linux服务器上就已经安装完成了。
3、通过 Another Redis Desktop Manager 客户端连接 redis,执行下面命令:
3.1、输入连接信息 (连接前 接的关闭防火墙)
3.2 测试命令
三、spring boot整合redis
1、在 pom.xml 中引入redis依赖
<!--redis--><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>3.0.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.11.1</version></dependency>
2、在 application.yml 中配置连接
server:port: 8084spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.159.130:3306/schoolusername: rootpassword: H@ppy2022#上面是数据库连接信息redis:database: 0 #redis的0号数据库host: 192.168.159.130port: 6379password: 123456jedis:pool:max-active: 5000max-wait: -1min-idle: 1max-idle: 100#上面是redis连接信息
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.slf4j.Slf4jImplglobal-config:db-config:logic-delete-field: delFlagmapper-locations: classpath:mapper/*.xml
3、创建实体类
@Data
@ApiModel(value = "用户实体类",description = "")
@TableName("user")
public class UserModel implements Serializable {/*** 序号ID*/@ApiModelProperty(name = "序号ID", notes = "")@TableId(type = IdType.AUTO)private Integer cosId;/*** 用户账号*/@ApiModelProperty(name = "用户账号", notes = "")private String cosUser;/*** 用户密码*/@ApiModelProperty(name = "用户密码", notes = "")private String cosPassword;
}
4、在数据库中建表
5、创建 controller
import org.springframework.data.redis.core.RedisTemplate;
@RestController
@RequestMapping("user")
@Api(tags = "资源查询类接口")
@Slf4j
public class UserController {@Resourceprivate UserService userService;//这是redis的jar中的@Resourceprivate RedisTemplate redisTemplate;@PostMapping(value = "/set")@ApiOperation(value = "redis set")public void set(@RequestBody UserModel userModel){//用 json格式 传 userModel 实例对象 具体看postman 测试redisTemplate.opsForValue().set("user",userModel,3600, TimeUnit.SECONDS);log.info("redis 存入 启动");}@GetMapping(value = "/get/{key}")@ApiOperation(value = "redis get")public UserModel getData(@PathVariable("key") String key ) {log.info("redis 取出 启动");UserModel userModel=(UserModel)redisTemplate.opsForValue().get(key);System.out.println(userModel);return (UserModel)redisTemplate.opsForValue().get(key);}@DeleteMapping("/delete/{key}")public boolean delete(@PathVariable("key") String key){//true = 失败 false = 成功redisTemplate.delete(key);return redisTemplate.hasKey(key);}
四、用postman进行redis的存取测试
1、查看客户端的可key情况
可以看到暂时是没有key的
2、现在用postman存一个key
如图所示:调用接口成功
3、查看客户端key情况
如图所示:可以看到存入了 一个key 且可以看到存入的过期时间
4、用postman查看key情况
如图所示:可以看到postman获取了 一个key
五、过程中问题解决
1、 客户端无法远程连接Redis的解决方法
cd /usr/local/redis#进入redis配置文件 进行编辑 vi redis.conf
如下
1、bind 127.0.0.1改为 bind 0.0.0.0127.0.0.1: 代表本地地址,访问redis服务只能通过本机的客户端连接,而无法通过远程连接0.0.0.0: 接受所有来自于可用网络接口的连接2、protected-mode yes 改为 protected-mode noyes: 保护模式,只允许本地链接no: 保护模式关闭3、daemonize no: 当前界面将进入redis的命令行界面,exit强制退出或者关闭连接工具(xshell等)都会导致redis进程退出注意: 启动redis 一定要指定配置文件,否则配置文件不生效的./src/redis-server redis.conf
2、redis的启停及操作
#查看redis状态ps -aux | grep redis1、命令启动redis服务在 /usr/local/redis 目录下执行./bin/redis-server redis.conf2、执行redis命令./bin/redis-cli认证auth password #1234563、关闭redis服务shutdown4、操作存一个keyset key value取一个keyget key5、redis 密码#设置密码config set requirepass 123456#查看密码config get requirepass6、查询所有keykeys *退出客户端ctrl + c5、下面是red hat/CentOs7关闭防火墙的命令!1:查看防火状态systemctl status firewalld2:暂时关闭防火墙systemctl stop firewalld3:永久关闭防火墙systemctl disable firewalld4:重启防火墙systemctl enable firewalld
6
#查看密码config get requirepass6、查询所有keykeys *退出客户端ctrl + c5、下面是red hat/CentOs7关闭防火墙的命令!1:查看防火状态systemctl status firewalld2:暂时关闭防火墙systemctl stop firewalld3:永久关闭防火墙systemctl disable firewalld4:重启防火墙systemctl enable firewalld
springboot整合redis详细教程相关推荐
- springboot整合mysql5.7_详解SpringBoot整合MyBatis详细教程
1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver 然后导入以下整合依赖 org.mybatis.spring.boo ...
- SpringBoot整合MyBatis详细教程~
目录 1. 导入依赖 2. 连接数据库 3. 编写数据库配置信息 4. 编写pojo实体类 5. 编写mapper接口 6. 编写mapper.xml 7. 编写controller 8. 测试 1. ...
- 【七】springboot整合redis(超详细)
springboot篇章整体栏目: [一]springboot整合swagger(超详细 [二]springboot整合swagger(自定义)(超详细) [三]springboot整合token(超 ...
- SpringBoot整合Quartz--使用/教程/实例
原文网址:SpringBoot整合Quartz--使用/教程/实例_IT利刃出鞘的博客-CSDN博客 简介 说明 本文用实例介绍quartz的用法. quartz与spring自带的定时最大的区别之处 ...
- SpringBoot整合Redis+mybatis,封装RedisUtils工具类等实战(附源码)
点击上方蓝色字体,选择"标星公众号" 优质文章,第一时间送达 关注公众号后台回复pay或mall获取实战项目资料+视频 作者:陈彦斌 cnblogs.com/chenyanbin/ ...
- Springboot整合Rocketmq系列教程
Springboot整合Rocketmq系列教程 本教程是基于Springboot2.6.3整合Rocketmq5.0,其中涉及了Rocketmq的安装,消息的发送及消费的代码实现. 本文不会对roc ...
- SpringBoot 整合 Redis 使用详解(StringRedisTemplate 和 RedisTemplate 对比分析)
背景: 最近项目采用前后端分离的架构,单点登录系统采用Redis存储用户session信息,在这里总结下springboot整合redis的详细过程,以及部分源码分析 1.前期准备 首先保证安装好re ...
- SpringBoot第九篇: springboot整合Redis
这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis. 引入依赖: 在pom文件中添加redis依赖: <dependen ...
- Springboot整合redis(lettuce)
springboot 整合redis(lettuce) 首先确保电脑上装了redis.最好能用redisDesktop查看一下数据情况 redis是一款非常流行的Nosql数据库.redis的功能非常 ...
最新文章
- flask执行python程序_Flask app后如何执行代码(应用程序运行)开始
- AAAI 2020 | 时间可以是二维的吗?基于二维时间图的视频内容片段检测
- IBM中国研究院被曝已全面关闭
- 做好技术管理,你必须要跨越的4道槛
- SAP SD基础知识之输出控制(Output Control)
- 谜题81:烧焦到无法识别
- 叫我如何相信你?聊一聊语言模型的校准
- 电脑账户头像怎么删掉_情侣头像丨情侣头像一男一女背影
- python六角形的绘制 编程_利用Python的turtle重复画六边形
- python能写什么脚本_你用 Python 写过哪些有趣的脚本?
- 清理临时目录mysql,把MySQL的临时目录迁移到内存上-临时文件夹
- elasticsearch集群搭建-windows
- C# Winform 窗体美化(八、Icon)
- jQuery的实现,去掉传入html代码两端的空格:
- UI设计中常见插画应用素材,拿来就可以用!
- Java开发者需要掌握的基础知识
- React Native初探
- bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍
- 微信端打开页面,布局乱了
- 整型数组 java_java创建一个整型数组,数组的大小由用户输入?
热门文章
- Excel导入SQL server数据库e日期格式转换失败
- C语言中getch()与getchar()
- 家乡景区介绍网页模板
- jira与confluence的无缝链接
- 电路仿真软件详谈(26),基于电路仿真软件proteus的流水灯设计
- IBM SPSS Statistics 26 for mac统计分析spss软件安装包下载安装教程Mac系统
- python实现中文繁体和中文简体之间的相互转换的代码
- ArcGIS中将单一图层中多个要素合并
- Python爬取网易云音乐1万条评论
- 基于lbp算法的特征提取 表情识别和疲劳监测系统 matlab