2021-05-12 14:32:11
在CentOS7上安裝RabbitMQ 詳解
Linux下安裝RabbitMQ過程參考官網:
Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE)
首先需要安裝erlang,參考:http://fedoraproject.org/wiki/EPEL/FAQ
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了。
CentOS 7.2 下 RabbitMQ 叢集搭建 http://www.linuxidc.com/Linux/2016-12/137812.htm
CentOS7環境安裝使用專業的訊息佇列產品RabbitMQ http://www.linuxidc.com/Linux/2016-11/13673.htm
RabbitMQ入門教學 http://www.linuxidc.com/Linux/2015-02/113983.htm
相關文章