1

我正在尝试在我的 OS X 开发机器上的 docker 容器中部署我的 JHipster 微服务和注册表。

我使用 JHipster 开箱即用的默认 docker-compose 配置部署注册表:

version: '2'
services:
    jhipster-registry:
        image: jhipster/jhipster-registry:v3.1.0
        volumes:
            - ./central-server-config:/central-config
        # When run with the "dev" Spring profile, the JHipster Registry will
        # read the config from the local filesystem (central-server-config directory)
        # When run with the "prod" Spring profile, it will read the configuration from a Git repository
        # See https://jhipster.github.io/microservices-architecture/#registry_app_configuration
        environment:
            - SPRING_PROFILES_ACTIVE=dev,native
            - SECURITY_USER_PASSWORD=admin
            - SPRING_CLOUD_CONFIG_SERVER_NATIVE_SEARCH_LOCATIONS=file:./central-config/localhost-config/
            # - GIT_URI=https://github.com/jhipster/jhipster-registry/
            # - GIT_SEARCH_PATHS=central-config
        ports:
            - 8761:8761

但是,当我使用 部署我的微服务docker run时,会发生以下两种情况之一:

如果我发布了我想让微服务可用的端口,-p 8080:8080以便我可以通过浏览器访问它,我可以访问它,但微服务找不到注册表。

Could not locate PropertySource: I/O error on GET request for "http://jhipster-registry:8761/config/clientaggregator/dev,twilio": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

同时,我可以查看注册表服务正常的页面。

我可以通过在启动微服务时添加“--network=host”来解决这个问题。但是,当我这样做时,这显然会覆盖本机到主机的端口映射,并且无法从浏览器访问微服务。

更奇怪的是,大约一周前,我使用了完全相同的配置,效果很好。

在此处输入图像描述

如果我在 docker 容器之外运行我的应用程序,它会很好地连接到注册表。如果我创建一个不同的容器,或者连接到微服务容器并通过 curl 访问配置 url,我会收到响应。

4

1 回答 1

0

您的应用程序正在尝试使用名称jhipster-registry作为主机名来定位注册表。为此,您需要将注册表和应用程序添加到 docker 网络。

首先使用以下命令创建网络:

docker network create my-network

通过为容器命名并将其添加到网络创建来更新 compose 文件:

version: '2'
services:
    jhipster-registry:
        image: jhipster/jhipster-registry:v3.1.0
        volumes:
            - ./central-server-config:/central-config
        # When run with the "dev" Spring profile, the JHipster Registry will
        # read the config from the local filesystem (central-server-config directory)
        # When run with the "prod" Spring profile, it will read the configuration from a Git repository
        # See https://jhipster.github.io/microservices-architecture/#registry_app_configuration
        environment:
            - SPRING_PROFILES_ACTIVE=dev,native
            - SECURITY_USER_PASSWORD=admin
            - SPRING_CLOUD_CONFIG_SERVER_NATIVE_SEARCH_LOCATIONS=file:./central-config/localhost-config/
            # - GIT_URI=https://github.com/jhipster/jhipster-registry/
            # - GIT_SEARCH_PATHS=central-config
        ports:
            - 8761:8761
        networks:
            - my-network
        container_name: jhipster-registry


networks:
  my-network:
    external: true

运行应用程序时指定网络:

docker run --network=my-network ...

jhipster-registry现在您的应用程序可以使用主机名与注册表进行通信。

于 2017-09-05T15:08:56.887 回答