docker-compose 容器间通信

Ash*_*Ash 5 docker spring-boot docker-compose

我目前正在试验基于 Spring Boot 的微服务并开始使用 docker,但我遇到了障碍。

基本上我想要做的是容器化 2 个小服务:一个 spring 云配置服务和一个 spring 云尤里卡服务(发现服务)。eureka 服务从配置服务中获取其配置。

这两个服务都是具有自己的 Dockerfile 的独立项目:

Dockerfile-cloud-config-service:

FROM openjdk:10.0.2-13-jre-sid
ENV APP_FILE cloud-config-service.jar
ENV APP_HOME /usr/apps
EXPOSE 8888
COPY target/$APP_FILE $APP_HOME/
WORKDIR $APP_HOME
ENTRYPOINT ["sh", "-c"]
CMD ["exec java -jar $APP_FILE"]
Run Code Online (Sandbox Code Playgroud)

Dockerfile-发现-服务:

FROM openjdk:10.0.2-13-jre-sid
ENV APP_FILE discovery-service.jar
ENV APP_HOME /usr/apps
EXPOSE 8761
COPY target/$APP_FILE $APP_HOME/
WORKDIR $APP_HOME
ENTRYPOINT ["sh", "-c"]
CMD ["exec java -jar $APP_FILE"]
Run Code Online (Sandbox Code Playgroud)

使用 docker-compose 我试图使用以下docker-compose.yml将它们联系在一起

version: '3.7'
services:
  cloud-config-service:
    container_name: cloud-config-service
    build:
      context: cloud-config-service
      dockerfile: Dockerfile-cloud-config-service
    image: cloud-config-service:latest
    ports:
      - 8888:8888
    networks:
      - emp-network

  discovery-service:
    container_name: discovery-service
    build:
      context: discovery-service
      dockerfile: Dockerfile-discovery-service
    image: discovery-service:latest
    ports:
      - 8761:8761
    networks:
      - emp-network
    links:
      - cloud-config-service

networks:
  emp-network:
    driver: bridge
Run Code Online (Sandbox Code Playgroud)

起初,我将发现服务配置为从http://localhost:8888获取其配置,但经过一番挖掘,我发现容器中的 localhost 指的是容器本身,并在 Docker 文档中发现服务可以相互引用使用他们的名字。所以我改变了 discovery-service 的属性,从http://cloud-config-service:8888获取它的配置。这不起作用,因此这篇文章。

两个 Dockerfiles 构建和运行都很好,除了发现服务无法在http://cloud-config-service:8888 上获取配置服务

如果我使用host网络驱动程序和http://localhost:8888,它确实有效端点,,但是这“感觉”很古怪,而不是应该如何完成。

我可能遗漏了一些微不足道的东西,但恐怕我找不到什么。

编辑: 发现服务的控制台日志的小片段:

discovery-service       | 2018-10-02 13:14:26.798  INFO 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://cloud-config-service:8888
cloud-config-service    | 2018-10-02 13:14:26.836  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$8a18e3b3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
discovery-service       | 2018-10-02 13:14:27.129  INFO 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://cloud-config-service:8888. Will be trying the next url if available
discovery-service       | 2018-10-02 13:14:27.129  WARN 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://cloud-config-service:8888/discovery-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
Run Code Online (Sandbox Code Playgroud)

Sco*_*and 0

听起来你走在正确的轨道上......我会介绍depends_on告诉容器在启动之前discovery-service等待容器启动的概念cloud-config-service

  discovery-service:
    container_name: discovery-service
    build:
      context: discovery-service
      dockerfile: Dockerfile-discovery-service
    image: discovery-service:latest
    depends_on:
      - cloud-config-service
    ports:
      - 8761:8761
    networks:
      - emp-network
    links:
      - cloud-config-service
Run Code Online (Sandbox Code Playgroud)

在容器可以主动处理流量之前启动容器需要一段有限的时间,因此最好注意这个启动顺序,尤其是在处理数据库容器时尤其如此,其他人应该这样做depends_on