首页 > 代码库 > RabbitMQ学习(七)Ubuntu环境安装与.NET客户端测试
RabbitMQ学习(七)Ubuntu环境安装与.NET客户端测试
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用户的代码。
未完待续。。。
RabbitMQ学习(七)Ubuntu环境安装与.NET客户端测试