问题描述
我目前有两个微服务:
-服务-端口8080,此微服务尝试从其他微服务获取配置.
- config -端口8888,该微服务应该提供配置.
I currently have two microservices:
- service - port 8080, this microservice tries to fetch config from the other microservice.
- config - port 8888, this microservice is supposed to provide config.
由于某种原因,我的服务无法从config microservice获取配置.
我的配置微服务应能正常工作,因为我可以在收到的计算机上卷曲localhost:8888/service/default:
For some reason my service is unable to fetch configuration from config microservice.
My config microservice should work because I can curl localhost:8888/service/default on my machine I receive:
{"name":"service","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/shared/service.yml","source":{"server.port":8080,"spring.security.user.password":"admin"}},{"name":"classpath:/shared/service.yaml","source":{"server.port":8080,"spring.security.user.password":"admin"}}]}
收到错误(完整)
service | 2019-06-06 21:31:06.721 INFO 1 --- [main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://config:8888
service | 2019-06-06 21:31:06.894 INFO 1 --- [main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://config:8888. Will be trying the next url if available
service | 2019-06-06 21:31:06.904 ERROR 1 --- [main] o.s.boot.SpringApplication : Application run failed
Docker-compose.yaml
Docker-compose.yaml
version: '3.7'
services:
config:
container_name: config
build: ./config
ports:
- 8888:8888
service:
container_name: service
build: ./service
ports:
- 8080:8080
depends_on:
- config
服务Dockerfile:
Service Dockerfile:
FROM openjdk:8-jdk-alpine
ADD target/service.jar /app.jar
CMD [ "java", "-Xmx200m", "-jar", "/app.jar" ]
EXPOSE 8080
服务bootstrap.yaml
Service bootstrap.yaml
spring:
application:
name: service
cloud:
config:
uri: http://config:8888
fail-fast: true
service.yaml(具有服务配置)
service.yaml (has service configuration)
server:
port: 8080
spring:
security:
user:
password: admin # doesnt set since no connection
配置Dockerfile
Config Dockerfile
FROM openjdk:8-jdk-alpine
ADD target/config.jar /app.jar
CMD [ "java", "-Xmx200m", "-jar", "/app.jar" ]
EXPOSE 8888
配置application.yaml
Config application.yaml
spring:
application:
name: config
profiles:
active: composite
cloud:
config:
server:
composite:
- type: native
search-locations: classpath:/shared
server:
port: 8888
shared/service.yaml(具有服务配置)
shared/service.yaml (has service configuration)
server:
port: 8080
spring:
security:
user:
password: admin # doesnt set since no connection
有什么想法吗?
我发现了一些类似的问题,尽管他们的URI只是问题,我的设置正确.微服务无法访问Docker Compose上的Config Server a>
Docker-SpringConfig-连接被拒绝到ConfigServer
I found some similar issues, although they only had issues with their URI, mine is set correctly.Microservice can not reach to Config Server on Docker Compose
Docker - SpringConfig - Connection refused to ConfigServer
推荐答案
当一项服务依赖于另一项服务时,必须确保该服务已完全启动,然后才能连接到该服务.
When one service depends on another you have to make sure that the latter is fully started before connecting to it.
在您的情况下,很可能在运行 service
时启动了 config
,但尚未准备好(上下文已启动).正如@Ganesh Karewad和@asolanki指出的,解决方案是实现重新连接逻辑.另一个解决方案是在运行 service
之前,确保 config
已初始化并接受连接.
In your case, most probably, config
is started but not ready (context started) at the time service
is run. As @Ganesh Karewad and @asolanki pointed out, a solution is to implement a reconnection logic. Another solution is to make sure config
is initialized and accepting connections before you run service
.
您可以使用脚本来实现这一点,该脚本要等到 config
应用程序启动.或者,您可以使用运行状况检查配置 config
容器命令,然后启动它,请等待它被标记为运行状况良好.然后,您可以运行服务容器.
You can achieve that with a script that waits until the config
app is up. In alternative you could configure the config
container with a health check command and after you start it, wait until it is marked as healthy. Then you can run the service container.
希望有帮助.
这篇关于微服务无法在docker-compose中到达配置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!