Write at the beginning
- Recently Devops and Micro Front End have written almost the same, I started reviewing the knowledge of the back end, I wanted to write this article before, and finally fell to the ground
- If you want to join the front-end communication group, you can contact me at the end to join
Official Start
- Mac|Linux recommended for computer environments
- Install redis and start redis
`redis-server`
- After successful startup, the following will occur:
- redis default port 6379
Start Writing Node.js Code
- Download redis this library
yarn add redis --save
- Use Node.js Connect redis
const redis = require('redis'); const client = redis.createClient(6379, '127.0.0.1');
- Since it's a message queue, you need to have a producer, a consumer
❝
The ubiquitous use of message queues here, like redis, is an out-of-process service, which requires a single port to start the service
❞
What is a message queue?
- Message Queuing is a container that stores messages during their transmission.
- Messages are sent to the queue.Message Queuing is a container that stores messages during their transmission.The Message Queue Manager acts as an intermediary when relaying messages from its source to its destination.The primary purpose of a queue is to provide routing and guarantee the delivery of messages; if the recipient is not available when sending a message, the message queue retains the message until it can be successfully delivered.
- That is, producers, consumers, publishing subscription model implementation
Message Queue Usage Scenario
- Business decoupling
- Asynchronous processing improves performance
- Current Limiting and Peak Cutting (Reducing costs, it is not possible to have servers at peak flow)
Start Implementation
- Producer
`const redis = require('redis'); const client = redis.createClient(6379, '127.0.0.1'); client.on('error', function (err) { console.log('err' + err); }); client.on('ready', function () { client.publish('testFirst', 'hi! first!'); client.publish('testSecond', 'hi! second!'); client.publish('message', 'hi! message!'); });`
- Producers publish specific channel s with parameters
- Consumers subscribe to specific channel s, consume, and get data
`const client = require('redis').createClient(6379, '127.0.0.1'); client.on('error', function (err) { console.log('err' + err); }); client.subscribe('testSecond'); client.subscribe('message'); client.on('subscribe', function (channel, count) { console.log('subscribe channel:' + channel + ', count:' + count); }); client.on('message', function (channel, message) { console.log('message channel:' + channel + ', msg:' + message); }); client.on('unsubscribe', function (channel, count) { console.log('unsubscribe channel:' + channel + ', count:' + count); });`
- Result:
- I subscribed to both the testsecoud and message channels and triggered the subscribe event twice, as expected
Simulate scenarios where producers continually provide production
- Add timer
`const redis = require('redis'); const client = redis.createClient(6379, '127.0.0.1'); client.on('error', function (err) { console.log('err' + err); }); client.on('ready', function () { setInterval(() => { client.publish('testSecond', 'hi! second!'); client.publish('message', 'hi! message!'); },1000); });`
- The consumer keeps printing, triggering the message event
❝
In this way, we implemented a simple message queue using redis publishing subscription mode
❞
Achieve flow peak clipping and current limiting
- At present, our production is 1S message, but I want to control 2S consumption once, is that OK?
- We control the frequency of consumption without first changing the frequency of production
`const client = require('redis').createClient(6379, '127.0.0.1'); const ArrayList = []; client.on('error', function (err) { console.log('err' + err); }); client.subscribe('testSecond'); client.subscribe('message'); client.on('subscribe', function (channel, count) { console.log('subscribe channel:' + channel + ', count:' + count); }); client.on('message', function (channel, message) { ArrayList.push({ channel, message }); }); client.on('unsubscribe', function (channel, count) { console.log('channel:' + channel + ', count:' + count); }); setInterval(()=>{ console.log(ArrayList,'ArrayList') },2000)`
- Read queue data every 2S
-
What is the difference between the simulation and the reality?
- The simulation is in a process port and belongs to an in-process cache
- The truth is that you can confirm consumption by replying to ACK, exclusive to a port process, and belong to an out-of-process cache
A simple message queue via redis is done
- Source address:https://github.com/JinJieTan/MQ