开发者社区> 问答> 正文

当两者都与docker compose一起运行时,API网关永远不会从配置服务器获取配置

我有三项服务。

配置服务器 尤里卡服务器 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在配置服务器完全启动其服务之前启动。这就是为什么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,则可以正常工作。我正在使用zuul代理进行api网关服务。

展开
收起
垚tutu 2019-12-12 09:41:36 1108 0
1 条回答
写回答
取消 提交回答
  • #include

    一个快速的解决方法是添加一个'depends_on'子句,以使api服务依赖于配置服务器-这样,直到配置服务器启动后,api服务才启动。

    2019-12-12 09:41:42
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
应用 Docker 进行持续交付:用技术改变交付路程 立即下载
从Docker到容器服务 立即下载
构建基因数据应用生态系统—— docker in Bio/informatics 立即下载