Springboot报错:Fail fast is set and there was an error reading configuration from consul
今天搭建springboot集成consul配置中心的时候遇到如下报错
2021-02-09 11:50:11.787 [main] ERROR o.s.c.c.c.ConsulPropertySourceLocator:154 - Fail fast is set and there was an error reading configuration from consul.
点进源码断点查看,看到以下报错导致:
复制出来是这个
com.ecwid.consul.transport.TransportException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8500 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
然后断点到里面看到:
这里显示的consul地址是:localhost:8500
然后我们实际配置的是:
spring:
application:
name: game-gateway-server
cloud:
consul:
host: 127.0.0.1
port: 2000
scheme:
discovery:
service-name: ${spring.application.name}
prefer-ip-address: true #表示注册时使用ip而不是hostname
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #实例id
register: true #配置服务注册
deregister: true #服务停止时取消注册
health-check-critical-timeout: 30s #健康检查失败多长时间取消注册
config:
enabled: true # 启用配置中心
prefix: test
format: KEY_VALUE # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
说明consul拉取配置中心的时候,我们的配置并未生效
解决办法:
创建bootstrap.yml,将配置拷贝到bootstrap.yml里面去
spring:
application:
name: game-gateway-server
cloud:
consul:
host: 127.0.0.1
port: 2000
scheme:
discovery:
service-name: ${spring.application.name}
prefer-ip-address: true #表示注册时使用ip而不是hostname
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #实例id
register: true #配置服务注册
deregister: true #服务停止时取消注册
health-check-critical-timeout: 30s #健康检查失败多长时间取消注册
config:
enabled: true # 启用配置中心
prefix: test
format: KEY_VALUE # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
再次重启,读取成功