concept rabbitmq server in category docker

appears as: RabbitMQ server, The RabbitMQ server, RabbitMQ server, RabbitMQ servers
Bootstrapping Microservices with Docker, Kubernetes and Terraform MEAP V08

This is an excerpt from Manning's book Bootstrapping Microservices with Docker, Kubernetes and Terraform MEAP V08.

Figure 5.10. Using RabbitMQ to indirectly send messages to other microservices through message queues

5.8.3   Creating a RabbitMQ server

Let’s add a RabbitMQ server to our application! Believe it or not, RabbitMQ is programmed in the Erlang language and there might have been a day when it was difficult to set up, but not anymore! These days it’s a no brainer thanks to the skills we have already learned with Docker and Docker Compose.

Listing 5.8 is an extract from the example-3 Docker Compose file that shows adding a RabbitMQ server to our application. This is another example of instantiating a container from an image on Docker Hub as we did in chapter 4 for our MongoDB database.

Listing 5.8 - Adding a RabbitMQ server to the Docker Compose file (extract from chapter-5/example-3/docker-compose.yaml)
version: '3'
services:
 
  # ... other services defined here ...
 
  rabbit: #A
    image: rabbitmq:3.8.1-management #B
    container_name: rabbit #C
    ports: #D
      - "5672:5672"
      - "15672:15672" #D
    expose: #E
      - "5672"
      - "15672" #E
    restart: always #F
 
  # ... more services defined here ...

Listing 5.9 is an extract from the index.js file for the history microservice and it shows how we make the connection to the RabbitMQ server.

Listing 5.9 - Connecting to the RabbitMQ server (extract from chapter-5/example-3/history/index.js)
// ... other package imports here ...
 
const amqp = require("amqplib"); #A
 
const RABBIT = process.env.RABBIT; #B
 
// ... code omitted here ...
 
function connectRabbit() { #C
 
    return amqp.connect(RABBIT) #D
        .then(messagingConnection => {
            return messagingConnection.createChannel(); #E
        });
}
 
 
// ... code omitted here ...
 
function main() {
    return connectDb() #F
        .then(db => {
            return connectRabbit() #G
                .then(messageChannel => {
                    return startHttpServer(db, messageChannel); #H
                });
        });
}
 
main()
    .then(() => console.log("Microservice online."))
    .catch(err => {
        console.error("Microservice failed to start.");
        console.error(err && err.stack || err);
    });   

One of the most important parts of listing 5.9 above and listing 5.10 below is how the RABBIT environment variable configures the connection to the RabbitMQ server. Listing 5.10 is an extract from the example-3 Docker Compose file and it sets the RABBIT environment variable to include the user name (guest), the password (also guest), the hostname for the server (rabbit) and the port number (5672) for the connection.

Listing 5.10 - New configuration for the history microservice (extract from chapter-5/example-3/docker-compose.yaml)
version: '3'
services:
 
  # ... other services defined here ...
 
  history:
    image: history
    build:
      context: ./history
      dockerfile: Dockerfile-dev
    container_name: history
    volumes:
      - /tmp/history/npm-cache:/root/.npm:z
      - ./history/src:/usr/src/app/src:z
    ports:
     - "4002:80"
    environment:
      - PORT=80
      - RABBIT=amqp://guest:guest@rabbit:5672 #A
      - DBHOST=mongodb://db:27017
      - DBNAME=history
      - NODE_ENV=development
    depends_on:
      - db
      - rabbit #B
    restart: "no"

There’s yet another piece to this puzzle that may not have occurred to you until you try and start this version of our application. The RabbitMQ server is fairly heavy-weight and it takes time to start and be ready to accept connections. Our tiny microservices on the other hand are light-weight and ready in just moments.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest