【问题标题】:spring-boot client unable to start with consulspring-boot 客户端无法使用 consul 启动
【发布时间】:2018-02-11 08:33:06
【问题描述】:

我已经使用以下命令在我的系统上使用 docker 设置和运行 consul:

sudo docker run -p 8500:8500 consul:0.9.2

Consul 运行良好,我可以从 consul UI 中进行检查(下图):

现在,我正在尝试运行我的 spring-boot 服务来使用这个 consul 实例进行服务发现和注册。但是,每当我开始时,它都会给我以下异常:

2017-09-02 18:58:17.091 ERROR 5578 --- [  restartedMain] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2017-09-02 18:58:18.183 ERROR 5578 --- [  restartedMain] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2017-09-02 18:58:19.375 ERROR 5578 --- [  restartedMain] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2017-09-02 18:58:20.691 ERROR 5578 --- [  restartedMain] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2017-09-02 18:58:22.114 ERROR 5578 --- [  restartedMain] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2017-09-02 18:58:23.671 ERROR 5578 --- [  restartedMain] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2017-09-02 18:58:23.691 ERROR 5578 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

com.ecwid.consul.v1.OperationException: OperationException(statusCode=500, statusMessage='Internal Server Error', statusContent='No cluster leader')
    at com.ecwid.consul.v1.kv.KeyValueConsulClient.getKVValues(KeyValueConsulClient.java:159)
    at com.ecwid.consul.v1.ConsulClient.getKVValues(ConsulClient.java:487)
    at org.springframework.cloud.consul.config.ConsulPropertySource.init(ConsulPropertySource.java:66)
    at org.springframework.cloud.consul.config.ConsulPropertySourceLocator.create(ConsulPropertySourceLocator.java:157)
    at org.springframework.cloud.consul.config.ConsulPropertySourceLocator.locate(ConsulPropertySourceLocator.java:131)
    at org.springframework.cloud.consul.config.ConsulPropertySourceLocator$$FastClassBySpringCGLIB$$b35ebf8.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:91)
    at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:286)
    at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:163)
    at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:118)
    at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:152)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at org.springframework.cloud.consul.config.ConsulPropertySourceLocator$$EnhancerBySpringCGLIB$$66375879.locate(<generated>)
    at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:93)
    at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:567)
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:338)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
    at com.pyg.auth.AuthServiceApp.main(AuthServiceApp.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

SpringBoot Main Class 的注释很好(我猜),因为我的 spring-boot 服务之前在另一个 consul 实例上运行良好。

【问题讨论】:

  • 你是把 Consul 作为节点还是服务器运行?这是本地开发实例?
  • 抱歉大家忘记在这里更新了。我通过将领事服务器和端口配置从 application.yml 移动到 bootstrap.yml 解决了我的问题。

标签: spring spring-boot microservices consul spring-cloud-consul


【解决方案1】:

我能够通过将领事服务器和端口配置从 application.yml 移动到 bootstrap.yml 来解决我的问题。

我不太了解它是如何解决的,以及为什么它无法从 application.yml 中读取。如果有人对此有一些详细信息,请。告诉我。

【讨论】:

  • bootstrap.yml 在 application.yml 之前加载,Consul 客户端必须在应用程序本身之前检查其配置,因此将 consul 服务器和端口配置从 application.yml 移动到 bootstrap.yml。它对你有用。参考:[stackoverflow.com/questions/32997352/…
【解决方案2】:

检查你的 pom.xml。

不要使用spring-cloud-starter-consul-all。请改用spring-cloud-starter-consul-discovery

这对我有用。

【讨论】:

    【解决方案3】:

    请检查 pom.xml 以避免使用 spring-cloud-starter-consul-all 如果使用spring-cloud-starter-consul-all,请将配置写入bootstrap.yml,首先加载bootstrap.yml配置文件。

    【讨论】:

      【解决方案4】:

      使用sudo docker run -p 8500:8500 consul:0.9.2 -bootstrap 解决问题。我有 添加了-bootstrap 一个参数来引导服务器并被选为领导者。

      不建议在较新版本的 Consul(0.5 及更高版本)中使用 -bootstrap 手动引导,因为它更容易出错。

      解释:-

      作为said in docs 在Consul集群开始服务请求之前,必须选举一个服务器节点。这就是你在启动spring-boot服务时出现异常的原因,领导者没有被选为!!

      为什么没有选出领导?应该引导集群中涉及的服务器列表。并且服务器可以使用 -bootstrap-expect 配置选项。 推荐

      注意:- 仅出于测试/学习目的,您可以继续创建单个服务器,因为非常不鼓励使用单个服务器部署,因为在故障情况下数据丢失是不可避免的。

      【讨论】:

        猜你喜欢
        • 2019-06-10
        • 2018-10-31
        • 1970-01-01
        • 2016-06-27
        • 1970-01-01
        • 2022-12-05
        • 1970-01-01
        • 2018-12-31
        • 1970-01-01
        相关资源
        最近更新 更多