运行 docker-compose up 命令时 RabbitMQ 客户端抛出错误

RabbitMQ client throwing error while running docker-compose up command

我正在尝试 运行 docker-与两个具有 rabbitMQ 依赖性的 .net 核心控制台应用程序组合,我正在使用 Docker 和 Windows。

我已添加到控制台应用程序,如 RabbitMQ 官方文档所述https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

我在两个应用程序之外添加了 docker-compose.yml

文件夹结构:

1.发送

  1. Send.cs(在 RabbitMQ 文档中)
  2. Send.cs项目
  3. Docker文件

    FROM mcr.microsoft.com/dotnet/core/sdk:2.2 as build-env
    WORKDIR /app
    
    # Copy the project file and restore the dependencies
    COPY *.csproj ./
    RUN dotnet restore
    
    # Copy the remaining source files and build the application
    COPY . ./
    RUN dotnet publish -c Release -o out
    
    # Build the runtime image
    FROM mcr.microsoft.com/dotnet/core/sdk:2.2
    WORKDIR /app
    COPY --from=build-env /app/out .
    ENTRYPOINT ["dotnet", "Send.dll"]
    

2. 收到

  1. Recieve.cs(在 RabbitMQ 文档中)

  2. Recieve.csproj

  3. Dockerfile(与发件人 Dockerfile 相同,只是 Send 替换为 Recieve)

3。 Docker-compose.yml

  version: '3'

    services:
      rabbitmq:
        image: rabbitmq:3-management
        ports:
          - "5672:5672"
          - "15672:15672"
        container_name: rabbitmq
        hostname: my-rabbit
      Send:
        image: sender
        build:
          context: ./Send
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq
      Reciever:
        image: reciever
        build:
          context: ./Recieve
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq

抛出错误

Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.AggregateException: One or more errors occurred. (Connection failed) ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:5672

为了让Docker个容器能够相互通信,你需要将它们设置在同一个网络中。在您的 docker-compose.yml 添加以下内容:

version: '3'
    services:
      rabbitmq:
        image: rabbitmq:3-management
        ports:
          - "5672:5672"
          - "15672:15672"
        container_name: rabbitmq
        hostname: my-rabbit
        networks:
          - my-network-name
      Send:
        image: sender
        build:
          context: ./Send
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq
        networks:
          - my-network-name
      Reciever:
        image: reciever
        build:
          context: ./Recieve
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq
        networks:
          - my-network-name
networks:
  my-network-name:
    driver: bridge

可以看到,所有的容器都在同一个网络中,可以通过下面的方式进行通信:

http://<container_name>:<port>

因此,如果您希望 SenderRabbitMQ 发送消息,则:

amqp://rabbitmq:5672

如果您使用 localhost 作为任何容器内的 IP,它将使用其环回接口,自己接收请求。

Victor 的回答也很有帮助,但是,问题是 rabbitmq 启动较晚,而发送和接收方启动较早。

当我重新启动同一个停止的发送方和接收方容器时,它按预期工作(因为 rabbitmq 容器已启动)。 所以我在容器上添加了重试策略,从 .

中找到

我的 docker-compose 文件的最终版本如下所示,并且运行良好。 版本:'3' 服务:

  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    container_name: rabbitmq
    hostname: my-rabbit
    networks:
      - my-network-name
    healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:15672"]
        interval: 30s
        timeout: 10s
        retries: 5

  Send:
    image: sender
    build:
      context: ./Send
      dockerfile: Dockerfile
    depends_on:
        - rabbitmq
    restart: on-failure
    networks:
      - my-network-name
    links: 
        - rabbitmq

  Reciever:
    image: reciever
    build:
      context: ./Recieve
      dockerfile: Dockerfile
    depends_on:
        - rabbitmq
    restart: on-failure
    networks:
      - my-network-name
    links: 
        - rabbitmq
networks:
  my-network-name:
    driver: bridge