【问题标题】:Error in Connecting to RabbitMQ from NET application从 NET 应用程序连接到 RabbitMQ 时出错
【发布时间】:2022-12-03 01:58:53
【问题描述】:

我的后端服务在 .NET 中,我在 docker 容器中运行它。 此外,rabbitmq 在 container 中运行。

在运行服务时,它从 .NET 应用程序中抛出以下错误消息:

  There was error while connecting Rabiitmq Broker.
      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.Sockets.SocketException (111): Connection refused
         at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
         at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
         at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
      --- End of stack trace from previous location ---
         at RabbitMQ.Client.Impl.TcpClientAdapter.ConnectAsync(String host, Int32 port)
         at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, TimeSpan timeout)
         at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, TimeSpan timeout)
         --- End of inner exception stack trace ---
         at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, TimeSpan timeout)



 var factory = new ConnectionFactory { HostName = hostName, Port = 5672 };
  factory.UserName = PathManager.DataPathManager.GetRabbitmqUserName();
  factory.Password = PathManager.DataPathManager.GetRabbitmqPassword();

             
  _logger.LogInformation($"{module} > ExecuteAsync > factory object created");
                var connection = factory.CreateConnection();
                _logger.LogInformation($"{module} > ExecuteAsync > CreateConnection() method called");

  channel = connection.CreateModel();
  _logger.LogInformation($"{module} > ExecuteAsync > CreateModel() method called");

我能够发送消息但无法阅读它。但是,在接收端得到这个

  There was error while synchronizing with Broker queue.
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Indus.Product.Manager.Managers.ProcessEventHandler.Receive(IModel channel) in /agent/work/5/s/Service/Source/MicroServices/Services/Product/Manager/Managers/Handler.cs:line 128
     at Indus.Product.Manager.SynchronisationWorker.ExecuteAsync(CancellationToken stoppingToken) in /agent/work/5/s/Service/Source/MicroServices/Services/Product/Manager/Handler.cs:line 96   

【问题讨论】:

    标签: c# rabbitmq


    【解决方案1】:

    此错误消息表明 .NET 应用程序无法连接到容器中运行的 RabbitMQ 代理。这可能是由于多种原因造成的,例如未正确配置代理、网络连接问题或无法从 .NET 应用程序访问代理。

    要解决此问题,您可以尝试以下步骤:

    1. 验证 RabbitMQ 代理正在运行并且可以从 .NET 应用程序访问。您可以尝试使用 rabbitmqctl 命令行工具连接到代理,或者使用 telnet 之类的工具在正确的端口(通常是 5672)上建立到代理的连接。

    2. 确保使用正确的主机名和端口连接到 RabbitMQ 代理。在您提供的代码 sn-p 中,主机名设置为 hostName,端口设置为 5672。如果代理在不同的主机或端口上运行,您将需要相应地更新这些值。

    3. 检查 RabbitMQ 代理配置以确保它允许来自 .NET 应用程序的连接。默认情况下,RabbitMQ 代理将只接受来自本地主机的连接,因此您可能需要将 .NET 应用程序的主机名或 IP 地址添加到 rabbitmq.config 文件中的允许连接列表中。

    4. 如果您使用防火墙或其他网络安全措施,请确保它们不会阻止与 RabbitMQ 代理的连接。您可能需要为 RabbitMQ 代理的端口(通常是 5672)添加一个例外,以允许 .NET 应用程序连接。

      检查完这些项目后,您应该能够连接到 RabbitMQ 代理并发送和接收消息。如果您仍然遇到问题,在 RabbitMQ 代理和 .NET 应用程序中启用日志记录可能会有所帮助,以获取有关连接失败的更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多