使用docker-compose编排spring-cloud微服务时顺序问题
问题如题,使用spring-cloud
构建了几个微服务,用于测试使用docker-compose
编排,在测试过程中发现一个问题:
微服务提供者工程使用了配置中心工程提供的变量,使用
@Value("${from}")
方式注入,两个服务都依赖发现服务(Eureka
),那么当配置中心工程未启动时,微服务提供者是无法正确启动的。启动时${from}
变量会提示不存在,从而导致出错。如果是本地启动,可以先启动配置中心工程,是可以规避这个问题的,但在使用docker-compose
编排时,要怎样解决依赖问题,或者说是否可以指定某个工程启动完成后再启动其它工程?
docker-compose.yml
version: '2'
services:
discover1:
image: zlikun/eureka:1.0.0
container_name: discover1
ports:
- "8761:8761"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover2:8761/eureka/,http://discover3:8761/eureka/
discover2:
image: zlikun/eureka:1.0.0
container_name: discover2
ports:
- "8762:8761"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover1:8761/eureka/,http://discover3:8761/eureka/
discover3:
image: zlikun/eureka:1.0.0
container_name: discover3
ports:
- "8763:8761"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover1:8761/eureka/,http://discover2:8761/eureka/
config1:
image: zlikun/config:1.0.0
container_name: config1
ports:
- "7001:7001"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover1:8761/eureka/,http://discover2:8761/eureka/,http://discover3:8761/eureka/
config2:
image: zlikun/config:1.0.0
container_name: config2
ports:
- "7002:7001"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover1:8761/eureka/,http://discover2:8761/eureka/,http://discover3:8761/eureka/
user1:
image: zlikun/user:1.0.0
container_name: user1
ports:
- "9001:9000"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover1:8761/eureka/,http://discover2:8761/eureka/,http://discover3:8761/eureka/
user2:
image: zlikun/user:1.0.0
container_name: user2
ports:
- "9002:9000"
environment:
- spring.profiles.active=prod
- eureka.client.service-url.defaultZone=http://discover1:8761/eureka/,http://discover2:8761/eureka/,http://discover3:8761/eureka/
测试工程
报错信息
2017-08-16 15:47:28.474 WARN 12932 --- [ main] lientConfigServiceBootstrapConfiguration : Could not locate configserver via discovery
java.lang.IllegalStateException: No instances found of configserver (config)
at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstance(ConfigServerInstanceProvider.java:25) ~[spring-cloud-config-client-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.refresh(DiscoveryClientConfigServiceBootstrapConfiguration.java:80) [spring-cloud-config-client-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.startup(DiscoveryClientConfigServiceBootstrapConfiguration.java:66) [spring-cloud-config-client-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:256) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:177) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:140) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:399) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at com.zlikun.sc.UserApplication.main(UserApplication.java:12) [classes/:na]
2017-08-16 15:47:28.513 INFO 12932 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9000 (http)
2017-08-16 15:47:28.513 INFO 12932 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9000
2017-08-16 15:47:28.517 INFO 12932 --- [ main] com.zlikun.sc.UserApplication : Started UserApplication in 8.029 seconds (JVM running for 8.768)
这里的异常信息是我在本机调试时,把配置中心关闭情况下启动微服务提供者时抛出的异常。我有两个问题:
- 怎样规避配置依赖的问题(从
spring-cloud
本身着手) - 怎样处理
docker-compose
编排时的容器启动顺序(关于docker-compose
本人是初学,所以还不是很懂)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
拆分开docker-compose文件
把基础平台的如配置中心之类的单独出来。 毕竟这些在整个应用周期可能就只启动一次的。