万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • Redis

    • Redis简介
    • 缓存的认识
    • Linux安装Redis的正确方式
    • Redis数据类型
    • Redis底层数据结构
    • Redis回收策略与缓存过期
    • Redis持久化
    • Redis发布与订阅
    • Redis事务
    • Redis Lua脚本
    • Redis 慢查询
    • Redis 监视器
      • 使用monitor命令
      • 监视器的实现
      • Redis监控平台
    • Redis通讯协议
    • Redis事件处理机制与NIO演进
    • Redis 常用命令
    • Redis与MyBatis整合
    • Spring、SpringBoot整合Redis
  • 集群架构

  • Redis
  • Redis
2022-03-30
目录

Redis 监视器

# Redis 监视器

Redis客户端通过执行MONITOR命令可以将自己变为一个监视器,实时地接受并打印出服务器当前处理的命令请求的相关信息。

此时,当其他客户端向服务器发送一条命令请求时,服务器除了会处理这条命令请求之外,还会将这条命令请求的信息发送给所有监视器。

monitor.png

# 使用monitor命令

Redis 客户端1 作为监视器:

127.0.0.1:6379> monitor
OK
1648644597.609619 [0 127.0.0.1:52467] "set" "k1" "testmonitor"

Redis 客户端2 执行命令:

127.0.0.1:6379> set k1 testmonitor
OK

# 监视器的实现

redisServer 维护一个 monitors 的链表,记录自己的监视器,每次收到 MONITOR 命令之后,将客户端追加到链表尾。

void monitorCommand(redisClient *c) { 
  /* ignore MONITOR if already slave or in monitor mode */ 
  if (c->flags & REDIS_SLAVE) return; 
  c->flags |= (REDIS_SLAVE|REDIS_MONITOR); 
  listAddNodeTail(server.monitors,c); 
  addReply(c,shared.ok); 
}

利用call函数实现向监视器发送命令:

/* Call() is the core of Redis execution of a command */ 
void call(redisClient *c, int flags) { 
  // ......
  long long dirty, start = ustime(), duration; 
  int client_old_flags = c->flags; 
  /* Sent the command to clients in MONITOR mode, only if the commands are * not generated from reading an AOF. */ 
  if (listLength(server.monitors) && 
      !server.loading && 
      !(c->cmd->flags & REDIS_CMD_SKIP_MONITOR)) {
    // 将命令打包为协议RESP发送给监视器。
    replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc); 
  }
  // ......
}

# Redis监控平台

Redis的监控平台主要有:grafana、prometheus以及redis_exporter。

  • Grafana (opens new window) 是一个开箱即用的可视化工具,具有功能齐全的度量仪表盘和图形编辑器,有灵活丰富的图形化选项,可以混合多种风格,支持多个数据源特点。
  • Prometheus是一个开源的服务监控系统,它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上。
  • redis_exporter为Prometheus提供了redis指标的导出,配合Prometheus以及grafana进行可视化及监控。
上次更新: 5/30/2023, 11:42:20 PM
Redis通讯协议

Redis通讯协议→

最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式