简体   繁体   English

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

[英]spring-boot client unable to start with consul

I have setup and run consul using docker on my system using following command: 我使用以下命令在我的系统上使用docker设置并运行consul:

sudo docker run -p 8500:8500 consul:0.9.2

Consul is running fine as I can check from the consul UI (Image attached below): 领事运行正常,可以从领事UI进行检查(下面的图片):

在此处输入图片说明

Now, I am trying to run my spring-boot service to use this consul instance for service discovery and registration. 现在,我尝试运行我的spring-boot服务,以使用该领事实例进行服务发现和注册。 But, whenever i start it gives me following exception: 但是,无论何时启动,它都会给我以下异常:

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 is annotated fine (I guess), because my spring-boot service was working fine with another consul instance earlier. SpringBoot Main Class可以很好地注释(我想),因为我的spring-boot服务可以与先前的另一个领事实例一起正常工作。

I was able to solve my issue by moving consul server and port configuration from application.yml to bootstrap.yml. 我可以通过将领事服务器和端口配置从application.yml移到bootstrap.yml来解决我的问题。

I don't know much about how it was solved and why it was unable to read from application.yml. 我对如何解决它以及为什么无法从application.yml中读取信息了解不多。 If anyone has some details about it, pl. 如果有人对此有一些细节,请。 let me know. 让我知道。

Use sudo docker run -p 8500:8500 consul:0.9.2 -bootstrap to resolve the problem. 使用sudo docker run -p 8500:8500 consul:0.9.2 -bootstrap解决此问题。 I have added -bootstrap one argument to bootstrap the server/servers and get elected as a leader. 我添加了-bootstrap一个参数来引导服务器,并被选为领导者。

Manual bootstrapping with -bootstrap is not recommended in newer versions of Consul (0.5 and newer) as it is more error-prone. 在Consul的新版本(0.5和更高版本)中,不建议使用-bootstrap进行手动引导,因为它更容易出错。

Explanation:- 说明:-

As said in docs Before a Consul cluster can begin to service requests, a server node must be elected leader. docs中所述, 在Consul群集可以开始处理请求之前,必须选择服务器节点的领导者。 And this is reason of your exception on start of spring-boot service the leader has not been elected yet!! 这是您在启动春季启动服务时出现例外的原因,领导者尚未当选!!

Why the leader has not been elected? 为什么领导人没有当选? The list of servers involved in the cluster should be bootstrapped. 群集中涉及的服务器列表应被引导。 And the servers can be bootstrapped using the -bootstrap-expect configuration option. 可以使用-bootstrap-expect配置选项来引导服务器。 Recommended 推荐的

Note:- Just for testing/learning purpose you can go ahead and create a single server because A single server deployment is highly discouraged as data loss is inevitable in a failure scenario. 注意:-仅出于测试/学习目的,您可以继续创建单个服务器,因为强烈建议不要部署单个服务器,因为在故障情况下不可避免地会丢失数据。

Check your pom.xml. 检查您的pom.xml。

Don't use spring-cloud-starter-consul-all . 不要使用spring-cloud-starter-consul-all Use spring-cloud-starter-consul-discovery instead. 请改用spring-cloud-starter-consul-discovery

That worked for me. 那对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM