gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:47:53 25 4
gpt4 key购买 nike

我正在尝试使用两个依赖于rabbitMQ的.net core控制台应用程序运行docker-compose,我在Windows上使用Docker。

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

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

文件夹结构:

1.发送

  1. Send.cs(如 RabbitMQ 文档中所示)
  2. 发送.csproj
  3. Dockerfile

    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. 接收.csproj

  3. Dockerfile(与 Sender 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>

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

amqp://rabbitmq:5672

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

关于docker - RabbitMQ 客户端在运行 docker-compose up 命令时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56901780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com