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

相关推荐
沫璃染墨21 分钟前
从零入门计算机网络系列(十一):HTTP协议进阶——从重定向原理到GET/POST与Fiddler抓包
linux·服务器·开发语言·网络·c++·tcp/ip·html
Kobebryant-Manba35 分钟前
C/S和B/S结构
运维·服务器
AI_Lab012 小时前
宝塔用了两年,有没有必要换成轻量方案?
linux·运维·服务器
by209992 小时前
Linux GDB 调试与 CGDB 使用
linux·运维·服务器·经验分享
小羊没烦恼!2 小时前
关于分布式锁原理的一些学习与思考-redis分布式锁,zookeeper分布式锁
java·服务器·网络·学习·负载均衡
小Ti客栈2 小时前
Netty 零拷贝原理与实战
运维·服务器·网络
SDWAN_Cheap3 小时前
物理服务器跟云服务器有什么区别,使用于什么场景
运维·服务器
Smoothcloud润云3 小时前
GPU算力平台推荐:复现实验时,如何判断租用资源是否合适?
服务器·人工智能·深度学习·gpu算力·gpu
叼烟扛炮5 小时前
[LangChain] 学习笔记(02):LCEL 链式调用与动态模型配置实战
服务器·开发语言·网络·笔记·学习·langchain
Raas1005 小时前
AI网关怎么部署?MAI Gateway部署实操教程
人工智能·开源·gateway·mai gateway·企业级ai产品