惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 意犹未尽

VsCode AI生态开源项目 jvm堆外内存-direct buffer spring-boot-actuator-Health原理 服务治理 - 意犹未尽 概念、架构、协议格式到裸协议实现,彻底搞懂 MCP 的本质 maven-antrun-plugin插件 Spring AI-MCP源码整理 java响应式编程基础 压测实践案例之网关 elasticseach-分页搜索 sentinel增加ip来源限流后占用服务高内存问题分析 spring-boot-actuator - 意犹未尽 elasticseach-head插件安装及使用 设计模式之美学习-代码命名规范 ES-Client-api-easy es sentinel-ProcessorSlot sentinel-SPI初始化时机 记录一次内存泄漏排查 设计思路之系统做深能力的思维方式
掌握Redis集群通信,解决数据存取难题
意犹未尽 · 2026-02-24 · via 博客园 - 意犹未尽

环境

配置

spring:
  data:
    #redis配置
    redis:
      password: ****
      lettuce:
        pool:
          max-idle: 200
          min-idle: 50
          max-active: 5000
          max-wait: 1000
      timeout: 2000
      cluster: # 此处新增
        nodes: 101.4.3.191:7000,101.4.3.191:7001,101.4.3.190:7000,101.4.3.190:7001,101.4.3.189:7000,101.4.3.189:7001     # 集群地址
        max-redirects: 6 #最大重定向次数

异常场景

使用redis template 写入再读取没有数据

写入

   redisTemplate.opsForZSet()
                .add(AgenticBusRedisConstants.AGENTIC_BUS_LONG_POLLING_KEY, taskNo, nextPollingTime);

读取数量返回0

        Long size = redisTemplate.opsForZSet()
                .size(AgenticBusRedisConstants.AGENTIC_BUS_LONG_POLLING_KEY);

问题排查

使用客户端工具访问集群的key

排除是写入失败的问题

image

是否是key序列化问题

怀疑是不是底层改变了key,调试到最底层,写入和读取key是一致的

了解集群通信原理

image

选择机制代码配置参考

@Configuration
public class RedissonConfig {

    @Value("${spring.data.redis.cluster.nodes}")
    private List<String> nodes;

    @Value("${spring.data.redis.password}")
    private String password;


    @Bean
    public RedissonClient clusterRedisClient() {
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers();

        if (!StringUtils.isBlank(password)) {
            clusterServersConfig.setPassword(password);
        }
        // 集群状态扫描间隔时间,单位是毫秒
        clusterServersConfig.setScanInterval(2000);
        // 关键配置:启用集群槽位全覆盖检查,确保正确处理 MOVED/ASK 重定向
        clusterServersConfig.setCheckSlotsCoverage(true);
        // 设置读取模式:MASTER - 只从主节点读取,确保数据一致性
        clusterServersConfig.setReadMode(org.redisson.config.ReadMode.MASTER);
        // 设置订阅模式:MASTER
        clusterServersConfig.setSubscriptionMode(org.redisson.config.SubscriptionMode.MASTER);

        for (String node : nodes) {
            String url = "redis://" + node;
            clusterServersConfig.addNodeAddress(url);
        }
        RedissonClient redissonClient = Redisson.create(config);

        return redissonClient;
    }


}

View Code

MOVED/ASK 重定向原理

image

image

查看当前集群状态

执行命令

CLUSTER NODES

image

image

 可以让AI生成可视化表格

image

 问题排查

1、通过打断点发现路由的服务为

[addr=redis://101.4.3.189:7000] 无数据 查询没有数据,未从节点

2、我将ReadModel改为主节点 则可以正常查询到数据

/**
 * @Description redisson相关配置
 * @Date 2023/9/26 10:44
 * @Author liqiang
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.data.redis.cluster.nodes}")
    private List<String> nodes;

    @Value("${spring.data.redis.password}")
    private String password;


    @Bean
    public RedissonClient clusterRedisClient() {
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers();

        if (!StringUtils.isBlank(password)) {
            clusterServersConfig.setPassword(password);
        }
        // 集群状态扫描间隔时间,单位是毫秒
        clusterServersConfig.setScanInterval(2000);
        // 关键配置:启用集群槽位全覆盖检查,确保正确处理 MOVED/ASK 重定向
        clusterServersConfig.setCheckSlotsCoverage(true);
        // 设置读取模式:MASTER - 只从主节点读取,确保数据一致性
        clusterServersConfig.setReadMode(org.redisson.config.ReadMode.MASTER);
        // 设置订阅模式:MASTER
        clusterServersConfig.setSubscriptionMode(org.redisson.config.SubscriptionMode.MASTER);

        for (String node : nodes) {
            String url = "redis://" + node;
            clusterServersConfig.addNodeAddress(url);
        }
        RedissonClient redissonClient = Redisson.create(config);

        return redissonClient;
    }


}

3、101.4.3.189:7000为从节点,是不是没有复制到数据 连上这台机执行

INFO replication

image

image

 反馈给运维处理,回复

其他命令

计算一个key的槽位

测试环境(101.4.3.191:7001)>CLUSTER KEYSLOT agentic-bus:task:long_polling:
"11299"

查看槽位分配

101.4.3.189(101.4.3.189:7000)>CLUSTER SLOTS
1) 1) "10923"
   2) "16383"
   3) 1) "101.4.3.191"
      2) "7001"
      3) "ebb6538d73bd3324cbde380ce6dc5afb83bad7c4"

   4) 1) "101.4.3.189"
      2) "7000"
      3) "4019bbab983d360c15b7f68d8afdae8a27cf6861"


2) 1) "0"
   2) "5460"
   3) 1) "101.4.3.191"
      2) "7000"
      3) "7b74ab40f4a2c0bdc615ad95ee593785d0a805a0"

   4) 1) "101.4.3.190"
      2) "7001"
      3) "ea8b427b4b0b5b21fe721b7ef9b62488e488f596"


3) 1) "5461"
   2) "10922"
   3) 1) "101.4.3.190"
      2) "7000"
      3) "ac7020869fab5a1c4e6f2789d1945a7c31ad0246"

   4) 1) "101.4.3.189"
      2) "7001"
      3) "8f9349d8fb48b809fbd065d7e9013acf59b2abe3"