随笔 - 607,  文章 - 173,  评论 - 2,  阅读 - 21万

 


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服务掉了

 

posted on   菜鸟乙  阅读(39)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示

目录导航