springboot整合redis连接不上,报RedisConnectionFailureException异常解决方案

springboot整合reids:

redis是安装在阿里云的云服务器上,
现通过本地java客户端springboot整合redis,
测试连接报错

以下是我的相关依赖以及springboot配置:

引入的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.properties配置:

spring.redis.host=47.98.125.20
spring.redis.port=6379

测试类:

@Test
    void redis(){
        redisTemplate.opsForValue().set("name","this is my redis and springboot");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }

执行结果,报错:


org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 47.98.125.20:6379

原因分析:

很明显,连接异常,无法连接,接下来找错误原因。


解决步骤:

  1. 依赖是否引入,application.properites配置是否写正确(经检查确认没问题)

  2. 阿里云安全组规则是否开发3679端口(经检查,已开放)
    在这里插入图片描述

  3. 查看redis.config(vim redis.config)

  • 找到bind 127.0.0.1,把它进行注释(把绑定本地ip给注释)
  • 找到protected-mode yes 把它改成no(允许非本地客户端连接)
  • 是在这里插入图片描述
  • 在这里插入图片描述
  1. 开启防火墙,过滤6379端口
 systemctl status firewall		#查看防火墙状态
 systemctl start firewalld.service		#开启防火墙
 firewall-cmd --zone=public --add-port=6379/tcp --permanent		#开启端口
 firewall-cmd --reload 			#重启防火墙
  1. 重启阿里云服务,重启redis,重新run。
 [root@iZbp17dyjyf6pfutv2rzikZ bin]# redis-server myconfig/redis.conf

在这里插入图片描述
连接成功…


版权声明:本文为weixin_42355715原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。