Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve this error: RabbitMQ.Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable'

i'm working in eshop order basket project. i want help for solving rabbitmq error from this code:

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(); 
        } 
    } 
} 

This is eroor msg when running the code:

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

ArgumentNullException: Value cannot be null. ConnectFailureException: Connection failed

like image 672
Suramya Patel Avatar asked Apr 25 '18 07:04

Suramya Patel


People also ask

How many connections can RabbitMQ handle?

Below is the default TCP socket option configuration used by RabbitMQ: TCP connection backlog is limited to 128 connections.

What is the default port for RabbitMQ?

By default, RabbitMQ will listen on port 5672 on all available interfaces.

What is RabbitMQ client?

The RabbitMQ Java client library allows Java and JVM-based applications to connect to and interact with RabbitMQ nodes. 5. x release series of this library require JDK 8, both for compilation and at runtime. On Android, this means only Android 7.0 or later versions are supported.


1 Answers

I don't see you set your connection parameters anywhere.

Typically:

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

means that your connection parameters are incorrect, so please check them.


As by Aage's comment:

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


From the RabbitMQsite:

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();

Or alternatively:

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

IConnection conn = factory.CreateConnection();

So, my guess is, that one of these parameters are incorrect for your setup. Please check them.

Please note:

guest account is only accessible from localhost.

If possible, you might want to check Masstransit, which adds an abstraction layer to the MQ.

like image 164
Stefan Avatar answered Sep 25 '22 03:09

Stefan