Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解决此错误:RabbitMQ.Client.Exceptions.brokerRunReachableException:&x27;指定的端点均不可访问';_C#_Asp.net_.net_Rabbitmq - Fatal编程技术网

C# 如何解决此错误:RabbitMQ.Client.Exceptions.brokerRunReachableException:&x27;指定的端点均不可访问';

C# 如何解决此错误:RabbitMQ.Client.Exceptions.brokerRunReachableException:&x27;指定的端点均不可访问';,c#,asp.net,.net,rabbitmq,C#,Asp.net,.net,Rabbitmq,我在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 Syste

我在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(); 
        } 
    } 
} 
使用Microsoft.Extensions.Logging;
使用Polly;
使用Polly.Retry;
使用RabbitMQ.Client;
使用RabbitMQ.Client.Events;
使用RabbitMQ.Client.Exceptions;
使用制度;
使用System.IO;
使用System.Net.Sockets;
命名空间InfiniteWorx.MRU.BuildingBlocks.EventBusRabbitMQ
{ 
公共类DefaultRabbitMQPersistentConnection
:IRabbitMQPersistentConnection
{ 
专用只读IConnectionFactory\u connectionFactory;
专用只读ILogger\u记录器;
私有只读int_retryCount;
i连接\u连接;
布卢;
对象同步_根=新对象();
公共DefaultRabbitMQPersistentConnection(IConnectionFactory
连接工厂,ILogger记录器,内部
retryCount=5)
{ 
_connectionFactory=connectionFactory??抛出新
ArgumentNullException(名称(连接工厂));
_记录器=记录器??抛出新的
ArgumentNullException(name of(logger));
_retryCount=retryCount;
} 
公共图书馆断开了
{ 
得到
{ 
返回_connection!=null&&u connection.IsOpen&&
!!;
} 
} 
公共IModel CreateModel()
{ 
如果(!未连接)
{ 
抛出新的InvalidOperationException(“无RabbitMQ连接
可以执行此操作);
} 
返回_connection.CreateModel();
} 
公共空间处置()
{ 
如果(_)返回;
_这是真的;
尝试
{ 
_connection.Dispose();
} 
捕获(IOEX异常)
{ 
_LogCritical(例如ToString());
} 
} 
公共bool TryConnect()
{ 
_logger.LogInformation(“RabbitMQ客户端正在尝试连接”);
锁定(同步\u根)
{ 
var policy=RetryPolicy.Handle()
.或()
.WaitAndRetry(_retryCount,retrytry=>
TimeSpan.FromSeconds(数学功率(2,重试尝试)),(例如,时间)=>
{ 
_logger.LogWarning(例如ToString());
} 
); 
policy.Execute(()=>
{ 
_连接=\u连接工厂
.CreateConnection();
}); 
如果(未连接)
{ 
_connection.ConnectionShutdown+=OnConnectionShutdown;
_connection.CallbackException+=OnCallbackException;
_connection.ConnectionBlocked+=OnConnectionBlocked;
_logger.LogInformation($”RabbitMQ持久连接
已获取连接{u connection.Endpoint.HostName},并已订阅
故障事件);
返回true;
} 
其他的
{ 
_LogCritical(“致命错误:RabbitMQ连接
无法创建和打开“);
返回false;
} 
} 
} 
连接被阻止的私有void(对象发送方,
连接阻塞事件(e)
{ 
如果(_)返回;
_logger.LogWarning(“RabbitMQ连接已关闭。正在尝试
重新连接…);
TryConnect();
} 
void OnCallbackException(对象发送方,CallbackExceptionEventArgs e)
{ 
如果(_)返回;
_LogWarning(“RabbitMQ连接引发异常。
正在尝试重新连接…);
TryConnect();
} 
无效OnConnectionShutdown(对象发送方、ShutdownEventArgs原因)
{ 
如果(_)返回;
_logger.LogWarning(“RabbitMQ连接正在关闭。正在尝试重新连接…”);
TryConnect();
} 
} 
} 
当运行代码时,这是ERO或msg:

RabbitMQ.Client.Exceptions.BrokerRunReachableException:'指定的终结点均不可访问'

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

我看不到您在任何地方设置了连接参数

通常:

RabbitMQ.Client.Exceptions.BrokerRunReachableException:'指定的终结点都不可访问

表示您的连接参数不正确,请检查它们


根据Aage的评论:


带有特殊字符(如@)的密码也可能导致此异常


从网站:

或者:

ConnectionFactory factory = new ConnectionFactory();
factory.Uri = "amqp://user:pass@hostName:port/vhost";

IConnection conn = factory.CreateConnection();
所以,我猜,其中一个参数对于您的设置是不正确的。请检查一下

请注意:

来宾帐户只能从本地主机访问


如果可能的话,您可能需要检查,这将为MQ添加一个抽象层。

这一点非常清楚。您的Rabbitconnectionstring不正确或无法访问终结点。请检查您的连接参数、端口、登录名、虚拟主机和防火墙。请建议我如何使用此连接代码。。执行(()=>{u connection=\u connectionFactory.CreateConnection();})+1表示“只能从本地主机访问来宾帐户”。是否可以从容器内部授予来宾帐户访问权限?只有当它被识别为“localhost”时,我不确定在何处进行此检查。无论如何其想法是创建其他用户(并可能一起禁用来宾帐户)。创建add非常容易
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = "amqp://user:pass@hostName:port/vhost";

IConnection conn = factory.CreateConnection();