liveBook · Manning

RabbitMQ

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

RabbitMQ is a message broker that routes messages between producers and consumers.

RabbitMQ is message-queuing software for indirect, asynchronous communication between microservices. It lets senders publish messages without knowing which receivers, if any, will handle them, reducing direct coupling. section 5.1 section 5.8

Use RabbitMQ when a microservice should communicate without directly orchestrating another service:

  • Publish events to a queue so another service can process them in its own time. section 5.8
  • Broadcast application-wide events to multiple recipients when several services may react independently. section 5.8.9
  • Prefer direct HTTP messaging when the sender must sequence behavior or immediately know whether handling succeeded. section 5.11

A Docker Compose deployment can run RabbitMQ as a rabbitmq:3.12.4-management container, exposing ports 5672 and 15672. A Node.js service connects through amqplib, using an environment-configured AMQP URI, then creates a messaging channel. section 5.8.3 section 5.8.5

Viewed event

Instead of having the video-streaming microservice call the history microservice directly, publish a “Viewed” message. The history service pulls it from the queue in its own time, while other services can receive the event independently. section 5.8 section 5.8.9

Listing 5.8 Adding a RabbitMQ server to the Docker Compose file
version: '3'
services:
 
  --snip--
 
  rabbit:                                #1
    image: rabbitmq:3.12.4-management    #2
    container_name: rabbit               #3
    ports:                               #4
      - "5672:5672"                      #4
      - "15672:15672"                    #4
    restart: always                      #5
 
  --snip--

Inspect the Docker Compose service definition for the RabbitMQ image, management variant, exposed ports, and restart policy. section 5.8.3

Listing 5.9 Connecting to the RabbitMQ server
--snip--
 
const amqp = require("amqplib");                    #1
 
const RABBIT = process.env.RABBIT;                  #2
 
async function main() {
 
  --snip--
 
  const messagingConnection = 
     await amqp.connect(RABBIT);                  #3
  const messageChannel = 
     await messagingConnection.createChannel();   #4
  
  --snip--
 
  app.listen(PORT);                                 #5
}
#1 Imports the amqplib library. This is the API for talking to the RabbitMQ server.
#2 Gets the URI for connecting to RabbitMQ
#3 Connects to the RabbitMQ server
#4 Creates a RabbitMQ messaging channel
#5 Starts the HTTP server

Inspect the Node.js connection sequence: import amqplib, read the RABBIT URI, connect, and create a channel. section 5.8.5

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Explains queues, deployment, client connection, startup ordering, and messaging choices, but defers robust reconnection to later treatment.
Examples
Uses a Viewed event between video-streaming and history services, Docker Compose deployment, and a Node.js amqplib connection.
What it assumes
Assumes familiarity with microservices, Docker, Docker Compose, Node.js, and HTTP while introducing RabbitMQ and amqplib.
What you can do after
Operate/configure RabbitMQ with Docker Compose and connect Node.js microservices using amqplib and environment settings.
How it connects
Connects message queues with microservices, asynchronous messaging, Docker Compose, Node.js, amqplib, HTTP, and AMQP.

Also explained in

Compare how 5 books explain RabbitMQ →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.