In the previous article, we mentioned the problem of Asynchrony in the short series of scalable architectures. It was recommended to use RABBITMQ to do the task queue implementation. This article takes node. js as an example to actually manipulate how to interact with RABBITMQ.
Introduced
RABBITMQ is a message agent. Its initial thought was particularly simple: to accept and forward messages. You can think of it as a post office: When you put the mail in the mailbox, you can be very sure that the courier will eventually send the message to the recipient. You can liken RABBITMQ to a mailbox, a post office, and a courier. The main difference between RABBITMQ and the post office is that it does not process paper, but instead accepts, stores, and forwards binary data? messages.
In RABBITMQ, there are some basic terms:
- Producer: It is the party that sends the information.
- Task queue: Although the flow of information flows between RABBITMQ and your application, it can be stored in a queue. The queue is not constrained by any restrictions, it can store any number of messages, it is essentially an infinite buffer. Many producers can send messages to this queue, and many consumers can try to accept data from this queue.
- Consumer: Consumers and information recipients have similar meanings, and a consumer is a program that waits to receive information.
Installing RABBITMQ Server
Open the download page for RABBITMQ (https://www.rabbitmq.com/download.html) and download the installation, as an example of Mac OSX platform installation:
RABBITMQ relies on Erlang, and because Mac OSX itself already has Erlang installed, it can be installed directly through the homebrew.
$ brew update$ brew install rabbitmq
After installation, you need to add/usr/local/sbin to $path, add to the./.bash_profile file, and then
$ source ./.bash_profile$ echo $PATH // 检查环境变量是否已经成功加入
After the installation is complete, you can start RABBITMQ server.
At this point, the installation is complete. Errors may be reported when you run the rabbitmq-server command: ERROR:EPMD error for host localhost:timeout (timed out), if this is the case, you can open the/etc/hosts file and add it to the end of the file 127.0.0.1 localhost solves the problem.
Hello World
In this section, I'll use JavaScript to write two small programs. A consumer that sends a single message to the producer and receives the message and prints it out. We will skip some of the details in the Amqp.node API and focus on this very simple thing.
In, p stands for Producer, c stands for consumer, middle red represents task queue-message buffer
First, use NPM to install Amqp.node
$ npm install amqplib
Send Message
Here I call the sender of the message send.js, the message recipient is called Receive.js, the message sender connects to RABBITMQ, sends a message, and finally exits.
First introduce the Amqplib module:
var amqp = require(‘amqplib/callback_api‘);
Connect to RabbitMQ Server
amqp.connect(‘amqp://localhost‘, function(err, conn) {});
Next, create a channel,
amqp.connect(‘amqp://localhost‘, function(err, conn) { conn.createChannel(function(err, ch) {});});
In order to send a message, we need to define a queue where we can send messages to this queue:
amqp.connect(‘amqp://localhost‘, function(err, conn) { conn.createChannel(function(err, ch) { var q = ‘hello‘; ch.assertQueue(q, {durable: false}); // Note: on Node 6 Buffer.from(msg) should be used ch.sendToQueue(q, new Buffer(‘Hello World!‘)); console.log(" [x] Sent ‘Hello World!‘"); });});
Finally, we close the connection and exit:
setTimeout(function() { conn.close(); process.exit(0) }, 500);
Final code reference: Send.js
Accept Message
The way to build a recipient is the same as the sender. Open a connection and channel, and declare a queue that needs to be processed. Note: The queues and senders defined in this queue need to be matched.
amqp.connect(‘amqp://localhost‘, function(err, conn) { conn.createChannel(function(err, ch) { var q = ‘hello‘; ch.assertQueue(q, {durable: false}); });});
This also defines the reason for the queue: the recipient may start executing before the sender. We need to make sure that the queue is there when the recipient handles the queue.
Since the message is sent asynchronously, we need to provide a callback so that when RABBITMQ sends a message to our consumer, the callback executes. This is what channel.consume do.
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);ch.consume(q, function(msg) { console.log(" [x] Received %s", msg.content.toString());}, {noAck: true});
Final code reference: Receive.js
Run code
// 先执行send.js$ ./send.js// 后执行receive.js$ ./receive.js
Final result
Using the RABBITMQ Series in node. js a Hello World