Hi there,
This blog entry is to make you comfortable working with Drizzle’s RabbitMQ plugin. I will also demonstrate how this can be used dynamically.
Introduction
RabbitMQ is a message broker. It is like a postoffice which accepts and forwards the messages to different queues. There may be applications built over these queues which receive the messages and process them.
RabbitMQ plugin registers few system variable with drizzle client, namely,
- logging_enable: This shows if logging function is enabled or not. If disabled, messages will not be forwarded to rabbitMQ server.
- host: Specifies the host name for rabbitMQ server. IP address of server can also be provided.
- port: Port on which rabbitMQ is running. Default port is 5672
- username and password: Logging credentials required for access to rabbitMQ server
- virtualhost: This specifies the virtual host in which the client intend to operate. (I am not very sure about this.).
- exchange: This specifies the name of the rabbitMQ exchange. No message is directly pushed to the desired queue. Instead, they are redirected to exchanges. It is the job of exchanges to decide which queue it wants to push the message into.
- routingkey: This is the name of the queue to which we wants to push the message into.
Plugin in Action
First make sure that rabbitmq server and corresponding libraries are installed on your system. Refer here for more details. Now you will have to rebuild drizzle to build rabbitmq plugin. Start drizzle server with rabbitmq plugin loaded.
Now we will start drizzle and check the corresponding variables registered.
Welcome to the Drizzle client.. Commands end with ; or \g.
Your Drizzle connection id is 2
Connection protocol: mysql
Server version: 7.2.2.2569-snapshot Source distribution (rabbitmq_dynamic)Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.drizzle> SHOW VARIABLES LIKE “%rabbitmq%”;
+————————-+———————–+
| Variable_name | Value |
+————————-+———————–+
| rabbitmq_exchange | ReplicationExchange |
| rabbitmq_host | localhost |
| rabbitmq_logging_enable | ON |
| rabbitmq_password | guest |
| rabbitmq_port | 371725864 |
| rabbitmq_routingkey | ReplicationRoutingKey |
| rabbitmq_username | guest |
| rabbitmq_virtualhost | / |
+————————-+———————–+
8 rows in set (0.001047 sec)
These all are the default variables for the rabbitMQ plugin. We can also pass the desired values when starting the server.
Now lets try changing some variable to new value. Lets change the name of exchange to “MyOwnExchange”
ERROR 1210 (HY000): Incorrect arguments to SET
Whoa! What is this error? The variable value is perfectly valid. Well, on going to the server console, you will figure out the error.
Value of variables cannot be changed when plugin is loaded. So, as the plugin suggests, lets stop the plugin first and then change the value.
Query OK, 0 rows affected (0.001198 sec)drizzle> SET GLOBAL rabbitmq_exchange=”MyOwnExchange”;
Query OK, 0 rows affected (0.000236 sec)
drizzle> SHOW VARIABLES LIKE “%rabbitmq%”;
+————————-+———————–+
| Variable_name | Value |
+————————-+———————–+
| rabbitmq_exchange | MyOwnExchange |
| rabbitmq_host | localhost |
| rabbitmq_logging_enable | OFF |
| rabbitmq_password | guest |
| rabbitmq_port | 371725864 |
| rabbitmq_routingkey | ReplicationRoutingKey |
| rabbitmq_username | guest |
| rabbitmq_virtualhost | / |
+————————-+———————–+
8 rows in set (0.00106 sec)
drizzle> SET GLOBAL rabbitmq_logging_enable=true;
Query OK, 0 rows affected (0.003609 sec)
No error! π
This is all from database point of view. But how to be sure, if messages are sent to rabbitMQ server or not? In the rabbitmq c library, there are few codes in /examples/ which can be used for this purpose. Open a separate terminal, go to the examples folder of c library and type
This program will now listen for all the messages sent to rabbitMq server’s MyOwnExchange. Now lets query something and check if it actually works.
Query OK, 1 row affected (0.121521 sec)
On the listener terminal..
ansh@ubuntu:~/Desktop/rabbitmq/rabbitmq-c/examples$ ./amqp_listen localhost 5672 MyOwnExchange ReplicationRoutingKey
Result 0
Frame type 1, channel 1
Method AMQP_BASIC_DELIVER_METHOD
Delivery 1, exchange MyOwnExchange routingkey ReplicationRoutingKey
—-
00000000: 0A 18 08 01 10 83 BE 04 : 18 9D CC E7 B9 A4 D5 B1 …………….
00000010: 02 20 F6 C7 EB B9 A4 D5 : B1 02 12 72 08 01 10 A6 . ………r….
00000020: CC E7 B9 A4 D5 B1 02 18 : D7 CC E7 B9 A4 D5 B1 02 …………….
00000030: 2A 36 0A 11 0A 0B 64 72 : 69 7A 7A 6C 65 73 6C 61 *6….drizzlesla
00000040: 70 12 02 74 31 12 06 08 : 05 12 02 69 64 12 0B 08 p..t1……id…
00000050: 04 12 07 69 6E 74 63 6F : 6C 31 12 0C 08 01 12 08 …intcol1……
00000060: 63 68 61 72 63 6F 6C 31 : 32 24 08 01 10 01 1A 1E charcol12$……
00000070: 0A 02 37 31 0A 06 31 32 : 33 34 35 36 0A 0A 54 65 ..71..123456..Te
00000080: 73 74 20 45 6E 74 72 79 : 10 00 10 00 10 00 20 01 st Entry…… .
00000090: 28 01 : (.
00000092:
So, this actually works. π
Similar is the case with other variables. I could have demonstrated them as well, but it required creating new settings in rabbitMQ server (like creating new user, vhosts and setting the permissions etc), which may be a bit confusing. You can get back to me if in case you need that to be demonstrated as well.
Nice work, Anshu. I had never read much about RabbitMQ. This post makes it clearer to me. Also, now that I see the need to disable the plugin before changing its host, I like it even more. In an ideal world, everything would be “hot/immediate change-over”, but that’s difficult to code. I think it’s very reasonable to require the user to stop, reconfigure, and restart; and I think this solution will work for the majority of cases.
True that. Every user want the changes to appear instantly in the system and never bothers about the programming effort. Sometimes its valid to make user do some more work to reduce the effort required and cluttering in the code. π