项目中配置了 redis,启动项目报错:Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.17.128:6379
从以下几个方面入手,查找问题:
1、防火墙没有开放端口
执行 firewall-cmd --list-port 命令,查看服务器防火墙开放端口列表。如果没有redis端口,那么依次执行下面的添加防火墙开放端口和重新加载防火墙命令(以redis默认端口6379为例)。
# 查询防火墙开放端口列表
firewall-cmd --list-port
# 添加防火墙开放端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 重新加载防火墙
firewall-cmd --reload
# 查询防火墙开放端口列表
firewall-cmd --list-port
2、修改 redis 配置文件
首先编辑 redis 配置:/usr/local/redis/etc/redis.conf
- protected-mode yes 改为 protected-mode no
- bin 127.0.0.1 改为 bin 0.0.0.0
- timeout 改为大于0
修改配置文件保存后需要重启redis:
如果是用apt-get或者yum install安装的redis,可以直接通过下面的命令停止/启动/重启redis:
/etc/init.d/redis-server stop
/etc/init.d/redis-server start
如果是根据压缩包安装:
1. redis 关闭
无密码:redis-cli -h 127.0.0.1 -p 6379 shutdown
有密码:redis-cli -h 127.0.0.1 -p 6379 -a password shutdown
2. redis 启动
注意,需要进入到 /usr/local/redis/bin 文件夹执行下面的命令。
./redis-server …/redis.conf
3、查看是否启动了 redis 服务
执行查询 redis 进程命令,出现下面的 redis-server 0.0.0.0:6379 代表成功执行
# 查询 redis 进程
ps -ef | grep redis
root xxxx x x xxxx xxxx redis-server 0.0.0.0:6379
4、项目中 redis 配置文件
一般是在 application.yml 这种配置文件中配置 redis 服务,保证 redis 配置的信息 host、port、password 没有问题。
spring.redis.timeout 连接超时时间(毫秒)中不能设置为 0,一般设置 5000 。