1. 程式人生 > >在CentOS7上安裝RabbitMQ

在CentOS7上安裝RabbitMQ

pack wiki 用戶名 inf rec tag nec open sicp

Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE)

首先需要安裝erlang,參考:http://fedoraproject.org/wiki/EPEL/FAQ#howtouse

rpm -Uvh http://download.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
yum install erlang

安裝過程中會有提示,一路輸入“y”即可。

完成後安裝RabbitMQ:

先下載rpm:

wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm

下載完成後安裝:

yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm 

完成後啟動服務:

service rabbitmq-server start

可以查看服務狀態:

service rabbitmq-server status

技術分享圖片

這裏可以看到log文件的位置,轉到文件位置,打開文件:

技術分享圖片

這裏顯示的是沒有找到配置文件,我們可以自己創建這個文件

cd /etc/rabbitmq/
vi rabbitmq.config

編輯內容如下:

[{rabbit, [{loopback_users, []}]}].

這裏的意思是開放使用,rabbitmq默認創建的用戶guest,密碼也是guest,這個用戶默認只能是本機訪問,localhost或者127.0.0.1,從外部訪問需要添加上面的配置。

保存配置後重啟服務:

service rabbitmq-server stop
service rabbitmq-server start

此時就可以從外部訪問了,但此時再看log文件,發現內容還是原來的,還是顯示沒有找到配置文件,可以手動刪除這個文件再重啟服務,不過這不影響使用

rm rabbit\@mythsky.log 
service rabbitmq-server stop
service rabbitmq-server start

開放5672端口:

firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --reload 

在Windows上進行測試:

新建.net core控制臺項目,引用RabbitMQ.Client包:

Install-Package RabbitMQ.Client

測試代碼:

技術分享圖片
public static void Main(string[] args)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.UserName = "guest";
            factory.Password = "guest";
            factory.VirtualHost = "/";
            factory.HostName = "localhost";
            //factory.HostName = "10.255.19.111";
            try
            {
                IConnection conn = factory.CreateConnection();
                IModel model = conn.CreateModel();
                string exchangeName = "test";
                string queueName = "testq";
                string routingKey = "first";

                model.ExchangeDeclare(exchangeName, ExchangeType.Direct);
                model.QueueDeclare(queueName, false, false, false, null);
                model.QueueBind(queueName, exchangeName, routingKey, null);
                byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
                model.BasicPublish(exchangeName, routingKey, null, messageBodyBytes);
                Console.WriteLine("message sended.");

                bool noAck = false;
                BasicGetResult result = model.BasicGet(queueName, noAck);
                if (result == null)
                {
                    Console.Write("no message.");
                }
                else
                {
                    IBasicProperties props = result.BasicProperties;
                    byte[] body = result.Body;
                    model.BasicAck(result.DeliveryTag, false);
                    string message = System.Text.Encoding.UTF8.GetString(body);
                    Console.Write(message);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
技術分享圖片

也可以使用官網的例子(這裏更清晰):

http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

發送端:

技術分享圖片
using System;
using RabbitMQ.Client;
using System.Text;

class Send
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            string message = "Hello World!";
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "",
                                 routingKey: "hello",
                                 basicProperties: null,
                                 body: body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }
}
技術分享圖片

接收端:

技術分享圖片
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

class Receive
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: "hello",
                                 noAck: true,
                                 consumer: consumer);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }
    }
}
技術分享圖片

在Windows上發送,在CentOS上接收,效果如圖:

技術分享圖片

開啟管理UI:

rabbitmq-plugins enable rabbitmq_management
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload

在Windows下打開地址:

http://10.255.19.111:15672

技術分享圖片

用戶名和密碼都是 guest

技術分享圖片

這樣就可以方便管理RabbitMQ了。

在CentOS7上安裝RabbitMQ