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

    科技2022-07-10  126

    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

    原因分析:

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


    解决步骤:

    依赖是否引入,application.properites配置是否写正确,经检查确认无误

    阿里云安全组规则是否开发3679端口,经检查,已开放

    查看redis.config(vim redis.config)

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

    好起来了

    Processed: 0.012, SQL: 8