我有一个spring cloud配置服务器并将其打包为docker图像然后我有spring spring eureka服务器,它也打包为docker image .
当我使用docker compose运行两个时,我得到以下错误 .
discovery-service_1 | 2017-06-24 15:36:12.059 INFO 5 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://config-service:9001 discovery-service_1 | 2017-06-24 15:36:12.997 WARN 5 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://config-service:9001/cls-discovery-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
虽然配置服务已成功启动并运行,但发现服务仍然由于某种原因找不到它 .
Docker compose file being used here is this version: '2' services: config-service: image: cloudsea/cls-config-service ports: - 9001:9001 expose: - "9001" discovery-service: image: cloudsea/cls-discovery-service depends_on: - config-service environment: CLOUD_SEA_CONFIG_SERVER_URI: http://config-service:9001 EUREKA_DEFAULT_ZONE_URL: http://discovery-service:8761/eureka/ ports: - 8761:8761 links: - config-service:config-service
以下是DISCOVERY SERVICE的 bootstrap.properties
spring.cloud.config.uri = ${CLOUD_SEA_CONFIG_SERVER_URI:http://localhost:9001} spring.application.name = ${SPRING_APPLICATION_NAME:cls-discovery-service}
下面是位于github的DISCOVERY SERVICE的 cls-discovery-service.properties .
server.port=${SERVER_PORT:8761} eureka.client.registerWithEureka: false eureka.client.fetchRegistry: false eureka.client.serviceUrl.defaultZone: ${EUREKA_DEFAULT_ZONE_URL:http://localhost:8761/eureka/} eureka.server.eviction-interval-timer-in-ms: 1000
我假设我的docker-compose.yml有问题,但我不确定 .
任何帮助我将坚持这几个小时......接近几天:(
3 回答
我通过将此配置添加到 discovery service's bootstrap.yml 来解决了这个问题 .
然后将 spring-boot-starter-aop 和 spring-retry 添加到 discovery service's maven dependencies .
问题是他们都是在同一时间开始 . 但是发现服务取决于配置服务 .
当您启动发现服务时,它将一次又一次地说“ Fetching config from server ”,直到配置服务启动 .
After the config service starts, discovery service is going to get its configuration successfully 然后它将开始自己 .
我遇到了同样的问题,并且卡住了一段时间,我打算采用与 spring 重试相同的路线,这不是一个糟糕的模式,它可以将弹性嵌入到您的应用程序中,但主要问题这里是码头 Worker 的发射顺序不合适 . 我将分享我的工作docker-compose文件,它非常相似,但这里的关键是“depends_on”参数 . 我在我的论点中添加了引号,它似乎有效 .
问题是所有docker容器将一起启动,但根据您的体系结构,config-service需要先启动,然后启动发现服务(eureka) . 所以发现服务正在给出错误 .
depends_on
does not wait for config-service to be “ready” before starting discovery-service - it only waits until it is started . 如果您需要等待服务准备就绪,则必须使用 controlling startup order .As suggested about you can set the APIs/Services to keep retrying in some intervals until the config server is up .
虽然,我也更喜欢使用depends_on或healthcheck并控制启动顺序,如下所示:
wait-for-it.sh是一个纯粹的bash脚本,它将等待可用性 . 我也建议调查, container orchestration tools like docker-swarm or kubernetes .