网关服务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

相关推荐
white-persist1 小时前
【vulhub shiro 漏洞复现】vulhub shiro CVE-2016-4437 Shiro反序列化漏洞复现详细分析解释
运维·服务器·网络·python·算法·安全·web安全
代码中介商1 小时前
手把手教你Linux 打包压缩与 gcc 编译详解
linux·运维·服务器·编译·打包·压缩
longerxin20202 小时前
阿里云AlmaLinux操作系统允许root登录配置步骤
linux·服务器·阿里云
xuanwenchao2 小时前
ROS2学习笔记 - 2、类的继承及使用
服务器·笔记·学习
孙同学_2 小时前
【项目篇】高并发服务器 - HTTP服务器组件拆解,从Util到HttpServer
运维·服务器·http
Sherry Wangs3 小时前
服务器 CUDA版本升级指南
运维·服务器
CQU_JIAKE3 小时前
4.17[Q]
java·linux·服务器
上海云盾-小余3 小时前
DDoS 攻击应急响应全流程:从告警触发到业务恢复的黄金 15 分钟
服务器·安全·游戏·ddos
LXY_BUAA3 小时前
《ubuntu22.04》_新系统的配置_20260418
linux·运维·服务器
上海云盾-小余3 小时前
API 接口 DDoS 与 CC 攻击防护实战:守住业务最脆弱的数字入口
服务器·ddos