Dubbo-Go 支持采集运行态 Metrics 指标,并接入 Prometheus + Grafana 实现微服务可观测性。
当前示例支持两种监控模式:
示例源码:
https://github.com/apache/dubbo-go-samples/tree/main/metrics
Dubbo-Go 应用 ---> Prometheus ---> Grafana
(暴露 /metrics 或 /prometheus 接口)
Prometheus 主动抓取 Dubbo-Go 应用指标。
Dubbo-Go 应用 ---> Pushgateway ---> Prometheus ---> Grafana
应用主动推送指标到 Pushgateway,Prometheus 再拉取。
Pushgateway 适用于 短生命周期任务(如 batch / cron job),不推荐用于长期运行的服务。
| 组件 | 端口 | 说明 |
|---|---|---|
| Grafana | 3000 | 指标可视化 |
| Prometheus | 9090 | 指标存储与查询 |
| Pushgateway | 9091 | 接收应用推送指标 |
| go-server 指标端口 | 本示例中为 9099 | Pull 模式下 Provider 指标暴露端口 |
| go-client 指标端口 | 本示例中为 9097 | Pull 模式下 Consumer 指标暴露端口 |
如果不使用本示例,而是直接使用 Dubbo-Go 默认配置,则默认指标端点为 http://localhost:9090/metrics。当前 sample 将指标路径改成了 /prometheus。
进入目录:
cd metrics/prometheus_grafana
启动监控组件:
docker-compose up -d
访问地址:
客户端与服务端使用相同环境变量:
export ZK_ADDRESS="127.0.0.1:2181"
# Push 模式必需
export PUSHGATEWAY_URL="127.0.0.1:9091"
export JOB_NAME="dubbo-service"
# 可选
export PUSHGATEWAY_USER="username"
export PUSHGATEWAY_PASS="1234"
go run ./go-server/cmd/main.go
go run ./go-client/cmd/main.go
go run ./go-client/cmd/main.go --push=false
go run ./go-server/cmd/main.go --push=false
访问:
http://localhost:9091/metrics
<应用端口> 指的是 Dubbo-Go 应用自身暴露 Prometheus 指标的 HTTP 端口,不是 Prometheus 或 Pushgateway 的端口。
在当前 sample 中:
http://localhost:9099/prometheushttp://localhost:9097/prometheus这两个端口定义在 metrics/prometheus_grafana/prometheus_pull.yml 中。
如果你使用的是自己的 Dubbo-Go 应用,请将端口替换成应用实际配置的指标端口。
Home → Connections → Data sources
http://host.docker.internal:9090
说明:
host.docker.internal用于让 Docker 容器访问宿主机网络。如果该地址不可用,请替换为宿主机实际 IP。
Home → Dashboards → New → Import
grafana.json19294(Dubbo Observability)后点击 Loadsample 仓库已经直接提供 metrics/prometheus_grafana/grafana.json,因此优先上传这个文件即可。
选择 Prometheus 数据源
点击 Import
你将看到:
Pushgateway 默认:
不会自动删除旧指标
任务停止后:
机制:
job_pushed_at_seconds工具仓库位置:
详细说明:
该工具位于 apache/dubbo-go-samples 仓库中,不在 apache/dubbo-go 主仓库内。
用于:
请检查:
dubbo_consumer_requests_succeed_total
是否有数据
请改为实际 IP 地址:
metrics/prometheus_grafana/prometheus_pull.yml推荐使用:
kube-prometheus https://github.com/prometheus-operator/kube-prometheus
创建:
dubboPodMonitor.yaml
内容:
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: dubbo-pod-monitor
namespace: monitoring
spec:
namespaceSelector:
matchNames:
- dubbo-system
selector:
matchLabels:
app-type: dubbo
podMetricsEndpoints:
- port: metrics
path: /prometheus
如果集群开启了 RBAC,建议为 Prometheus 增加对 dubbo-system 命名空间内 Pod 的读取权限:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: dubbo-system
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: dubbo-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: pod-reader
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: monitoring
如果你的 Prometheus 安装使用的 ServiceAccount 不是 prometheus-k8s,请按实际名称修改 subjects。
kubectl apply -f Deployment.yaml
访问:
http://<prometheus-nodeport>/targets
确认 Pod 状态为:
UP
| 场景 | 推荐模式 |
|---|---|
| 长期运行服务 | Pull |
| 短生命周期任务 | Push |
| K8s 环境 | Pull + PodMonitor |
| 需要清理能力 | 配合 pgw-cleaner |
Dubbo-Go 当前支持:
通过以上配置,可以实现完整的 Dubbo-Go 可观测性体系。