Hello everyone! I'm back after busy work and want to introduce an event-driven app. I've explored and learned microservices and want to start from event-driven architecture. If you want to know more about Event-Driven Architecture, please visit this page.
Preparation
I'm using Node.js for the sample app so you will need some tools like Node.js and Yarn. I'll give you another sample with different programming languages such as C# and Go in the next post. You may check this Github Repository to look around.
We will provision the RabbitMQ manually through the AWS Console.
Search Amazon MQ in the search box and click the Amazon MQ.
Click Get started button.
Select RabbitMQ and click Next.
We will use Single-instance broker just for testing, you may use Cluster deployment for production use. And, click Next.
Give the broker name and select mq.t3.micro instance.
Setup the username and password for the RabbitMQ host.
Open the Additional setting. Make sure you select the Public access and Use the default VPC and subnet(s). Please avoid Public access for production use. You may need to use Private access and setup the VPC yourself for production use.
Finally, click Next. Review your configuration, and click Create broker.
Time to Code!
Init your project. Use yarn init. Input the details of your project.
Install some dependencies.
yarn add amqplib - for send/consume message from/to RabbitMQ.
yarn add uuid - to generate message id and correlation id.
Sender Code
constamqp=require('amqplib');const{v4:uuidv4}=require('uuid');// setup queue nameconstqueueName='test-queue';/**
* Send message
*/asyncfunctionsend(){// connect to RabbitMQconstconnection=awaitamqp.connect(process.env.RABBITMQ_HOST||'amqp://localhost');// create a channelconstchannel=awaitconnection.createChannel();// create/update a queue to make sure the queue is existawaitchannel.assertQueue(queueName,{durable:true,});// generate correlation id, basically correlation id used to know if the message is still related with another messageconstcorrelationId=uuidv4();// send 10 messages and generate message id for each messagesfor (leti=1;i<=10;i++){constbuff=Buffer.from(JSON.stringify({test:`Hello World ${i}!!`}),'utf-8');constresult=channel.sendToQueue(queueName,buff,{persistent:true,messageId:uuidv4(),correlationId:correlationId,});console.log(result);}// close the channelawaitchannel.close();// close the connectionawaitconnection.close();}send();
Consumer Code
constamqp=require('amqplib');// setup queue nameconstqueueName='test-queue';/**
* consume the message
*/asyncfunctionconsume(){// setup connection to RabbitMQconstconnection=awaitamqp.connect(process.env.RABBITMQ_HOST||'amqp://localhost');// setup channelconstchannel=awaitconnection.createChannel();// make sure the queue createdawaitchannel.assertQueue(queueName,{durable:true,});console.log(" [*] Waiting for messages in %s. To exit press CTRL+C",queueName);// setup consumechannel.consume(queueName,function (message){// just print the message in the consoleconsole.log("[%s] Received with id (%s) message: %s",message.properties.correlationId,message.properties.messageId,message.content.toString());// ack manuallychannel.ack(message);},{// we use ack manuallynoAck:false,});}consume();
Test Your Code
Setup your environment variable, you may use the example scripts to setup the environment variable. As example: