网关服务gateway注册Consul时报错Consul service ids must not be empty

网关服务gateway启动时,初始化Consul相关配置时报错。

java 复制代码
Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: cbda-server-gateway:10.111.236.142:30857

详细的错误信息如下:

网关gateway的 bootstrap.yml如下:

java 复制代码
spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: cbda-server-gateway
  cloud:
    consul:
      host: 10.111.236.142
      port: 8500
      config:
        enabled: true #false禁用Consul配置,默认true
        format: YAML    # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
        data-key: data   #表示consul上面的KEY值(或者说文件的名字) 默认是data
        prefix: config # 设置配置值的基本文件夹
        defaultContext: cbda-server-gateway # 设置所有应用程序使用的文件夹名称
        #profileSeparator # 设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
      # 服务发现配置
      discovery:
        # 启用服务发现
        enabled: true
        # 启用服务注册
        register: true
        # 服务停止时取消注册
        deregister: true
        # 表示注册时使用IP而不是hostname
        prefer-ip-address: true
        # 关闭consul健康监测
        register-health-check: true
        # 执行监控检查的频率
        health-check-interval: 30s
        # 设置健康检查失败多长时间后,取消注册
        health-check-critical-timeout: 30s
        # 健康检查的路径
        health-check-path: /soa_actuator/health
        # 服务注册标识,格式为:应用名称+服务器IP+端口
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

分析:

网关服务gateway在初始化Consul的相关配置时,未读取到ip,导致相关配置类初始化失败,就未能初始化instance-id。修改配置如下:

java 复制代码
spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: cbda-server-gateway
  cloud:
    consul:
      host: 10.111.236.142
      port: 8500
      config:
        enabled: true #false禁用Consul配置,默认true
        format: YAML    # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
        data-key: data   #表示consul上面的KEY值(或者说文件的名字) 默认是data
        prefix: config # 设置配置值的基本文件夹
        defaultContext: cbda-server-gateway # 设置所有应用程序使用的文件夹名称
        #profileSeparator # 设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
      # 服务发现配置
      discovery:
        # 启用服务发现
        enabled: true
        # 启用服务注册
        register: true
        # 服务停止时取消注册
        deregister: true
        # 表示注册时使用IP而不是hostname
        prefer-ip-address: true
        # 关闭consul健康监测
        register-health-check: true
        # 执行监控检查的频率
        health-check-interval: 30s
        # 设置健康检查失败多长时间后,取消注册
        health-check-critical-timeout: 30s
        # 健康检查的路径
        health-check-path: /soa_actuator/health
        # 服务注册标识,格式为:应用名称+服务器IP+端口
        #instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
        instance-id: ${spring.application.name}:${random.value}

最终服务能正常启动。

参考:The Spring Cloud project throws exception when generating document. · Issue #404 · spring-cloud/spring-cloud-consul · GitHub

相关推荐
yyuuuzz4 小时前
独立站的技术基础与常见运维问题
大数据·运维·服务器·网络·数据库·aws
日取其半万世不竭9 小时前
iftop、nethogs 和 nload:Linux 服务器网络流量实时监控工具介绍
linux·运维·服务器
mounter6259 小时前
Linux 内核资源管理:控制组(cgroup)的演进与“策略组”新提案
linux·运维·服务器·cgroup·kernel
bksczm9 小时前
文件在磁盘中的存储方式
linux·运维·服务器
L1624769 小时前
OpenSSH 半自动升级方案(独立编译 + 手动迁移 + 重建 systemd 服务)
linux·服务器·ssh
Wpa.wk9 小时前
win环境本地文件上传远程服务器(scp/远程连接工具)
运维·服务器
J2虾虾10 小时前
Spring AI Alibaba - Tools
服务器·人工智能·spring
着迷不白11 小时前
五、文本处理工具+正则表达式
linux·运维·服务器
卡次卡次111 小时前
vibecoding起步注意点:插件、Skills、MCP、Hooks
服务器·数据库·python·oracle
和舒貌12 小时前
【无标题】
服务器