標籤:style blog http color io os ar strong for
1、下載RabbitMQ Server安裝包
Ubuntu環境下的安裝包rabbitmq-server_3.3.5-1_all.deb的在這裡:http://www.rabbitmq.com/install-debian.html
Ubuntu自9.04版本之後預設會安裝RabbitMQ,但是版本會較老,所以我們需要安裝上面的最新的安裝包。
下載後直接雙擊安裝即可,Erlang環境都是有的。
安裝好了之後啟動命令是:
invoke-rc.d rabbitmq-server start
停止命令是:
invoke-rc.d rabbitmq-server stop
2、編寫.Net用戶端測試程式
發送程式Send.cs代碼是:
namespace RabbitMQ.SendReceive{ class Program { static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory() { HostName = "192.168.1.103" }; using (IConnection conn = factory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.QueueDeclare("hello", false, false, false, null); string mesg = "hello RabbitMQ"; byte[] body = Encoding.UTF8.GetBytes(mesg); channel.BasicPublish("", "hello", null, body); Console.WriteLine(" [x] Sent {0}", mesg); } } } }}
接收程式Receive.cs代碼是:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using RabbitMQ.Client;using RabbitMQ.Client.Events;namespace RabbitMQ.SendReceive{ class Program { static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory() { HostName = "192.168.1.103" }; using (IConnection conn = factory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.QueueDeclare("hello", false, false, false, null); QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel); channel.BasicConsume("hello", true, consumer); Console.WriteLine(" [*]Waiting for message..."); while (true) { var queueItem = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); byte[] body = queueItem.Body; string mesg = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received {0}", mesg); } } } } }}注意需要先在這裡(地址)下載rabbitmq-dotnet-client-3.3.5-dotnet-3.0,然後引入RabbitMQ.Client.dll和RabbitMQ.ServiceModel.dll兩個DLL。
3、運行測試代碼
我的測試代碼是跑在Windows7上面的,而RabbitMQ Broker安裝在Ubuntu的虛擬機器上面,以橋接方式進行網路連接。
運行代碼會出現訪問被拒絕的異常。官網上有一段話:
Default user access
The broker creates a user guest with password guest. Unconfigured clients will in general use these credentials. By default, these credentials can only be used when connecting to the broker as localhost so you will need to take action before connecting fromn any other machine.
意思是說未配置的用戶端預設會以guest的身份(密碼也是guest)作為訪問訊息中心(RabbitMQ Broker)的驗證,但是這隻在client和RabbitMQ Broker在同一台機器上的時候才有效,即才能夠訪問,要是跨機器,還需要做其他工作。
3.1、首先來看如何解決guest使用者只能本機訪問的問題:
"guest" user can only connect via localhost
By default, the guest user is prohibited from connecting to the broker remotely; it can only connect over a loopback interface (i.e. localhost). This applies both to AMQP and to any other protocols enabled via plugins. Any other users you create will not (by default) be restricted in this way.
This is configured via the loopback_users item in the configuration file.
If you wish to allow the guest user to connect from a remote host, you should set the loopback_users configuration item to []. A complete rabbitmq.config which does this would look like:
[{rabbit, [{loopback_users, []}]}].
這個上面已經說得很清楚了,預設情況下RabbitMQ的配置中loopback_users這個配置項需要修改為空白[],因為預設的是配置為guest了,修改為空白之後就沒有guest只能本機訪問這個限制了。
每一種發布版設定檔所在的位置都不一樣,下面是常見的作業系統設定檔所在位置:
- Generic UNIX - $RABBITMQ_HOME/etc/rabbitmq/
- Debian/Ubuntu - /etc/rabbitmq/
- RPM - /etc/rabbitmq/
- Mac OS X (Macports) - ${install_prefix}/etc/rabbitmq/, the Macports prefix is usually /opt/local
- Windows - %APPDATA%\RabbitMQ\
假如預設位置沒有設定檔,自己建立一個即可。
我的Ubuntu就沒有設定檔,我就自己建立了一個,內容只需要寫上我要修改的這個配置項即可。
這樣我WIndows7上面的測試代碼就可以正常運行了!!!
3.2 其他方案:
It is advisable to delete the guest user or change the password to something private, particularly if your broker is accessible publicly.
可以看到一般來說建議刪掉guest使用者,或者修改guest使用者的代碼。
未完待續。。。
3、運行測試代碼
RabbitMQ學習(七)Ubuntu環境安裝與.NET用戶端測試