无法在localhost:9095获取Eclipse Ditto全服务指标,求解决方案
实现localhost:9095获取Eclipse Ditto所有服务指标的方案
第一步:启用所有Ditto服务的Metrics输出
首先需要为每个Ditto核心服务(things、things-search、gateway、connectivity、policy等)配置环境变量,确保它们都能暴露Prometheus格式的metrics:
DITTO_METRICS_ENABLED=true:开启服务级指标采集DITTO_METRICS_PROMETHEUS_ENABLED=true:启用Prometheus兼容的指标端点DITTO_METRICS_PROMETHEUS_PORT=<服务专属内部端口>:每个服务分配唯一的内部端口(比如things用9091,things-search用9092,gateway用9093)
如果用Docker Compose部署,示例配置片段如下:
services: things: environment: - DITTO_METRICS_ENABLED=true - DITTO_METRICS_PROMETHEUS_ENABLED=true - DITTO_METRICS_PROMETHEUS_PORT=9091 ports: - "9091:9091" things-search: environment: - DITTO_METRICS_ENABLED=true - DITTO_METRICS_PROMETHEUS_ENABLED=true - DITTO_METRICS_PROMETHEUS_PORT=9092 ports: - "9092:9092" # gateway、connectivity、policy等服务按相同逻辑配置
方案一:用Prometheus聚合指标并暴露到9095端口
这是最常用的指标聚合方案,Prometheus会自动抓取所有Ditto服务的指标并统一对外暴露:
- 编写Prometheus配置文件:创建
prometheus.yml,添加所有Ditto服务的抓取目标
global: scrape_interval: 15s scrape_configs: - job_name: 'ditto-things' static_configs: - targets: ['localhost:9091'] - job_name: 'ditto-things-search' static_configs: - targets: ['localhost:9092'] - job_name: 'ditto-gateway' static_configs: - targets: ['localhost:9093'] # 其他Ditto服务的抓取目标依次添加
- 启动Prometheus并映射端口:
将Prometheus默认的9090端口映射到localhost:9095:
docker run -d -p 9095:9090 -v ./prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
- 访问聚合指标:
- 直接访问
http://localhost:9095/metrics获取所有服务的聚合指标 - 通过
http://localhost:9095进入Prometheus UI,可查询、筛选具体指标
方案二:用Nginx反向代理统一端口(含聚合选项)
如果不想依赖Prometheus,可通过Nginx将各服务的metrics端点路由到同一端口的不同路径,也可结合工具实现指标聚合:
- 基础Nginx配置(分路径访问):创建
nginx.conf
events {} http { server { listen 9095; location /things/metrics { proxy_pass http://localhost:9091/metrics; } location /things-search/metrics { proxy_pass http://localhost:9092/metrics; } location /gateway/metrics { proxy_pass http://localhost:9093/metrics; } # 其他服务的metrics路径按相同逻辑添加 } }
启动Nginx容器:
docker run -d -p 9095:9095 -v ./nginx.conf:/etc/nginx/nginx.conf nginx
此时可通过http://localhost:9095/things/metrics等路径单独访问各服务指标。
- 指标聚合扩展:
借助prometheus-metrics-merger工具合并所有服务的metrics,再通过Nginx暴露:
# 启动合并工具,监听9096端口,抓取所有Ditto服务的metrics docker run -d -p 9096:9096 quay.io/prometheuscommunity/merge-metrics --endpoints=http://localhost:9091/metrics,http://localhost:9092/metrics,http://localhost:9093/metrics
在Nginx配置中添加聚合路径:
location /all/metrics { proxy_pass http://localhost:9096/metrics; }
重启Nginx后,访问http://localhost:9095/all/metrics即可获取所有Ditto服务的聚合指标。
关键注意事项
- 所有Ditto服务的metrics端口必须在宿主机上正确映射,避免端口冲突
- Kubernetes部署场景可通过ServiceMonitor自动发现Ditto服务的metrics端点,再由Prometheus统一聚合
- 禁止直接将多个服务的metrics绑定到同一宿主机端口,会导致端口占用冲突
内容的提问来源于stack exchange,提问作者Utkarsh Kumar
相关产品推荐
相关产品推荐

