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

SpringBoot Data Redis连接Redis-Cluster集群

使用SpringBoot Data Redis无法连接Redis-Cluster集群

最近在研究系统高并发下的缓存架构,因此自己在自己买的云服务器上搭建好Redis 5.0 版本的集群后,使用springboot的 RedisTemplate连接是发现总是访问不到集群节点。上网百度了发现没有好的解决办法,没办法只好自己debug。

RedisTemplat封装Lettuce来实现Redis操作

在Spring boot 2.X 之后,SpringBoot 将默认的客户端换成了Lettuce。对于这个客户端的好处,大家可以自行查找。

Lettuce 初始化连接池时,无法连接到Redis-Cluster集群

在使用Jedis创建Redis-Cluster集群连接时,没有任何问题,但是使用了Lettuce客户端创建,就会出现连接超时的问题,具体配置如下:

application.jpg

但是当我启动程序时,Redis客户端访问的IP:PORT 与我在如上图所示配置的IP和端口不一致。这就让我很无奈。于是只好针对日志打印的类去查找相关源码。

最后查找到了这个方法io.lettuce.core.cluster.topology.NodeTopologyView#getOwnPartition

    RedisClusterNode getOwnPartition() {for (RedisClusterNode partition : partitions) {if (partition.is(RedisClusterNode.NodeFlag.MYSELF)) {return partition;}}

在走到这段代码的时候,他会将当前Redis服务器对外的互联网及IP转变为 本机IP。 知道这个问题就好办了。通过查看redis.conf配置文件,我发现了这么一段内容。

########################## CLUSTER DOCKER/NAT support  ######################### In certain deployments, Redis Cluster nodes address discovery fails, because
# addresses are NAT-ted or because ports are forwarded (the typical case is
# Docker and other containers).
#
# In order to make Redis Cluster working in such environments, a static
# configuration where each node knows its public address is needed. The
# following two options are used for this scope, and are:
#
# * cluster-announce-ip
# * cluster-announce-port
# * cluster-announce-bus-port
#
# Each instruct the node about its address, client port, and cluster message
# bus port. The information is then published in the header of the bus packets
# so that other nodes will be able to correctly map the address of the node
# publishing the information.
#
# If the above options are not used, the normal Redis Cluster auto-detection
# will be used instead.
#
# Note that when remapped, the bus port may not be at the fixed offset of
# clients port + 10000, so you can specify any port and bus-port depending
# on how they get remapped. If the bus-port is not set, a fixed offset of
# 10000 will be used as usually.
#
# Example:
#
#cluster-announce-ip 127.0.01
#cluster-announce-port 7001
#cluster-announce-bus-port 6380

我们通过上面的英文解读可以知道这段配置的大致内容

在某些部署中,Redis 群集节点地址发现失败,因为地址是 NAT-ted 或者因为端口被转发(典型的情况是Docker 和其他集装箱)为了让 Redis 集群在这样的环境中工作,静态配置,每个节点都知道它的公共地址是必需的。

我们可以知道 以下这几个配置 是可以静态配置,保证Redis集群工作的时候,可以发现需要的公共地址(这里就是我所配置的外网地址)

因此我将以上两个配置取消了注释,并修改为我对外访问的集群 IP和PORT

cluster-announce-ip 对外访问IP
cluster-announce-port 对外端口

修改完之后,重启集群再启动SpringBoot 程序即可。

小结

有些网上的解决方法,不一定是我们真正需要的,凡是还是要自己去探索和动脑。

最后编辑于:2024-11-11 21:08:21


喜欢的朋友记得点赞、收藏、关注哦!!!

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

相关文章:

  • 计算机网络——TCP篇
  • 【网络安全面经】技术性问题3
  • 前后端交互之动态列
  • 递归(3)----力扣40组合数2,力扣473火柴拼正方形
  • 十一:HTTP 状态码详解:解读每一个响应背后的意义
  • 《译文》2024年11月数维杯国际大学生数学建模挑战赛题目
  • shell命令统计文件行数之和
  • 第02章 CentOS基本操作
  • 241113.学习日志——[CSDIY] [ByteDance] 后端训练营 [02]
  • 【HOT100第三天】和为K的子数组,最大子数组和,合并区间,轮转数组
  • 设计模式-Adapter(适配器模式)GO语言版本
  • SAM_Med2D 训练完成后boxes_prompt没有生成mask的问题
  • 游戏引擎学习第18天
  • Kotlin return与return@forEachIndexed
  • 基于Canny边缘检测和轮廓检测
  • 力扣题目解析--合并k个升序链表
  • Linux:调试器-gdb/cgdb
  • 『VUE』30. 生命周期的介绍(详细图文注释)
  • Python 人脸检测:使用 Dlib 和 OpenCV
  • 【大数据学习 | flume】flume的概述与组件的介绍
  • torch.is_storage()
  • 2411rust,编译时自动检查配置
  • 在 Ubuntu 中用 VSCode 配置 C 语言项目的编译与调试(详解教程)
  • MATLAB绘制克莱因瓶
  • HTML5实现趣味飞船捡金币小游戏(附源码)
  • Excel表数学于三角函数、统计函数
  • 小试银河麒麟系统OCR软件
  • Dubbo RPC线程模型
  • 三角波生成函数
  • 使用Python实现对接Hadoop集群(通过Hive)并提供API接口