一、场景
Springboot集成Redis,连接不上Redis

二、报错异常信息

java.nio.channels.ClosedChannelException: null

io.lettuce.core.RedisConnectionException: Unable to connect [Redis IP地址]

三、解决方案
1、导入jar包注意,版本高的要用data-redis

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2、Springboot的yml配置文件注意
(1)Redis没配置密码,不要在yml里配置password;
(2)timeout不能为0。

spring:  
  redis:
    database: 0
    port: 6379
    host: x.x.x.x
    password: your_password
    timeout: 5000

3、Redis的配置更改
找到redis的配置文件 redis.conf并编辑。

vi redis.conf

(1)protected-mode 设置成no
默认是设置成yes的, 防止了远程访问,在redis3.2.3版本后。

protected-mode no

(2)bind 127.0.0.1 这一行给注释掉
这里的bind指的是只有指定的网段才能远程访问这个redis,注释掉后,就没有这个限制了。

#bind 127.0.0.1

(3)修改Redis默认密码

# requirepass foobared
requirepass your_password

4、 开放Linux的6379端口

#这里用的是firewall防火墙配置,开放端口并重启
firewall-cmd --zone=public --add-port=6379/tcp --permanent 

firewall-cmd --reload

如果是云服务器,配置安全组开放6379端口

更多推荐

Redis连接不上的报错解决方案汇总