Redis基础入门与Linux安装教程(含Jedis操作与SpringBoot整合)
本篇文章主要介绍了在Linux系统上安装和配置Redis 3.0.0版本的方法,以及如何使用Jedis进行操作,以及Redis与SpringBoot的整合。以下将详细阐述这些知识点: 1. **Redis安装**: Redis是一款开源的内存数据结构存储系统,适合用于缓存和键值对存储。在Linux系统上安装Redis,首先需要确保具备GCC编译器。如果没有,可通过`yum install gcc-c++`进行安装。安装步骤如下: - **下载源码**:从Redis官方网站下载对应版本的tar.gz文件(如redis-3.0.0.tar.gz)。 - **解压与编译**:在终端中进入下载的源码目录,执行`tar xvf redis-3.0.0.tar.gz`,然后`cd`进解压后的目录,运行`make`进行编译。 - **安装**:设置安装路径,如`make install PREFIX=/usr/local/redis`,将Redis安装到指定目录。 2. **单机版启动与连接**: - **前台启动**:在安装目录下运行`./redis-server`。 - **后台启动**:将`redis.conf`复制到`bin`目录,并修改`daemonize`为`yes`,然后执行`./redis-server redis.conf`。使用`ps aux | grep redis`检查进程状态。 - **连接与控制台交互**:通过`redis-cli`命令行工具连接Redis,可以指定服务器地址和端口。默认连接本地6379端口,如`./redis-cli`或`./redis-cli -h 192.168.25.153 -p 6379`。 - **关闭Redis**:在命令行中输入`exit`或`quit`退出Redis客户端,也可以使用`kill`命令终止进程。 3. **使用Jedis操作Redis**: Jedis是Java语言编写的Redis客户端库,简化了Java应用程序与Redis的交互。使用Jedis,开发者可以轻松地执行各种Redis操作,如字符串操作、哈希操作、列表操作、集合操作和有序集合操作等。例如,创建连接、设置和获取键值、执行命令等。 4. **Redis与SpringBoot整合**: 在SpringBoot项目中集成Redis,可以通过添加Spring Data Redis依赖并配置Spring Boot Actuator的`redis`监控模块。开发者可以使用Spring的`@Autowired`注解注入RedisTemplate或者RedisConnectionFactory,编写业务逻辑时直接调用相关方法。此外,Spring Boot还提供了支持缓存注解如`@Cacheable`,方便在服务层自动缓存数据。 这篇文章为读者提供了一个全面的Redis基础入门指南,包括Redis的安装、配置、命令行操作,以及如何将其融入Java应用,特别是与SpringBoot框架的集成。这为开发者在实际项目中高效利用Redis作为缓存和数据存储解决方案奠定了坚实的基础。
一、redis如何安装到Linux系统上
使用的版本:redis-3.0.0.tar.gz
1.单机版
(1)安装
Redis是c语言开发的。
安装redis需要c语言的编译环境。如果没有gcc需要在线安装。yum install gcc-c++
安装步骤:
第一步:redis的源码包上传到linux系统。
第二步:解压缩redis。
第三步:编译。进入redis源码目录。make
第四步:安装。make install PREFIX=/usr/local/redis
PREFIX参数指定redis的安装目录。一般软件安装到/usr目录下
(2)启动redis并连接
>>前端启动:在redis的安装目录下直接启动redis-server
[root@localhost bin]# ./redis-server
>>后台启动:
解压后的redis文件中有个redis.conf复制到redis/bin目录下
把/root/redis-3.0.0/redis.conf复制到/usr/local/redis/bin目录下
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
修改配置文件:
把daemonize no 该为daemonize yes
[root@localhost bin]# ./redis-server redis.conf
查看redis进程:
root 5190 0.1 0.3 33936 1712 ? Ssl 18:23 0:00 ./redis-server *:6379
root 5196 0.0 0.1 4356 728 pts/0 S+ 18:24 0:00 grep redis
[root@localhost bin]#
>>连接和关闭:
[root@localhost bin]# ./redis-cli
默认连接localhost运行在6379端口的redis服务。
[root@localhost bin]# ./redis-cli -h 192.168.25.153 -p 6379
-h:连接的服务器的地址
-p:服务的端口号
关闭redis:[root@localhost bin]# ./redis-cli shutdown
开放linux 6379 端口
1.编辑 /etc/sysconfig/iptables 文件:vi /etc/sysconfig/iptables
加入内容并保存:-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 6379 -j ACCEPT
2.重启服务:/etc/init.d/iptables restart
3.查看端口是否开放:/sbin/iptables -L -n
2.集群版
二、redis五中基本数据类型
Redis中所有的数据都是字符串。
命令不区分大小写,key是区分大小写的。 Redis是单线程的。Redis中不适合保存内容大的数据。
1.String 类型
剩余22页未读,继续阅读
- 粉丝: 1192
- 资源: 94
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 李兴华Java基础教程:从入门到精通
- U盘与硬盘启动安装教程:从菜鸟到专家
- C++面试宝典:动态内存管理与继承解析
- C++ STL源码深度解析:专家级剖析与关键技术
- C/C++调用DOS命令实战指南
- 神经网络补偿的多传感器航迹融合技术
- GIS中的大地坐标系与椭球体解析
- 海思Hi3515 H.264编解码处理器用户手册
- Oracle基础练习题与解答
- 谷歌地球3D建筑筛选新流程详解
- CFO与CIO携手:数据管理与企业增值的战略
- Eclipse IDE基础教程:从入门到精通
- Shell脚本专家宝典:全面学习与资源指南
- Tomcat安装指南:附带JDK配置步骤
- NA3003A电子水准仪数据格式解析与转换研究
- 自动化专业英语词汇精华:必备术语集锦