1

我使用以下命令在我的系统上使用 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 实例一起工作得很好。

4

4 回答 4

1

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

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

解释:-

正如文档中所说, 在 Consul 集群开始服务请求之前,必须选举一个服务器节点作为领导者。 And this is reason of your exception on start of spring-boot service the leader has not been elected yet!!

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

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

于 2019-08-23T19:12:33.117 回答
1

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

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

于 2017-11-01T06:12:41.740 回答
0

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

于 2019-06-11T04:50:23.187 回答
0

检查你的 pom.xml。

不要使用spring-cloud-starter-consul-all. 改为使用spring-cloud-starter-consul-discovery

这对我有用。

于 2018-11-30T12:43:44.910 回答