gpt4 book ai didi

docker - RabbitMq-Docker-compose-如何修复 'Connection refused 127.0.0.1:5672'异常

转载 作者:行者123 更新时间:2023-12-02 21:04:40 24 4
gpt4 key购买 nike

我已经在Stack Overflow上尝试了几种可能的解决方案,但是似乎没有任何效果。

我正在使用RabbitMQ开发微服务。该解决方案包含多个项目,并且可以正常运行,但是一旦我使用docker-compose选项构建项目,Visual Studio就会引发以下异常:

RabbitMQ.Client.Exceptions.BrokerUnreachableException ExtendedSocketException: Connection refused 127.0.0.1:5672



在我的解决方案中,我有三个项目通过RabbitMQ相互通信。

以下是我的YAML文件的代码。

我的docker-compose.yaml:
version: '3.4'

services:
rabbitmq:
hostname: webnet
image: rabbitmq:3.7.2-management
ports:
- "15672:15672"
- "5672:5672"
networks:
- webnet

sql-server-db:
container_name: sql-server-db
image: microsoft/mssql-server-linux:2017-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "customerdbalten@123"
ACCEPT_EULA: "Y"
networks:
- webnet

myproject.simulation.api:
image: ${DOCKER_REGISTRY-}myprojectsimulationapi
build:
context: .
dockerfile: myproject.Simulation.Api/Dockerfile
links:
- rabbitmq
ports:
- '5000'
networks:
- webnet

myproject.updateservice.api:
image: ${DOCKER_REGISTRY-}myprojectupdateserviceapi
build:
context: .
dockerfile: myproject.updateservice.Api/Dockerfile
links:
- rabbitmq
- sql-server-db
ports:
- '5050'
networks:
- webnet

myproject.web:
image: ${DOCKER_REGISTRY-}myprojectweb
build:
context: .
dockerfile: MyProject.Web/Dockerfile
links:
- rabbitmq
ports:
- '5001'
networks:
- webnet

networks:
webnet:
driver: bridge

我的Docker文件:

我的Dockerfile如下所示:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY
MyProject.UpdateService.Api/MyProject.UpdateService.Api.csproj MyProject.UpdateService.Api/
COPY MyProject.Common/MyProject.Common.csproj MyProject.Common/
RUN dotnet restore MyProject.UpdateService.Api/MyProject.UpdateService.Api.csproj
COPY . .
WORKDIR /src/MyProject.UpdateService.Api
RUN dotnet build MyProject.UpdateService.Api.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish MyProject.UpdateService.Api.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.UpdateService.Api.dll"]


我还创建了另一个简单的解决方案,仅使用两个项目-发送者和接收者-使用RabbitMQ。此解决方案在 docker-compose编码时抛出相同的异常,否则,它将运行。 YAML文件只包含自动生成的代码。

最佳答案

从讨论中,我们发现RabbitMQ容器未运行,因为主机上已经在运行RabbitMQ服务。

您有两种选择,停止主机RabbitMQ服务,然后尝试与RabbitMQ容器连接。

Hostname: rabbitmq:5672

或者,如果您想连接Host RabbitMQ服务,则可以使用
Hostname: host.docker.internal
#or
Hostname: HOST_IP


我想从主机上的容器连接到服务

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name `host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.



网关也可以通过 gateway.docker.internal.到达

docker-for-windows-networking

关于docker - RabbitMq-Docker-compose-如何修复 'Connection refused 127.0.0.1:5672'异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58798907/

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