Using .NET to consume RabbitMQ on a Raspberry Pi cluster

I got my hands on a couple more Raspberry Pi’s. I want to try to get a handle on a couple of clustering scenarios (beowulf/MPI, Hadoop, etc). One of the easier ones I thought I’d tackle first is RabbitMQ. If you are not familiar, a “message queue” system is a place where applications can drop off messages – and other applications can then pick up those messages. A queue is good for systems who have different downtime, or it’s a good mechanism is you have a bajillion requests you need to process and you simply want to queue them up – because the writes to the database take too long for the client.

Anyhow – perhaps you’ve used Microsoft MQ or if you are lucky, IBM MQ (that was sarcasm, by the way). This is no different really. You’d “cluster” or load-balance queues so that they can be fault-tolerant. One of the servers who supports the queue can go down, and the queue is still available. That is what I hope to achieve here, and to be able to connect to this cluster from .NET.

Why use Rabbit MQ?
It’s open source and commercially supported, it’s supported on pretty much every platform that exists – and it’s pretty time-tested by now. In the message queue wars, RabbitMQ seems to have won.

The Raspberry Pi setup:
I have 3 PI’s dedicated for this little project, they are named: raspicluster01, raspicluster02, and raspicluster03 (“raspi” is short for Raspberry Pi). They are running “wheezy” (Debian) Linux. They are hooked up to the same network with SSH enabled on each. Aside from that, there are no other prerequisites.

Getting RabbitMQ installed on each system:
Because I want to use “apt-get” to install this, I need to add their repository to my list of repositories. I just followed the instructions from here, with no issues. So, edit this file:

sudo nano /etc/apt/sources.list

and add the following line:

deb testing main

Then, simply accept the signing key, update the repository list, and install the product:

sudo wget
sudo apt-key add rabbitmq-signing-key-public.asc
sudo apt-get update
sudo apt-get install rabbitmq-server

before I went any further, I knew that RabbitMQ has a really nice web UI, a “management plug-in” – how do I install that? Turns out it’s already installed, but I need to enable it. To do that, I run:

sudo rabbitmq-plugins enable rabbitmq_management

Now, I have a very pretty UI to interact with the MQ server on each server:


Logging in to the web interface:
On each server where you’ve done that, you should be able to navigate to http://{{servername}}:15672/ which is the management console URL (like you see above). According to that same documentation, I should be able to login to this console with username “guest” and password “guest”, but I got “login failed”. After some research, I ran across this post which explains that with the latest version, they added a security feature.

So, a quick/easy way to fix this is to create a new account – I just created a test account like this:

sudo rabbitmqctl add_user test test
sudo rabbitmqctl set_user_tags test administrator
sudo rabbitmqctl set_permissions -p / test “.*” “.*” “.*”

that creates a user/pass of test/test and basically gives it administrative rights on the RabbitMQ server (within the software, that is). Now, from the web page I can log in and do things!

Connecting to a queue from .NET
I created a console program in Visual Studio, added a NuGet package called RabbitMQ.Client:


then, I just followed the sample code from this page:

Now, it wasn’t QUITE that simple. In fact, I got a bunch of errors if I made the queue ahead of time, but with no queue previously defined, here is the code for putting a message on a queue:

and here is the code for pulling messages off the queue. It’s similar to the example, but I needed to specify credentials (again, new security features):

(you can download the source for this here)

Setting up the RabbitMQ cluster:
WIth the above in place, I can put messages on a queue, take them off – and see the status on the web page while I’m doing that. Now, I want to cluster these 3 nodes so that they act as one. I followed the documentation here and it was very good. The good news is there is only one “gotcha”, and this is otherwise very simple.

How the nodes “authenticate” with the master node is that they need to have the “cookie” file. So, on your first node – EDIT the file so that it’s JUST that value:

sudo nano /var/lib/rabbitmq/.erlang.cookie

One problem I had was that the file – like if you did “cat .erlang.cookie” – it didn’t have a proper CR/LF in the file, so I couldn’t reproduce it on the other nodes. So, make this a proper text file on the first node, then edit the same file on your other nodes. Be careful to not have any extra spaces, tabs, or carriage returns.

The end-result being that the .erlang.cookie file is identical on all cluster nodes.

Then, reboot. The documentation doesn’t tell you this, but I got TONS of errors after doing this. When I reboot, it fixed everything. When you come back from a reboot, on node2 and node3 – run the following the join node1 in a cluster:

sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster –ram rabbit@raspicluster01
sudo rabbitmqctl start_app

Before, during, and after – back on your first node, you can run:

sudo rabbitmqctl cluster_status

and you should see it go from one node, to 3 nodes in this case:


also, from the web UI, I now see the other nodes:


and if a node fails, it knows immediately and takes it out of the rotation:


Lastly, I went back to the .NET code – I could run that same code and point to ANY node and the code still ran. However, all the messages seem to go to one node, but I don’t know if that’s something I’m doing wrong? Either way, I just wanted to get a basic implementation of this up and running, set up a cluster, and be able to put and get messages from .NET. All-in-all, this was pretty easy and straight-forward to do.

Also again, you can get the .NET code from here.

Posted in Cloud Computing, Computers and Internet, Development Tools, General, Infrastructure, Linux, MSMQ, New Technology, Raspberry Pi, Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: