当前位置: 首页 > news >正文

Springboot 集成 Redis集群配置公网IP连接报私网IP连接失败问题

1、问题:在Springboot 集成 Redis集群配置公网IP连接报私网IP连接失败,一直报私有IP连接失败

14 14:57:49.180  WARN 22012 --- [ioEventLoop-6-4] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6384]: connection timed out: /192.168.0.19:6384
2020-08-14 14:57:49.180  WARN 22012 --- [ioEventLoop-6-3] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6383]: connection timed out: /192.168.0.19:6383
2020-08-14 14:57:49.182  WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6382]: connection timed out: /192.168.0.19:6382
2020-08-14 14:57:49.182  WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6381]: connection timed out: /192.168.0.19:6381
2020-08-14 14:57:49.190  WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6385]: connection timed out: /192.168.0.19:6385
2020-08-14 14:57:49.191  WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6386]: connection timed out: /192.168.0.19:6386
2020-08-14 14:57:59.389  WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6382
2020-08-14 14:58:09.391  WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6381
2020-08-14 14:58:19.393  WARN 22012 --- [ioEventLoop-6-1] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6383
2020-08-14 14:58:29.396  WARN 22012 --- [ioEventLoop-6-2] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6384
2020-08-14 14:58:39.399  WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6386
2020-08-14 14:58:49.402  WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6385

2、配置文件

创建6个配置文件:redis-6381.conf,redis-6382.conf,redis-6383.conf,redis-6384.conf,redis-6385.conf,
redis-6386.conf。配置文件内容如下:

# 配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行对应修改#默认是 protected-mode yes,即开启保护模式, no=关闭
#在redis的配置文件中会遇到protected-mode,它直译为保护模式。
#如果设置为yes,那么只允许我们在本机的回环连接,其他机器无法连接。
#线上Redis服务,为了安全,我们建议将protected-mode设置为yes。
#protected-mode设置为yes的情况下,为了我们的应用服务可以正常访问Redis,我们需要设置Redis的bind参数或者密码参数#requirepass。
protected-mode yes
#端口号
port 6381
# IP绑定,redis不建议对公网开放,这里绑定了服务器私网IP及环回地址
bind 192.168.0.19 127.0.0.1
requirepass 123456
# redis数据文件存放的目录
dir /redis/workdata
# 日志文件
logfile "/redis/logs/cluster-node-6381.log"
# 开启AOF
appendonly yes
#后台启动
daemonize yes # 开启集群
cluster-enabled yes
# 集群持久化配置文件,内容包含其它节点的状态,持久化变量等,会自动生成在上面配置的dir目录下
cluster-config-file cluster-node-6381.conf
# 集群节点不可用的最大时间(毫秒),如果主节点在指定时间内不可达,那么会进行故障转移
cluster-node-timeout 5000

3、springboot集成redis集群有以下配置,二选一:

1:代码配置

@Configuration
public class RedisClusterConfig {@Beanpublic RedisConnectionFactory redisConnectionFactory() {// 客户端读写分离配置LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().readFrom(ReadFrom.REPLICA_PREFERRED).build();RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList("42.192.119.238:6381","42.192.119.238:6382","42.192.119.238:6383","42.192.119.238:6384","42.192.119.238:6385","42.192.119.238:6386"));return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);}
}

2:yml 配置

#集群模式
spring:redis:cluster:max-redirects: 3nodes:- 42.192.119.238:6381- 42.192.119.238:6382- 42.192.119.238:6383- 42.192.119.238:6384- 42.192.119.238:6385- 42.192.119.238:6386database: 0
#    host: 42.192.119.238
#    port: 6380password: timeout: 5000s #连接超时时长# 连接池最大连接数(使用负值表示没有限制)jedis:pool:max-active: 8 #连接池最大连接数量,负数表示无限,默认为8max-idle: 8 #连接池最大空闲数量,默认8min-idle: 0 #连接池最小空闲数量,默认0

4、解决链接报错问题

让Redis暴露公网IP其实在redis.conf配置文件里是能找到的,这里我们可以手动指定Redis的公网IP、端口以及总线端口(默认服务端口加10000)。
手动指定了公网ip后,Redis集群中的节点会通过公网IP进行通信,也就是外网访问。因此相关的总线端口,如下面的16381等总线端口必须在云服务器中的安全组中放开

# 配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行对应修改#默认是 protected-mode yes,即开启保护模式, no=关闭
#在redis的配置文件中会遇到protected-mode,它直译为保护模式。
#如果设置为yes,那么只允许我们在本机的回环连接,其他机器无法连接。
#线上Redis服务,为了安全,我们建议将protected-mode设置为yes。
#protected-mode设置为yes的情况下,为了我们的应用服务可以正常访问Redis,我们需要设置Redis的bind参数或者密码参数#requirepass。
protected-mode yes
#端口号
port 6381
# IP绑定,redis不建议对公网开放,这里绑定了服务器私网IP及环回地址
bind 192.168.0.19 127.0.0.1
requirepass 123456
# redis数据文件存放的目录
dir /redis/workdata
# 日志文件
logfile "/redis/logs/cluster-node-6381.log"
# 开启AOF
appendonly yes
#后台启动
daemonize yes # 开启集群
cluster-enabled yes
# 集群持久化配置文件,内容包含其它节点的状态,持久化变量等,会自动生成在上面配置的dir目录下
cluster-config-file cluster-node-6381.conf
# 集群节点不可用的最大时间(毫秒),如果主节点在指定时间内不可达,那么会进行故障转移
cluster-node-timeout 5000# 云服务器上部署需指定公网ip
cluster-announce-ip 42.192.119.238
# Redis总线端口,用于与其它节点通信
cluster-announce-bus-port 16381

根据以上配置修改每一个redis节点的配置,注意端口不能相同

在src 目录下执行命令:

./redis-cli -c -p 6381 -h 192.168.0.19 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
6297ab04ff4bbfcad778b80315619defc2f6e513 42.192.119.238:6382@16382 master - 0 1696924087000 2 connected 5461-10922
19d3955d4bacd04892c13e7a05e13c7744085896 42.192.119.238:6381@16381 myself,master - 0 1696924087000 1 connected 0-5460
3a21d6c05255229741593340a781affbdcad6236 42.192.119.238:6385@16385 slave df0858e942b03f5b3c848d1acb2a4fde1f70e290 0 1696924088000 3 connected
546c8528a07abee29a1e383a3130d4f306447f0e 42.192.119.238:6386@16386 slave 19d3955d4bacd04892c13e7a05e13c7744085896 0 1696924086000 1 connected
56b730c5631515b2359bbf9b6d4306460da8502c 42.192.119.238:6384@16384 slave 6297ab04ff4bbfcad778b80315619defc2f6e513 0 1696924088460 2 connected
df0858e942b03f5b3c848d1acb2a4fde1f70e290 42.192.119.238:6383@16383 master - 0 1696924089469 3 connected 10923-16383

可以发现,各节点暴露的IP全是公网IP了。

5、故障转移期间Lettuce客户端连接问题

解决方式:
1、yml指定使用jedis

2、代码配置
1)、更换Redis客户端

@Configuration
public class RedisClusterConfig {@Beanpublic RedisConnectionFactory redisConnectionFactory() {RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList("42.192.119.238:6381","42.192.119.238:6382","42.192.119.238:6383","42.192.119.238:6384","42.192.119.238:6385","42.192.119.238:6386"));return new JedisConnectionFactory(redisClusterConfiguration);}
}

参考链接:

https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster
https://github.com/lettuce-io/lettuce-core/wiki/Client-options#cluster-specific-options

2)、更改实现配置

@Configuration
public class RedisClusterConfig {@Beanpublic RedisConnectionFactory redisConnectionFactory() {// 开启自适应集群拓扑刷新和周期拓扑刷新,不开启相应槽位主节点挂掉会出现服务不可用,直到挂掉节点重新恢复ClusterTopologyRefreshOptions clusterTopologyRefreshOptions =  ClusterTopologyRefreshOptions.builder().enableAllAdaptiveRefreshTriggers() // 开启自适应刷新,自适应刷新不开启,Redis集群变更时将会导致连接异常.adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) //自适应刷新超时时间(默认30秒),默认关闭开启后时间为30秒.enablePeriodicRefresh(Duration.ofSeconds(20))  // 默认关闭开启后时间为60秒 .build();ClientOptions clientOptions = ClusterClientOptions.builder().topologyRefreshOptions(clusterTopologyRefreshOptions).build();// 客户端读写分离配置LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().clientOptions(clientOptions).build();RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList("42.192.119.238:6381","42.192.119.238:6382","42.192.119.238:6383","42.192.119.238:6384","42.192.119.238:6385","42.192.119.238:6386"));return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);}
}

参考文档:https://developer.aliyun.com/article/1167633

http://www.lryc.cn/news/190216.html

相关文章:

  • 解决方案 | 法大大电子签精准击破销售场景签约难题
  • ARM按键中断控制事件
  • 微信小程序之本地生活(九宫格)
  • 【Linux 安装Kibana 及 Es 分词器安装】
  • python-arima模型statsmodels库实现-有数据集(续)-statsmodels-0.9.0版本
  • JVM源码剖析之线程的创建过程
  • ansible的介绍安装与模块
  • el-form简单封装一个列表页中的搜索栏
  • 【Python 2】列表 模式匹配 循环 dict set 可变对象与不可变对象
  • 深度学习—cv动物/植物数据集
  • 高效团队协作软件推荐:提升工作效率的优选方案!
  • Mac中使用virtualenv和virtualenvwrapper
  • wpf webBrowser控件 常用的函数和内存泄漏问题
  • AI游戏设计的半年度复盘;大模型+智能音箱再起波澜;昇思大模型技术公开课第2期;出海注册经验分享;如何使用LoRA微调Llama 2 | ShowMeAI日报
  • 多线程 - 锁策略 CAS
  • VP记录——The 2021 CCPC Weihai Onsite
  • JavaWeb---Servlet
  • 英语——方法篇——单词——谐音法+拼音法——50个单词记忆
  • 35道Rust面试题
  • 01 时钟配置初始化,debug
  • Halcon我的基础教程(一)(我的菜鸟教程笔记)-halcon仿射变换(Affine Transformation)的探究与学习
  • c++视觉---中值滤波处理
  • Edge使用猴油脚本实战(实验室安全考试系统刷在线时长——网站永久自动刷新)
  • Vue 中 KeepAlive 内置缓存使用
  • 语言模型编码中/英文句子格式详解
  • 【Node.js】路由
  • matlab 2ask 4ask 信号调制
  • Python利用jieba分词提取字符串中的省市区(字符串无规则)
  • MuLogin防关联浏览器帮您一键实现Facebook账号多开
  • 【C语言】每日一题(半月斩)——day4