# Monitor-Study **Repository Path**: qingchuancoder/monitor-study ## Basic Information - **Project Name**: Monitor-Study - **Description**: monitor-study - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-09 - **Last Updated**: 2026-10-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Monitor-Study 可观测性监控演练仓库,包含两个独立模块: - `e-commerce/`:电商微服务演示模块,8 个 Spring Boot 服务(HTTP + Kafka,其中 user / product / inventory 对外暴露 gRPC)+ 完整监控栈(Prometheus / Alertmanager / Loki / Tempo / Grafana + Promtail) - `study/`:教学模块,2 个学习应用 + 同款监控栈(日志采集器换 Grafana Alloy)+ 7 章可观测性教程 两部分完全分离:Maven reactor、配置文件、凭据、启动脚本互不共享。**业务端口互不冲突**(`e-commerce` 8090-8097,`study` 8080-8081),但**监控栈端口重叠**(9090 / 9093 / 3000 / 3100 / 3200 两边都用),因此两套环境不能同时启动。仓库适合用于: - 本地微服务演示 - 监控链路学习与排错 - Prometheus / Alertmanager / Loki / Tempo / Grafana 配置实践 - 基于真实应用的可观测性实验 ## 选哪个模块 | 你的目标 | 选 | 理由 | |---|---|---| | 第一次接触可观测性 | `study/` | 2 个应用、启动成本低,三支柱链路最容易跑通 | | 看真实微服务链路、告警与 trace 关联 | `e-commerce/` | 8 服务 + Kafka + 3 个 gRPC 服务端 | | 学单个组件的配置 | `skills/` | 6 份按组件拆分的配置文档,可脱离本仓库阅读 | 建议顺序:跑通 `study/` → 读 `study/docs/tutorial/01-07` → 再进 `e-commerce/`。 ## 目录结构 ```text Monitor-Study/ ├── README.md ├── e-commerce/ # 电商演示模块 │ ├── README.md │ ├── apps/ │ ├── config/ │ ├── docs/ │ ├── infra/ │ ├── scripts/ │ ├── running-data/ # 运行数据(日志 / TSDB / positions,已 gitignore) │ └── ... ├── study/ # 教学模块 │ ├── README.md │ ├── apps/ │ ├── config/ │ ├── docs/ │ ├── scripts/ │ ├── running-data/ # 运行数据(日志 / TSDB / credentials,已 gitignore) │ └── ... ├── skills/ # AI 配置技能文档(6 份) │ ├── prometheus-config/ │ ├── alertmanager-config/ │ ├── loki-config/ │ ├── tempo-config/ │ ├── alloy-config/ │ └── grafana-config/ ├── .github/ │ ├── workflows/ │ └── scripts/ └── ... ``` ## 先决条件 在运行前,确保本机已安装: - JDK 17+(推荐 17 或 21) - Maven 3.8+ - 监控组件二进制(路径经 `MONITOR_*_HOME` 环境变量注入,默认值在各自 `scripts/monitor.ps1` 顶部): - 两模块共用:Prometheus 3.2.1 / Alertmanager 0.33.1 / Grafana 11.2.0 / Loki / Tempo 3.0.3 - `e-commerce` 另需 **Promtail**(日志采集,:9080);可选 `windows_exporter`(:9182,**默认不启**,需 `monitor.ps1 start -Component windows_exporter`) - `study` 另需 **Grafana Alloy**(日志采集,:12345,River 语法);可选 `windows_exporter`(:9182,**默认不启**) - 两模块都需要 OTel Java Agent(追踪,路径经 `MONITOR_OTEL_AGENT_HOME` 注入) - `promtool` / `amtool` 由脚本按绝对路径从组件目录取(`promtool.exe` 随 Prometheus 包、`amtool.exe` 随 Alertmanager 包,**无需加入 PATH**);缺失时 `monitor.ps1` 启动前预检会失败 - PowerShell 5.1+(Windows)或 bash/zsh(Linux/macOS,对应 `.sh` 脚本) - 端口未被占用 > 本仓库**不依赖 Docker**:监控栈与全部应用都以本地二进制 + `java -jar` 方式由 `monitor.ps1`(或 `monitor.sh`)一键启停。`e-commerce/infra/docker-compose*.yml` 与 `e-commerce/infra/k8s/` 仅作参考,`monitor.ps1` 不会读取它们。 建议先检查以下端口是否空闲(两行之间有重叠,同一时刻只能启动一套): - `e-commerce`:8090-8097(业务) + 9090 / 9093 / 3000 / 3100 / 3200(监控栈) + 9080(Promtail HTTP) + 4317 / 4318(OTLP 接收)+ 9082/9083/9085(gRPC) - `study`:8080-8081(业务) + 9090 / 9093 / 3000 / 3100 / 3200(监控栈) + 12345(Alloy 调试 UI) + 4317 / 4318(OTLP 接收) 如果端口被占用,需先停止冲突进程或调整配置文件中的端口。 ## 快速启动 ### 1)电商模块(推荐用于完整演示) 在仓库根目录执行: ```powershell # 只构建电商模块的应用 mvn -f e-commerce/apps/pom.xml clean package # 启动监控栈和相关组件 .\e-commerce\scripts\monitor.ps1 start # 查看状态 .\e-commerce\scripts\monitor.ps1 status ``` 如果你想只看日志或排查服务启动问题:日志统一落在 `e-commerce/running-data/logs/`,**扁平结构**,每个组件两个文件: - `running-data/logs/<组件名>.out.log`:标准输出,如 `gateway-service.out.log`、`prometheus.out.log` - `running-data/logs/<组件名>.err.log`:错误输出,**启动失败先看这里** `<组件名>` 与 `monitor.ps1 status` 输出的组件名一致(8 个业务服务 + 6 个监控栈组件)。 如果需要停止: ```powershell .\e-commerce\scripts\monitor.ps1 stop ``` ### 2)教学模块(推荐用于学习和验证) 同样在仓库根目录执行: ```powershell # 只构建教学模块的应用(聚合 reactor 位于 study/apps/pom.xml) mvn -f study/apps/pom.xml clean package # 启动监控栈和 2 个学习应用 .\study\scripts\monitor.ps1 start # 查看状态 .\study\scripts\monitor.ps1 status ``` 如果你想观察启动日志:日志落在 `study/running-data/logs/<组件名>/`,**每个组件一个子目录**(`service` 标签由 Alloy 的 `targets` 显式声明,与 `e-commerce` 的扁平结构不同): - `running-data/logs/prometheus-study/prometheus-study.out.log` - `running-data/logs/webhook-app/webhook-app.out.log` - 启动异常看同目录的 `<组件名>.err.log` 如果需要停止: ```powershell .\study\scripts\monitor.ps1 stop ``` 详细命令与说明请阅读: - `study/README.md` - `study/scripts/monitor.ps1` ## 启动成功判定标准 `monitor.ps1 start` 返回 0 只代表进程拉起成功。是否真正可用,用下面三条判断。 ### 1)进程与端口 - `scripts/monitor.ps1 status` 显示全部组件 UP(含二进制 / jar 存在性检查) - `http://localhost:9090/targets` 中所有 target 状态为 `UP` `monitor.ps1` 在启动前会跑 promtool / amtool 配置预检,配置有错会在启动阶段直接失败,不会带病运行。 ### 2)业务健康端点 | 模块 | 端点 | |---|---| | `study` prometheus-study | http://localhost:8080/customized_path/health | | `study` webhook-app | http://localhost:8081/actuator/health | | `e-commerce` gateway | http://localhost:8090/actuator/health | | `e-commerce` auth | http://localhost:8091/actuator/health | | `e-commerce` user | http://localhost:8092/actuator/health | | `e-commerce` product | http://localhost:8093/actuator/health | | `e-commerce` order | http://localhost:8094/actuator/health | | `e-commerce` inventory | http://localhost:8095/actuator/health | | `e-commerce` payment | http://localhost:8096/actuator/health | | `e-commerce` notification | http://localhost:8097/actuator/health | ### 3)三支柱联动 在 Grafana(http://localhost:3000)的 Explore 中: - Prometheus 数据源能查到 `up`、`jvm_memory_used_bytes` 等指标 - Loki 数据源能查到应用日志行,且行上带 `trace_id` - Tempo 数据源能用日志里的 `trace_id` 反查到完整链路 第 3 条是真正的闭环判据:它同时验证了指标采集、日志采集、trace 关联三件事。全都启动成功但查不到数据,通常是 targets 里 `team` 标签或 file_sd 路径不匹配。 完整清单见 `<模块>/scripts/smoke-test.ps1`(`-SkipLink` 只查本地端口与凭据,跳过 targets / 鉴权链路)。 ### 失败常见迹象 | 现象 | 常见原因 | |---|---| | 启动即退出 | 端口被占用;对应组件的 `.err.log` 里有堆栈,路径见上文两模块各自的日志位置说明(`e-commerce` 扁平、`study` 每个组件一个子目录) | | targets 全 `DOWN` | 应用没起来,或 file_sd 端口与 `server.port` 不一致 | | 进程都在但 Grafana 无数据 | 数据源 URL 错、鉴权失败,或告警规则未加载 | | 另一个模块起不来 | 监控栈端口重叠(见下表),先停掉已运行的一套 | ## 端口分配 两模块的**监控栈端口完全相同**,因此同一台机器上只能启动一套;业务端口则互不冲突。 | 组件 | `e-commerce` | `study` | 说明 | |---|---:|---:|---| | 业务服务 | 8090-8097 | 8080-8081 | 8 个微服务 / 2 个学习应用 | | gRPC | 9082 / 9083 / 9085 | — | user / product / inventory 服务端 | | Prometheus | 9090 | 9090 | 指标采集 | | Alertmanager | 9093 | 9093 | 告警聚合与路由 | | Grafana | 3000 | 3000 | 面板可视化 | | Loki | 3100 | 3100 | 日志聚合 | | Tempo | 3200 | 3200 | 分布式链路追踪 | | 日志采集器 | 9080(Promtail HTTP) | 12345(Alloy 调试 UI) | 两模块选型不同,不是遗漏 | | OTLP 接收 | 4317 / 4318 | 4317 / 4318 | OTel Java Agent 直连 Tempo | 其余组件(PostgreSQL 5432、Redis 6379、Kafka 9092)属外部依赖,`monitor.ps1` 不负责拉起。 ## 常用访问入口 | 组件 | 默认地址 | |---|---| | Prometheus | http://localhost:9090 | | Alertmanager | http://localhost:9093 | | Grafana | http://localhost:3000 | | Loki | http://localhost:3100 | | Tempo | http://localhost:3200 | Grafana 管理员口令由 `monitor.ps1` 首次启动时随机生成,写入 `running-data/credentials.env`(已 gitignore),**不会在 `status` 中回显**,需直接查看该文件。`e-commerce` 的邮件接收者是 `config/alertmanager.yml` 里的 `YOUR_QQ_EMAIL@…` 占位符,需自行替换。 ## 常见问题排查 ### 1)启动时报端口已占用 检查是否已有其他服务占用: ```powershell netstat -ano | findstr :9090 netstat -ano | findstr :3000 ``` 如果明确是本仓库的老进程,停止后再启动。 ### 2)监控组件二进制缺失 确保监控组件已下载并放到对应目录(`MONITOR_*_HOME` 指向的位置): ```powershell .\study\scripts\monitor.ps1 status # 会标出哪些组件的 binary 为 MISSING ``` 如果命令失败或报二进制不存在,先按 `study/README.md`「下载监控组件」补全对应组件。 ### 3)Maven 依赖下载失败 尝试重新构建: ```powershell mvn -U -f e-commerce/apps/pom.xml clean package ``` 必要时检查网络、代理、Maven 设置与本地仓库状态。 ### 4)看不到 Grafana / Prometheus 数据 通常有以下原因: - 组件进程还在初始化 - 应用未正常启动 - 配置文件路径错误 - 端口映射不正确(检查组件配置的端口与实际监听端口) 建议先按上文「日志位置」小节找到对应组件的 `.out.log` / `.err.log`,再运行 `scripts/monitor.ps1 status` 确认组件与端口状态。 ### 5)另一个模块起不来 两模块监控栈端口重叠(9090 / 9093 / 3000 / 3100 / 3200),同一时刻只能跑一套。启动前先 `monitor.ps1 status` 确认另一套没在运行,再 `monitor.ps1 stop` 停掉残留进程。 ## 额外资源 - Awesome Prometheus Alerts:https://samber.github.io/awesome-prometheus-alerts/ - Grafana Dashboards:https://grafana.com/grafana/dashboards/ - JVM Dashboard:https://grafana.com/grafana/dashboards/4701-jvm-micrometer/ - Spring Boot Observability:https://grafana.com/grafana/dashboards/17175-spring-boot-observability/ - Spring Boot 3.x Statistics:https://grafana.com/grafana/dashboards/19004-spring-boot-statistics/