You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prometheus发现服务地址为空?Spring Boot 2+Zookeeper注册问题排查

解决Prometheus报错"Address of discovered service is empty"的问题

我之前在使用Spring Boot 2结合spring-cloud-starter-zookeeper-discovery做服务发现时,也碰到过完全一样的问题!咱们来一步步拆解解决:

问题根源分析

从你提供的Zookeeper注册内容来看,服务的address字段是bf3fb9191373——这明显是容器的内部ID,而不是Prometheus能够访问到的真实IP或主机名。Prometheus在通过Zookeeper发现服务时,无法识别这个容器ID作为有效地址,所以就抛出了"Address of discovered service is empty"的错误。

解决方案

1. 强制指定服务注册的可访问地址

修改你的Spring Boot应用配置(application.yml或application.properties),明确设置服务注册到Zookeeper时使用的主机地址:

如果用YAML配置:

spring:
  cloud:
    zookeeper:
      discovery:
        instance-host: 你的服务器公网/内网IP # 或者可被Prometheus访问的主机名

如果用Properties配置:

spring.cloud.zookeeper.discovery.instance-host=你的服务器公网/内网IP

如果是容器化部署(比如Docker/K8s),可以通过环境变量动态传递地址:

  • 启动命令中添加参数:-Dspring.cloud.zookeeper.discovery.instance-host=${HOST_IP}
  • 运行容器时注入HOST_IP环境变量,值为宿主机或集群内可访问的IP

2. 验证Zookeeper注册内容

修改配置并重启应用后,登录Zookeeper客户端查看节点数据:

zkCli.sh get /services/example-service

确认返回的address字段已经变成你指定的可访问地址,而不是容器ID。

3. 检查Prometheus的服务发现配置

确保Prometheus的Zookeeper服务发现配置正确提取地址和端口信息。示例配置如下:

scrape_configs:
  - job_name: 'zookeeper-discovered-services'
    zookeeper_sd_configs:
      - servers: ['你的Zookeeper地址:2181']
        paths: ['/services']
    relabel_configs:
      - source_labels: ['__meta_zookeeper_address']
        target_label: 'instance'
      - source_labels: ['__meta_zookeeper_port']
        target_label: 'port'

这里的关键是__meta_zookeeper_address要能正确提取到我们配置的instance-host值,这样Prometheus才能拿到有效的服务地址。

额外排查点

  • 确认spring-cloud-starter-zookeeper-discovery的版本和Spring Boot 2的版本兼容,版本不匹配可能导致注册字段异常;
  • 如果是K8s集群环境,可以考虑使用Pod IP或Service地址作为instance-host,确保Prometheus所在的网络能够访问到该地址。

内容的提问来源于stack exchange,提问作者stphngrtz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:55:33