spring-boot客户端无法从领事开始

时间:2017-09-02 13:43:14

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

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

sudo docker run -p 8500:8500 consul:0.9.2

领事运行正常,因为我可以从领事UI(下面的图片)中查看:

enter image description here

现在,我正在尝试运行我的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 个答案:

答案 0 :(得分:1)

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

我不太了解它是如何解决的以及为什么它无法从application.yml中读取。如果有人有关于它的一些细节,pl。让我知道。

答案 1 :(得分:1)

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

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

  

说明:-

作为said in docs ,在Consul群集可以开始为请求服务之前,必须将服务器节点选为领导者。这是在启动spring-boot服务时导致领导者例外的原因。还没有当选!

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

注意:-仅出于测试/学习目的,您可以继续创建单个服务器,因为强烈建议不要部署单个服务器,因为在故障情况下不可避免地会丢失数据。

答案 2 :(得分:0)

检查您的pom.xml。

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

对我有用。

答案 3 :(得分:0)

请检查pom.xml以避免使用spring-cloud-starter-consul-all 如果您使用spring-cloud-starter-consul-all,请将该配置写入bootstrap.yml,bootstrap.yml配置文件是第一个加载的文件。