目录

问题 :Unable to connect to 172.16.xx.xxx:6379

解决方法:

1.首先检查你的连接信息是否有误。主机连接地址,端口号

2.确保你的项目连接配置信息无误后。再确认虚拟机的redis有没有开启,linux下的话使用以下命令来确认

3.接着找到redis安装路径下的配置文件redis.conf,打开配置文件,执行下面操作,设置Redis可远程访问

 4.使用iptable是设置允许外部访问6379端口永久生效

5.关闭虚拟机firewalld防火墙


问题 :Unable to connect to 172.16.xx.xxx:6379

今天用SpringBoot整合redis,连接的是我本地Virtual Box上安装的CentOs7.6虚拟机,新的探索总会伴随很多的问题,你猜到了,连接不上。

 

解决方法:

1.首先检查你的连接信息是否有误。主机连接地址,端口号

SpringBoot2.x之前的版本

redis:
        database: 0
        host: 172.16.xx.xxx
        port: 6379
        jedis:
          pool:
            max-active: 100
            max-idle: 10
            max-wait: 100000
        timeout: 5000

SpringBoot2.x及之后的版本 

redis:
    # 连接的数据库索引,Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
    database: 0
    # redis服务的ip地址
    host: 172.16.xx.xxx
    # redis端口
    port: 6379
    lettuce:
      shutdown-timeout: 0
      pool:
        # 最大活跃连接
        max-active: 8
        # 最大阻塞时间
        max-wait: -1
        # 最大空闲连接
        max-idle: 8
        # 最小空闲连接
        min-idle: 0
    timeout: 10000

Redis 连接Linux redis 连接hostname can

注意:这里说明一下,SpringBoot1.4以下版本整合Redis时,使用的是“spring-boot-starter-redis”,而SpringBoot1.4以上版本使用的是“spring-boot-starter-data-redis”

2.确保你的项目连接配置信息无误后。再确认虚拟机的redis有没有开启,linux下的话使用以下命令来确认

ps -ef | grep redis

Redis 连接Linux redis 连接hostname can

如果你的redis没有开启,则请按照下面命令进行开启

  • 方法1: 

找到redis的安装目录,一般是在/usr/local下,进入到/redis/bin目录下,执行下面命令

./redis-server

  • 方法2:直接执行下面命令

service redis start 或 systemctl start redis.service

3.接着找到redis安装路径下的配置文件redis.conf,打开配置文件,执行下面操作,设置Redis可远程访问

vim redis.conf

Redis 连接Linux redis 连接hostname can

Redis 连接Linux redis 连接hostname can

然后按Esc键,输入:wq保存修改,退出

 4.使用iptable是设置允许外部访问6379端口永久生效

# 添加iptables规则
iptables -I INPUT 1 -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

# 保存
service iptables save

# 配置iptables开机自启
systemctl enable iptables.service

注意:执行保存命令的时候有时候可能会报错:

The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

这是因为没有安装iptables服务,直接使用yum安装iptables服务即可.

yum install iptables-services

 安装完成后,重新执行 service iptables save 命令即可保存成功

5.关闭虚拟机firewalld防火墙

# 停止防火墙服务
systemctl stop firewalld.service

# 禁用防火墙服务
systemctl disable firewalld.service

执行完上面的操作之后,建议重启一下虚拟机,这里推荐使用管理redis的第三方工具来测试一下是否连接成功再去SpringBoot项目里面测试,在测试连接之前,请确保本地可以ping得通虚拟机

如果执行完上面的操作仍旧不行,请自行百度或检查自己的项目配置