1 集成redis
1.1 redis安装
- 安装gcc
yum install gcc-c++
一直y,到complete!
- 安装redis
windows下载的redis使用 fileZilla 上传到linux服务器 root路径下。我使用的是3.0.7.
tar -zxvf redis-3.0.7.tar.gz 进行解压
进入redis文件夹 cd redis-3.0.7
进行编译 make
进行安装 make PREFIX=/usr/local/redis install ,使用这个命令进行安装,安装的目录是/usr/local/redis
接下来,进入到已经被解压的Redis目录 root 下 cd /root,把目录下的Redis.conf 文件移动到 /usr/local/redis 目录下 cp redis.conf /usr/local/redis
- 启动redis
后端启动,需要修改配置文件
进入指定目录 cd /usr/local/redis
修改配置文件 vim redis.conf
将 daemonize no 改为 daemonize yes 后保存退出
后端执行启动redis命令 ./bin/redis-server ./redis.conf
- 停止redis
./bin/redis-cli shutdown
- 验证是否启动成功
ps -ef | grep -i redis 看看有没有6379的端口
- 使用redis命令
进入redis 命令模式(执行客户端):./bin/redis-cli
使用 set name helloworld ,就可以插入值
使用get name 查看
del删除
keys * 查看所有的key
参考博客:
https://blog.csdn.net/qq_37789071/article/details/82837024
参考课程:
https://www.imooc.com/video/14926
- 另外一种安装方式
下载地址官网 https://redis.io/download 安装准备: (1)VMware Workation虚拟机 (2)Linux(CentOS)系统 (3)SecureCRT(Xshell也行) 安装过程: (1)安装编译器:yum install gcc-c++ (需要先安装C++编译器,redis 使用c++写的) (2)下载Redis源码并上传到服务器 (3)解压Redis压缩包:tar -zxvf *redis* (4)进入Redis目录进行编译:make (2/3/4步)或者直接在Linux上($符不用输入) $ wget http://download.redis.io/releases/redis-3.2.9.tar.gz (下载) $ tar xzf redis-3.2.9.tar.gz (解压) $ cd redis-3.2.9 $ make (编译) 编译后是二进制文件仅在目录中 src 可用。运行Redis: 要想更好的使用还需 (5)安装Redis:make PREFIX=/user/local/redis install (6)将redis.conf拷贝到Redis安装目录:cp redis.conf /user/local/redis (7)进入安装目录,更改redis.conf文件:vim redis.conf --> daemonize no 改为 yes (之后可以后台模式运行) (vi 下按i 进行编辑 按esc后shift+zz(或者直接l俩大写Z),或者:wq 保存并退出) (8)启动redis后端模式:./bin/redis-server ./redis.conf (9)终止redis的操作 : ./bin/redis-cli shutdown 上述命令输入时遇到错误比如-bash: wget: command not found 是你电脑上没装这个功能,百度装一下就好了 这个问题 : yum -y install wget 就OK了
1.2 依赖引入jar包,但是注解无法导入
pom.xml 配置文件格式书写不正确
<!-- 标识springcloud版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2 redis的使用
2.1 redis demo
- pom引入依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
- redis测试类
public static void main(String[] args) throws InterruptedException { //我这里用的是Jedis库 Jedis jedis=new Jedis("172.25.104.145",6379); jedis.set("name","doudou"); System.out.println(jedis.get("name")); jedis.close(); }
- 执行结果
doudou
Process finished with exit code 0
2.1 redis demo 连接池 超时demo
- 测试类:
/** * 使用连接池获取jedis对象 * @author weidoudou * @date 2023/6/19 8:26 * @return void **/ @Test public void test3(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(30); //设置最大连接数 config.setMaxIdle(10); //设置最大空闲连接数 JedisPool pool = new JedisPool(config,"172.25.104.145",6379,1); Jedis jedis = null; try { jedis = pool.getResource(); jedis.set("name","doudou"); jedis.expire("name",1);//设置超时时间 为 1s System.out.println(jedis.get("name")); Thread.sleep(1000*2);//线程沉睡2s System.out.println(jedis.get("name")); } catch (Exception e) { e.printStackTrace(); } finally { if(jedis!=null){ jedis.close(); } if(pool!=null){ pool.close(); } } }
- 测试结果:
doudou null Process finished with exit code 0
3 常见问题
3.1 Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnect
最后发现是在application.properites的redis配置中的spring.redis.timeout中连接超时时间(毫秒)中时间设置不能为0
spring-boot整合redis过程中的坑
当使用spring-boot 1.4一下的版本的时候redis可以使用:spring-boot-starter-redis
使用1.4以上的版本的时候需要使用spring-boot-starter-data-redis
https://www.cnblogs.com/jinlin/p/9151597.html
3.2 Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.52:6379
两种可能原因
a redis.conf配置文件applications.properties中 bind 127.0.0.1生效,因此只能本机访问redis,
b redis服务掉了