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

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

Spring Boot 2.0中,默认使用Lettuce客户端来连接Redis服务。默认情况下,不使用连接池,只有在配置`redis.lettuce.pool`属性后才能使用Redis连接池。 使用连接池可以提升性能。例如,在插入1万条数据时,如果配置了连接池,执行速度会比没有配置连接池快很多[2]。 在Spring Boot中,使用Lettuce连接池时,可以配置连接池的大小。比如,如果当前连接池中已经有3个连接,再加上当前查询窗口的连接,一共就是4个socket连接。通过配置连接池大小,可以控制同时处理的连接数,以及减少与Redis服务器的重复建立和断开连接的开销。 综上所述,Spring Boot整合Redis连接池可以通过配置`redis.lettuce.pool`属性来启用,并且可以提升性能和优化连接管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot 配置 Redis 连接池](https://blog.csdn.net/web18334137065/article/details/126114299)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [springboot2整合redis使用lettuce连接池(解决lettuce连接池无效问题)](https://blog.csdn.net/qq_41921994/article/details/109627736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值