配置 prometheus.yml
在 Prometheus 的配置文件 prometheus.yml 中,打开 scrape_configs 部分并添加一个抓取作业(job),用于监控 Spring Boot 应用。配置项说明如下:
### 监控 SpringBoot 项目- job_name: 'monitor'scrape_interval: 5smetrics_path: '/actuator/prometheus'static_configs:- targets: ['172.27.XX.XX:8080']
启动 Prometheus
1. 执行如下命令,启动 Prometheus 监控服务。启动后,观察日志输出,确保没有报错。如果出现错误,请根据日志中的提示信息进行排查并解决问题。
./prometheus --config.file=prometheus.yml
2. Prometheus 默认侦听的端口是 9090。当 Prometheus 正常启动后,可以通过浏览器访问
http://localhost:9090
,查看 Prometheus 的管理界面。
3. 在上方导航栏,选择 Status > Target health 选项,可看到配置的 monitor 任务,检查任务的 State 是否为 UP。

使用 Prometheus
1. 使用已实现的 web 服务,对腾讯云 Redis 进行操作,生成监控数据。本实践教程使用了一个 bash 脚本对腾讯云 Redis 不断进行 SET、GET 操作。
#!/bin/bash# 初始化计数器counter=1# 无限循环while true; do# 生成当前的 key 和 value,每次都会变化key="test${counter}"value="Hello${counter}"# 使用 curl 调用 Web 服务的 SET 接口,对 Redis 进行 SET 操作echo "Setting key: ${key} with value: ${value}"curl -X POST "http://localhost:8080/redis/set?key=${key}&value=${value}"echo -e "\\n"# 使用 curl 调用 Web 服务的 GET 接口,对 Redis 进行 GET 操作echo "Getting value for key: ${key}"curl -X GET "http://localhost:8080/redis/get?key=${key}"echo -e "\\n"# 计数器加1counter=$((counter + 1))# 等待5秒钟sleep 5done
2. 访问 Prometheus 操作界面,在 Query 页面的命令框中,执行如下命令,可以查看到 SET 命令的平均延迟的监控视图。
lettuce_command_completion_seconds_sum{command="SET"}/lettuce_command_completion_seconds_count{command="SET"}

3. 在命令框输入
lettuce_command_completion_seconds_max{command="SET"}
,可以查看 SET 命令的最大延迟,从图中可以看到最大延迟大约为1.35 ms。说明:
本教程指标统计从向 Redis 发送命令开始,到接收到 Redis 完整响应后所经过的时间。Lettuce 还提供了从向 Redis 发送命令开始,到接收到第一个 Redis 的响应字节的时间,客户可以通过以下几个指标进行监控:
lettuce_command_firstresponse_seconds_count
lettuce_command_firstresponse_seconds_sum
lettuce_command_firstresponse_seconds_max
