Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ works in localhost, but throws BrokerUnreachableException in LAN - (.NET Windows environment.)

Tags:

.net

rabbitmq

I'm trying to work with RabbitMQ in a project.

I've installed RabbitMQ Server rabbitmq-server-3.4.4.exe on Win8 (64bit) PC, which has the IP 192.168.100.6.

I have added a user using rabbitmqctl add_user username password in RabbitMQ command prompt.

Tried to receive the message as follows-

ConnectionFactory factory = new ConnectionFactory();
factory.UserName = "skp";
factory.Password = "111";
factory.VirtualHost = "/";
factory.Protocol = Protocols.DefaultProtocol;
factory.HostName = "localhost";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
        try
        {
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare("hello", false, false, false, null);

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume("hello", true, consumer);

                    Console.WriteLine(" [*] Waiting for messages." +
                                             "To exit press CTRL+C");
                    while (true)
                    {
                        var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x] Received {0}", message);
                    }
                }
            }
        }
        catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
        {
            Console.WriteLine(ex.Message.ToString());
            Console.WriteLine(ex.Message);

            Console.ReadLine();
        }

When the hostname="localhost", it works fine.

But if I try to connect from other PC on LAN and hostname="192.168.100.6", it throws a BrokerUnreachableException - "None of the specified endpoints were reachable"

What I missed here?

UPDATE: Firewall on 192.168.100.6 (RabbitMQ Server) is turned off.

like image 789
s.k.paul Avatar asked Mar 10 '15 09:03

s.k.paul


People also ask

Why is RabbitMQ not working?

Verify Server Configuration Here are the recommended steps: Make sure the node is running using rabbitmq-diagnostics status. Verify config file is correctly placed and has correct syntax/structure. Inspect listeners using rabbitmq-diagnostics listeners or the listeners section in rabbitmq-diagnostics status.

What is the default port for RabbitMQ?

By default, RabbitMQ will listen on port 5672 on all available interfaces. It is possible to limit client connections to a subset of the interfaces or even just one, for example, IPv6-only interfaces.

What is RabbitMQ Amqp connection?

AMQP 1.0 provides a way for connections to multiplex over a single TCP connection. That means an application can open multiple "lightweight connections" called sessions on a single connection. Applications then set up one or more links to publish and consume messages.

How do I connect to RabbitMQ server remotely?

Create new RabbitMQ user and set permissions To create a new RabbitMQ user to access the RabbitMQ server remotely: Open a browser and navigate to http://localhost:15672/. The RabbitMQ Management login screen displays. Log into RabbitMQ using guest as both the username and password.


1 Answers

I think the problem is about the UserName & Password. Follow this instruction to install and run your RabbitMQ Server.

Then browse http://localhost:15672/. This will open the RabbitMQ management login page. Add a user i.e. "abc" with password. Add all the tags shown below i.e. "administrator,management, policymaker".

Now use the following code to your client-

       var factory = new ConnectionFactory() { HostName = "192.168.100.6", Password = "123", UserName = "abc" };

        using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare("hello", false, false, false, null);

                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume("hello", true, consumer);

                Console.WriteLine(" [*] Waiting for messages." +
                                         "To exit press CTRL+C");
                while (true)
                {
                    var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received {0}", message);
                }
            }
        }

Hope, this will solve your problem.

like image 79
Code It Avatar answered Sep 28 '22 15:09

Code It