SpringBoot连接不上redis解决方案。

今天使用springboot的时候,想要整合redis做下测试,却怎么都连不上redis。要不就是拒绝访问,要不就是直接连接不上(具体的错误截图忘了截)但只要是这两种原因,那么下边的解决方案应该是都可以解决。下边是针对这两种问题的解决方案。

❤首先第一步要确认redis有没有开启,如果安装在linux下的话使用ps -ef | grep redis来确认

在这里插入图片描述
图片显示表示已经开启,如果没有开启的话。按照下边命令操作开启redis。

在这里插入图片描述

❤接着找到redis的配置文件redis.conf。

直接进入redis在linux下的安装目录,一般默认安装在/usr/local下

cd /usr/local/redis

直接进入配置文件:

vim redis.conf

找到 protected-mode no,修改成yes

protected-mode  no  →  protected-mode  yes
注释掉bin 127.0.0.1  →  # 127.0.0.1

然后保存退出,在src文件夹下输出命令

./redis-server ../redis.conf来使用redis配置开启redis,说白了就是让配置生效

❤排除我们的springboot 的配置文件application.properties没有问题。

这边尤其要注意的是你的redis的服务器地址的配置,如果你redis安装在虚拟机里就是你虚拟机的ipv4地址,安装在主机下就是主机的地址,这个填错肯定100%连不了。

#reids的连接信息
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.0.103
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000

如果上边三步都做了还是不行的话,那么就尝试关闭虚拟机的防火墙,或者在防火墙中添加放行的端口。因为关闭防火墙就可以进行外部访问,不受端口限制。如果个人电脑可以直接关闭虚拟机的防火墙,也可以开启防火墙,放行部分端口,具体操作如下

永久有效

开启: chkconfig iptables on 
关闭: chkconfig iptables off

即刻生效

开启: service iptables start 
关闭: service iptables stop 

放行部分端口的操作如下:

vim /etc/sysconfig/iptables 进入配置文件

添加想要开启的端口

-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8082 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

service iptables restart 

这三步是针对springboot使用redis时,连接不上和拒绝访问的解决方案。其他错误可以自行百度。

你可能感兴趣的:(学习路上遇错的解决方案,IT小白的学习之路,linux)