SpringBoot 2.1 远程连接阿里云Redis服务器

在学习SpringBoot继承Redis的时候,在阿里云上装了Redis,却发现本地的程序始终连不上Redis,会报以下错误

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to ****.****.****:****:6379

以下是我的配置

Spring Boot 配置

application.properties

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

redis服务器配置 Ubuntu

  1. 打开端口6379

    1. 查看端口 netstat -tlpn
    2. 开启端口 iptables -A INPUT -p tcp --dport 6379 -j ACCEPT
  2. 修改redis配置文件

    1. vim redis.conf
    2. 找到 bind127.0.0.1 这一行,并注释掉
    3. 找到 protected-mode yes 改为 protected-mode no
    4. [可选] 安全起见设置一下密码 requirepass [自己的密码]
    5. 保存之后重启redis

以上为止都是根据网上的教程进行配置,但是结果发现始终连不上服务器,依旧提示错误↓

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 101.200.162.129:6379

	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1092)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1065)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:865)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:340)
	at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
	......
	
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 101.***.***.***:6379
	at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
	at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
	at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:234)

之后想到可能是阿里云安全组阻止了访问,于是又对安全组添加了规则

阿里云安全组设置

  1. 进入安全组规则
    在这里插入图片描述
  2. 设置端口在这里插入图片描述

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值