无法从 docker 将 RabbitMQ 连接到我的应用程序 [重复]

2024-04-07

我目前被这个问题困扰了大约一周,确实找不到合适的解决方案。问题是,当我尝试连接到 dockerized RabbitMQ 时,它每次都会给出相同的错误:

wordofthedayapp-wordofthedayapp-1  | [40m[1m[33mwarn[39m[22m[49m: MassTransit[0]
wordofthedayapp-wordofthedayapp-1  |       Connection Failed: rabbitmq://localhost/
wordofthedayapp-wordofthedayapp-1  |       RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were
 reachable
wordofthedayapp-wordofthedayapp-1  |        ---> System.AggregateException: One or more errors occurred. (Connection failed)
wordofthedayapp-wordofthedayapp-1  |        ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
wordofthedayapp-wordofthedayapp-1  |        ---> System.TimeoutException: The operation has timed out.
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, TimeSpan timeout)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpo
int endpoint, TimeSpan timeout)
wordofthedayapp-wordofthedayapp-1  |          --- End of inner exception stack trace ---
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpo
int endpoint, TimeSpan timeout)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingAddressFamily(AmqpTcpEndpoint end
point, Func`2 socketFactory, TimeSpan timeout, AddressFamily family)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingIPv4(AmqpTcpEndpoint endpoint, Fu
nc`2 socketFactory, TimeSpan timeout)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Impl.SocketFrameHandler..ctor(AmqpTcpEndpoint endpoint, Func`2 socket
Factory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.Framing.Impl.IProtocolExtensions.CreateFrameHandler(IProtocol protoco
l, AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.ConnectionFactory.CreateFrameHandler(AmqpTcpEndpoint endpoint)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, F
unc`2 selector)
wordofthedayapp-wordofthedayapp-1  |          --- End of inner exception stack trace ---
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, F
unc`2 selector)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver
, String clientProvidedName)
wordofthedayapp-wordofthedayapp-1  |          --- End of inner exception stack trace ---
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver
, String clientProvidedName)
wordofthedayapp-wordofthedayapp-1  |          at RabbitMQ.Client.ConnectionFactory.CreateConnection(IList`1 hostnames, String clientPr
ovidedName)
wordofthedayapp-wordofthedayapp-1  |          at MassTransit.RabbitMqTransport.Integration.ConnectionContextFactory.CreateConnection(I
Supervisor supervisor)

在这里你可以找到我的 docker-compose.yml:

version: '3.9'
services:
  rabbitmq:
    image: rabbitmq:3.9-management
    hostname: rabbitmq
    volumes:
      - "~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/"
      - "~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq"
    ports:
      - 5672:5672
      - 15672:15672
    expose:
     - 5672
     - 15672
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
    healthcheck:
        test: [ "CMD", "rabbitmqctl", "status", "-f", "http://localhost:15672"]
        interval: 5s
        timeout: 20s
        retries: 5
    networks:
      - app

  ms-sql-server:
    container_name: ms-sql-server
    image: mcr.microsoft.com/mssql/server:2019-latest
    user: root
    volumes:
      - "appdb:/var/opt/mssql/data"
    environment:
      ACCEPT_EULA: "Y"
      SA_PASSWORD: "Password123!"
      MSSQL_PID: Express
    ports:
      - 1433:1433
    healthcheck:
        test: ["CMD" ,"ping", "-h", "localhost"]
        timeout: 20s
        retries: 10
    networks:
      - app

  wordofthedayapp:
    build:
      dockerfile: WordOfTheDay.Api/Dockerfile
    image: wordofthedayapp
    environment:
      DbServer: "ms-sql-server"
      DbPort: "1433"
      DbUser: "sa"
      Password: "Password123!"
      Database: "appdb"
    ports:
      - 5001:80
    restart: on-failure
    depends_on:
      - rabbitmq
    networks:
      - app

volumes:
  appdb:

networks:
  app:

我的应用程序设置字符串:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "WordContext": "Server=ms-sql-server;Database=master;User=sa;Password=Password123!;MultipleActiveResultSets=true;Integrated Security=false;TrustServerCertificate=true",
    "RabbitMQHost": "amqp://elias:123456@localhost:5672"
  }
}

这是使用 MassTransit 在应用程序中的工作方式:

public static void AddConfiguredMassTransit(this IServiceCollection services, string host)
        {
            services.AddMassTransit(Configuration =>
            {
                Configuration.UsingRabbitMq((context, config) =>
                {
                    config.Host(host);
                });
            });

            services.AddMassTransitHostedService();
        }
services.AddConfiguredMassTransit(Configuration.GetConnectionString("RabbitMQHost"));

我希望至少有人知道这段代码有什么问题,因为我真的厌倦了尝试修复它并浏览互联网寻求解决方案。先感谢您!

附:重要信息!当我在没有 Docker 的情况下在本地测试它时,一切都很完美,但是当我尝试对应用程序进行 dockerize 时,就会发生这种情况。


如果您在本地测试,主机可能localhost因为 Docker 正在将端口暴露给本地计算机。但是,当在容器中运行时,虚拟网络的主机名应为rabbitmq,需要使用它来代替localhost当在同一网络上的容器内运行时。

由于日志显示:

Connection Failed: rabbitmq://localhost/

我猜您在容器内运行时没有更新主机名。

您可以轻松确定您的应用程序是否在容器中运行:

bool IsRunningInContainer => bool.TryParse(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), out var inDocker) && inDocker;

然后,在您的配置中:

var host = IsRunningInContainer ? "rabbitmq" : "localhost";

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法从 docker 将 RabbitMQ 连接到我的应用程序 [重复] 的相关文章

随机推荐

  • DateTimePicker 控件不显示 AM/PM

    我在使用自定义格式 其中包括两个字母的 A M P M 的 DateTimePicker 控件时遇到问题 缩写 使用 en US CultureInfo DateTimeFormat ShortTimePattern 会导致 时 嗯tt 但
  • Python 运行守护进程子进程并读取标准输出

    我需要运行一个程序并将其输出收集到标准输出 该程序 socat 需要在 python 脚本运行期间在后台运行 Socat 一旦运行就会处于守护进程模式 但首先它会将一些行输出到标准输出 我的脚本的其余部分需要这些行 命令 socat d d
  • 如何访问证书扩展(信息)值?

    我有一个由变量访问的 X509Certificate 当我尝试获取证书的详细信息时 我设法通过提供的函数轻松获取 CriticalExtensions 值 但是我想要达到的是存储在证书中并由对象 ID 2 5 29 32 表示的非关键扩展
  • 在 Windows Server 2019 Core 中强制安装不兼容的 .inf 驱动程序 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在尝试在我的第六代 NUC 上安装 Server 2019 Core 尽管安装进展顺利 但当它在没有任何网络连接的情况下启动时 我想起
  • 用 C 处理 TCP 的部分返回

    我一直在读Beej 的网络编程指南 http beej us guide bgnet 获取 TCP 连接的句柄 在其中一个示例中 简单 TCP 流客户端的客户端代码如下所示 if numbytes recv sockfd buf MAXDA
  • 如何序列化和反序列化 JavaScript 对象?

    我需要序列化和反序列化 JavaScript 对象以将它们存储在数据库中 请注意 这些对象包含函数 因此我无法将它们存储为 JSON 因此无法使用 json2 js JavaScript 对象 当然是 JavaScript 的 反 序列化的
  • 在jmeter中设置整个请求url

    我有一个请求 它提供上传网址作为响应正文 uploadUrl https test com 9000 sample uploadurl I m able to extract the uploadUrl using JSON extract
  • Neo4j 中的自动增量

    有没有办法像在 Neo4j 的 MySQL 中一样设置 auto increment 例如 当我使用 GraphDatabaseService 对象创建节点时 我希望节点以 1000000000 等数字开头 非常感谢 看看这个答案 我可以在
  • 从 XSD 生成 Ruby 类

    有没有办法从 XSD 生成 Ruby 类 甚至可能是 ActiveResource 类 以便它们包含将类序列化为对初始 XSD 有效的 xml 的方法 我知道soap4r有xsd2ruby 但似乎生成的ruby类无法轻松序列化为xml 无耻
  • Android:滚动后 RecyclerView 内容混乱[关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我使用 RecyclerView 来显示标记列表 并且值的每个标记都显示为 CardView 但是在RecyclerVi
  • Google 地图 v3 与 MeteorJS 加载同步问题

    我的应用程序在 MeteorJS 框架上运行并使用谷歌地图 javascript api v3 谷歌地图加载方案类似于此中解释的方案post https stackoverflow com a 16797219 942899 和官方的很相似
  • 如何创建 .chartForegroundStyleScale 的动态输入

    在 Swift Charts 中 签名为chartForegroundStyleScale为每个数据系列设置 ShapeStyle 的方法是 func chartForegroundStyleScale
  • 如何在Jenkins中设置环境变量?

    我希望能够做类似的事情 AOEU echo aoeu 并让詹金斯设置AOEU aoeu The 环境变量詹金斯的部分没有这样做 相反 它设置AOEU echo aoeu 如何让 Jenkins 评估 shell 命令并将输出分配给环境变量
  • Elasticsearch 对具有部分边缘 ngram 补全的数组字段的搜索建议

    我正在尝试根据文档中的字符串数组构建一个建议器 它类似于this one https stackoverflow com questions 20789224 elasticsearch autocomplete search on arr
  • 使用自动配置时,Windows 实际上如何检测 LAN(代理)设置

    When Windows Internet 属性 gt 连接 gt LAN 设置 gt 自动配置被设定为 自动检测设置 Windows 实际上如何确定 发现设置是什么 它是网络广播还是对注册表中某处配置的服务器的某种有针对性的查询 还是其他
  • iOS8 委托方法中的 nil 对象 - 自定义键盘

    我正在构建一个自定义键盘 我正在我的中实现以下委托方法InputViewController 但我总是得到 textInput nil void textWillChange id
  • 让AJAX调用等待php中的事件

    我不知道我的瓷砖线是否足够清晰 我的问题是 我有一个 JS 应用程序需要等待服务器上的事件 目前 它每秒通过 XMLHttpRequest 轮询连续的服务器数据 我在想的是 是否可以让调用等待 例如 PHP 中的变量发生变化 我希望我的问题
  • 为什么 jQuery.html() 能够成功接受 jQuery 对象,尽管文档说它不能?

    我只是想确切地了解它是如何工作的 因为我的代码中有一个错误可能与我的误解有关 The docs http api jquery com html html2说htmlString的参数 html htmlString 可以是htmlStri
  • 内迭代

    我有一个 ViewScoped豆与一个List
  • 无法从 docker 将 RabbitMQ 连接到我的应用程序 [重复]

    这个问题在这里已经有答案了 我目前被这个问题困扰了大约一周 确实找不到合适的解决方案 问题是 当我尝试连接到 dockerized RabbitMQ 时 它每次都会给出相同的错误 wordofthedayapp wordofthedayap