Schema design Nodejs operation Message Queuing RABBITMQ

Source: Internet
Author: User
Tags install brew rabbitmq

I. What is Message Queuing?

A message is the data that is transferred between apps. Messages can be very simple, such as containing only text strings, or they can be more complex and may contain embedded objects.

Message Queuing is a way of communicating between applications, which can be returned immediately after a message is sent, and the message system ensures reliable delivery of the message. The message publisher simply publishes the message into MQ without having to take care of who gets it, and the message user takes the message out of MQ regardless of who posted it. So the publisher and the user do not have to know the other side of the existence.

Two. What are the common message queues?

RabbitMQ, ROCKETMQ, ActiveMQ, Kafka, ZeroMQ, Metamq.

Even now some nosql can do Message Queuing, such as Redis.

Three. What are the usage scenarios for Message Queuing?
    • Asynchronous processing
    • Application decoupling
    • Flow Cutting Peak
Four. Use cases

On the scale of the company will have their own log analysis system, the log system is how to achieve it?

Diagram: When the user accesses the application, we want to record the user's operation record and the system Exception log, the general practice is to save the system generated log to the server disk, the server to open the scheduled task, timed to transfer the log information of the disk into MQ (producer), The messages in MQ are also timed out and co-existing in the appropriate database, such as Elasticsearch or hive.

Five. How do I install RABBITMQ?

The above case describes a usage scenario for MQ, which I use RABBITMQ for example, and what may be used in a real project is Kafka.

    1. First install Brew (Mac for example)

    2. Installing RABBITMQ

      Brew Install RABBITMQ
    3. Run RABBITMQ

      Go to/usr/local/cellar/rabbitmq/3.7.7, execute

      Sbin/rabbitmq-server
    4. Launch Plugin

      Enter to/usr/local/cellar/rabbitmq/3.7.7/sbin

      ./rabbitmq-plugins Enable Rabbitmq_management
    5. Login Management Interface

      Open Browser input: Http://localhost:15672,RabbitMQ default 15672 Port six. Nodejs Operation RABBITMQ

Several corresponding node SDKs can be found online, which is recommended amqplib

1. Producers

/** * Package for RABBITMQ*/Let amqp= Require (' Amqplib '), class RabbitMQ {constructor () { This. hosts = [];  This. index = 0;  This. length = This. Hosts.length;  This. Open = Amqp.connect ( This. hosts[ This. index]); } sendqueuemsg (QueueName, MSG, errcallback) { let self= This; Self.open. Then (function(conn) {returnConn.createchannel (); }). Then (function(channel) {returnChannel.assertqueue (QueueName). Then (function(OK) {returnChannel.sendtoqueue (QueueName,NewBuffer (msg), {persistent:true                    }); }). Then (function(data) {if(data) {Errcallback&& Errcallback ("Success");                        Channel.close (); }                    })                    .Catch(function() {setTimeout ()= {                            if(channel) {channel.close (); }                        }, 500)                    }); })            .Catch(function() {Let num= self.index++; if(Num <= self.length-1) {Self.open=Amqp.connect (Self.hosts[num]); } Else{Self.index= = 0;    }            }); }}

2. Consumers

/** * Package for RABBITMQ*/Let amqp= Require (' Amqplib '), class RabbitMQ {constructor () { This. Open = Amqp.connect ( This. hosts[ This. index]); } receivequeuemsg (QueueName, ReceiveCallback, errcallback) { let self= This; Self.open. Then (function(conn) {returnConn.createchannel (); }). Then (function(channel) {returnChannel.assertqueue (queuename). Then (function(OK) {returnChannel.consume (QueueName,function(msg) {if(Msg!==NULL) {Let data=msg.content.toString ();                                Channel.ack (msg); ReceiveCallback&&receivecallback (data); }                        })                            .finally(function() {setTimeout ()= {                                    if(channel) {channel.close (); }                                }, 500)                            }); })            })            .Catch(function() {Let num= self.index++; if(Num <= self.length-1) {Self.open=Amqp.connect (Self.hosts[num]); } Else{Self.index= 0; Self.open= Amqp.connect (self.hosts[0]);    }            }); }

3. Send a message to MQ through the producer and create a queue

New RabbitMQ (); Mq.sendqueuemsg (' testqueue ', ' My first message ', (error) = {    console.log (Error)}) 

After execution, we opened the management platform and found that RABBBITMQ had received a message:

And RABBBITMQ has added a queue testqueue

4. Get the message for the specified queue

New RabbitMQ (); Mq.receivequeuemsg (' Testqueue ', (msg) = {       console.log (msg)})//  Output result: My first message copy code

When the RABBITMQ management platform opens, the number of messages has changed to 0

In summary: We briefly describe the message queue and RABBITMQ related knowledge, and how we use Nodejs to produce and consume messages, the above is relatively simple, then published more articles on the Message Queuing cluster building and disaster tolerance implementation.

Schema design Nodejs operation Message Queuing RABBITMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.