java - 当两者都使用 docker compose 运行时,API-gateway 永远不会从配置服务器获取配置
问题描述
我有三项服务。
- 配置服务器
- 尤里卡服务器
- api网关
如果我单独运行它们,它工作正常。然后我试图在上述服务上介绍 docker。所以我为每个服务准备了 3 个 dockerfile:
VOLUME /tmp
ADD config-server/build/libs/config-server-0.0.1-SNAPSHOT.jar config-server-0.0.1-SNAPSHOT.jar
CMD ["java", "-jar", "config-server-0.0.1-SNAPSHOT.jar"]
VOLUME /var/lib/config-repo
EXPOSE 10270
FROM java:8
VOLUME /tmp
ADD eureka-server/build/libs/eureka-server-1.0-SNAPSHOT.jar eureka-server-1.0-SNAPSHOT.jar
CMD ["java","-jar","eureka-server-1.0-SNAPSHOT.jar"]
EXPOSE 10210
FROM java:8
VOLUME /tmp
ADD api-gateway/build/libs/api-gateway-0.0.1-SNAPSHOT.jar api-gateway-0.0.1-SNAPSHOT.jar
RUN bash -c 'touch /api-gateway-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/api-gateway-0.0.1-SNAPSHOT.jar"]
然后我准备了我的 docker-compose 文件:
version: '3'
services:
eureka-server:
restart: always
container_name: eureka-server
build:
context: .
dockerfile: eureka-server/Dockerfile_eureka_server
expose:
- 10210
ports:
- 10210:10210
networks:
- servicenet
config-server:
restart: always
container_name: config-server
build:
context: .
dockerfile: config-server/Dockerfile_config_server
expose:
- 10270
ports:
- 10270:10270
healthcheck:
test: ["CMD", "curl", "-f", "http://config-server:10270"]
interval: 30s
timeout: 10s
retries: 5
networks:
- servicenet
api-gateway:
restart: on-failure
container_name: api-gateway
build:
context: .
dockerfile: api-gateway/Dockerfile_api_gateway
expose:
- 10200
ports:
- 10200:10200
networks:
- servicenet
links:
- config-server
- eureka-server
networks:
servicenet:
driver: bridge
但是 api-gateway 在 config-server 完全启动它的服务之前启动。这就是为什么 api 网关从 8080 端口开始并在主机 localhost 和端口 8621 上寻找 eureka 服务器的原因。虽然它没有在 docker 中获取此端口和主机,但它继续寻找 eureka 服务器但不再从配置服务器获取配置。我的配置有什么问题吗?
我在 github 上的 application.properties 文件是这样的
server.port=10200
#Eureka configuration
eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
lombok.equalsAndHashCode.callSuper = call
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.client.registryFetchIntervalSeconds=5
eureka.client.serviceUrl.defaultZone=http://eureka-server:10210/eureka
spring.cloud.service-registry.auto-registration.enabled=true
eureka.client.enabled=true
eureka.client.serviceUrl.registerWithEureka=true
lombok.anyConstructor.suppressConstructorProperties = true
#Zuul Configuration
# A prefix that can added to beginning of all requests.
#zuul.prefix=/api
# Disable accessing services using service name (i.e. gallery-service).
# They should be only accessed through the path defined below.
zuul.ignored-services=*
# Map paths to services
zuul.routes.gallery-service.path=/gallery/**
zuul.routes.gallery-service.service-id=gallery-manager
zuul.routes.image-service.path=/image/**
zuul.routes.image-service.service-id=image-service
zuul.routes.book-manager.path=/book-manager/**
zuul.routes.book-manager.service-id=book-manager
zuul.routes.auth-service.path=/auth/**
zuul.routes.auth-service.service-id=auth-manager
zuul.routes.remote-library.path=/remote/**
zuul.routes.remote-library.service-id=remote-library
#zuul.routes.auth-service.strip-prefix=false
# Exclude authorization from sensitive headers
zuul.routes.auth-service.sensitive-headers=Cookie,Set-Cookie
注意:如果我尝试使用其他服务而不是 api-gateway 它工作正常。我正在为 api 网关服务使用 zuul 代理。
解决方案
一个快速的解决方法是添加一个“depends_on”子句,以便 api-service 依赖于配置服务器 - 这样 api-service 在配置服务器启动之前不会启动。
推荐阅读
- matlab - MatLab / Simulink 中的符号
- pattern-matching - 模式匹配案例中的统一
- javascript - node.js await 仅在异步函数中有效
- c - 程序打印一个字节联合的所有位而不使用按位运算符
- c# - ViewBag.Message 未显示 ASP.NET MVC c#
- linux - Need understand "ulimit"'s nofile setting in host and container
- java - 随机浮点值重复
- java - Java - 如何为多个线程同步运行函数
- python - 动态传递列名以及python mysql查询中的值
- elasticsearch - 动态模板支持默认类型?