gpt4 book ai didi

c# - 如何解决这个错误 : RabbitMQ. Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable'

转载 作者:行者123 更新时间:2023-12-03 22:16:44 26 4
gpt4 key购买 nike

我在 eshop 订单篮项目中工作。我需要帮助解决此代码中的rabbitmq错误:

using Microsoft.Extensions.Logging; 
using Polly;
using Polly.Retry;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Exceptions;
using System;
using System.IO;
using System.Net.Sockets;

namespace InfiniteWorx.MRU.BuildingBlocks.EventBusRabbitMQ
{
public class DefaultRabbitMQPersistentConnection
: IRabbitMQPersistentConnection
{
private readonly IConnectionFactory _connectionFactory;
private readonly ILogger<DefaultRabbitMQPersistentConnection> _logger;
private readonly int _retryCount;
IConnection _connection;
bool _disposed;
object sync_root = new object();
public DefaultRabbitMQPersistentConnection(IConnectionFactory
connectionFactory, ILogger<DefaultRabbitMQPersistentConnection> logger, int
retryCount = 5)
{
_connectionFactory = connectionFactory ?? throw new
ArgumentNullException(nameof(connectionFactory));
_logger = logger ?? throw new
ArgumentNullException(nameof(logger));
_retryCount = retryCount;
}
public bool IsConnected
{
get
{
return _connection != null && _connection.IsOpen &&
!_disposed;
}
}
public IModel CreateModel()
{
if (!IsConnected)
{
throw new InvalidOperationException("No RabbitMQ connections
are available to perform this action");
}

return _connection.CreateModel();
}

public void Dispose()
{
if (_disposed) return;

_disposed = true;

try
{
_connection.Dispose();

}
catch (IOException ex)
{
_logger.LogCritical(ex.ToString());
}
}

public bool TryConnect()
{
_logger.LogInformation("RabbitMQ Client is trying to connect");

lock (sync_root)
{
var policy = RetryPolicy.Handle<SocketException>()
.Or<BrokerUnreachableException>()
.WaitAndRetry(_retryCount, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
{
_logger.LogWarning(ex.ToString());
}
);

policy.Execute(() =>
{
_connection = _connectionFactory
.CreateConnection();
});

if (IsConnected)
{
_connection.ConnectionShutdown += OnConnectionShutdown;
_connection.CallbackException += OnCallbackException;
_connection.ConnectionBlocked += OnConnectionBlocked;

_logger.LogInformation($"RabbitMQ persistent connection
acquired a connection {_connection.Endpoint.HostName} and is subscribed to
failure events");

return true;
}
else
{
_logger.LogCritical("FATAL ERROR: RabbitMQ connections
could not be created and opened");

return false;
}
}
}

private void OnConnectionBlocked(object sender,
ConnectionBlockedEventArgs e)
{
if (_disposed) return;

_logger.LogWarning("A RabbitMQ connection is shutdown. Trying to
re-connect...");

TryConnect();
}

void OnCallbackException(object sender, CallbackExceptionEventArgs e)
{
if (_disposed) return;

_logger.LogWarning("A RabbitMQ connection throw exception.
Trying to re-connect...");

TryConnect();
}

void OnConnectionShutdown(object sender, ShutdownEventArgs reason)
{
if (_disposed) return;

_logger.LogWarning("A RabbitMQ connection is on shutdown. Trying to re-connect...");

TryConnect();
}
}
}

这是运行代码时的 eroor msg:

RabbitMQ.Client.Exceptions.BrokerUnreachableException: '没有一个指定的端点是可达的'

ArgumentNullException:值不能为空。
ConnectFailureException: 连接失败

最佳答案

我没有看到您在任何地方设置连接参数。
通常:

RabbitMQ.Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable


表示您的连接参数有误,请检查。

正如Aage的评论:

A password with special characters (like an @) can cause this exception as well.



来自 RabbitMQ地点:
ConnectionFactory factory = new ConnectionFactory();
// "guest"/"guest" by default, limited to localhost connections
factory.UserName = user;
factory.Password = pass;
factory.VirtualHost = vhost;
factory.HostName = hostName;

IConnection conn = factory.CreateConnection();
或者:
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = "amqp://user:pass@hostName:port/vhost";

IConnection conn = factory.CreateConnection();
所以,我的猜测是,这些参数之一对您的设置不正确。请检查它们。
请注意:

guest account is only accessible from localhost.


如果可能,您可能需要查看 Masstransit ,它为 MQ 添加了一个抽象层。

关于c# - 如何解决这个错误 : RabbitMQ. Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50016159/

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