RabbitMQ: sending messages from one computer to another

I am trying to configure the rabbitMQ queue, which is on one computer, and is receiving messages from other computers, giving them tasks. I followed all the tutorials on the rabbit website, but they only apply to the local host. Can someone explain how I get the same code for communication through 2 computers, and not just from the same computer.

I have the following code:

Sender.cs

class Send
{
    static void Main(string[] args)
    {
        Console.WriteLine("------------------");
        Console.WriteLine("RabbitMQ Test");
        Console.WriteLine("------------------");

        var factory = new ConnectionFactory() { HostName = "localHost" };

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

                    Console.WriteLine("Enter the messages you want to send (Type 'exit' to close program)...");

                    string message = null;
                    while (message != "exit")
                    {
                        message = Console.ReadLine();
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("", "abc", null, body);
                    }

                }
            }
        }
        catch (Exception e)
        {
            string message = e.ToString();
        }
    }

Reciever.cs

    class Recieve
{
    static void Main(string[] args)
    {
        ConnectionFactory factory = new ConnectionFactory()
        {
            HostName = "localhost"
        };

        using (IConnection connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                channel.QueueDeclare("abc", false, false, false, null);

                QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume("abc", 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("[Recieved]: {0}", message);
                }
            }
        }

    }
}

Is it an idea to get these connections through 2 computers to change the ConnectionFactory hostname to the IP address of another computer or something like that? I installed rabbit correctly on both computers, and this code works correctly on each computer separately. I just need to communicate on computers.

. .

+4
2

RabbitMQ - . ( ), . .

, . . .

var server = "http://127.0.0.1"; // An install of RabbitMQ reachable from both clients
var password = "guest";
var username = "guest";
var connectionFactory = new ConnectionFactory { HostName = server, Password = password , Username = username};

- , ZeroMQ

+2

- node , (WAN).

-1

Source: https://habr.com/ru/post/1541228/


All Articles