12/28/2021

Install Rabbitmq Docker

98

Sudo yum update sudo yum install docker sudo service docker start # Docker docker run -d -hostname rabbitmq -name rabbitmq -p 4369:4369 -p 5671:5671. Deploy RabbitMQ with Docker and Architect. By Dockerizing our services, we've taken a great step toward a more portable application. But deploying these services remotely still requires several steps with manual configuration. While Docker makes individual services more portable, it doesn't quite do the same for the full stack. Sudo yum install docker sudo service docker start. Now the server is ready for the RabbitMQ installation. Rabbit1 is hostname. Erlang cookie is defined to facilitate cluster. Jan 19, 2021 Deploy RabbitMQ with Docker and Architect. By Dockerizing our services, we've taken a great step toward a more portable application. But deploying these services remotely still requires several steps with manual configuration. While Docker makes individual services more portable, it doesn't quite do the same for the full stack. Using the RabbitMQ Docker Image. The RabbitMQ container registry includes a variety of images for different platforms.In this article, we will use the RabbitMQ image with a tag 3-management, which is a Docker image with the RabbitMQ management plugin installed and enabled by default.

RabbitMQ is an open-source message broker. And it is probably one of the most widely used message brokers out there. In my experience, it is one of the most robust message brokers. It is extremely easy to install and use.

RabbitMQ is extremely lightweight and very easy to deploy. It supports multiple protocols. It is highly available and scalable. And finally, it supports multiple operating systems including Windows, Linux, and macOS.

What is a message broker?

You can think of a message broker like a post office. Its main responsibility is to broker messages between publishers and subscribers.

Once a message is received by a message broker from a producer, it routes the message to a subscriber. The message broker pattern is one of the most useful patterns when it comes to decoupling microservices.

The message broker consists of three main components: Producers, Consumers, and Queues.

What is a Producer?

A producer is an application responsible for sending message.

What is a Consumer?

A consumer is an application listening for messages.

Install Rabbitmq Docker
What is Queue?

A queue is a storage where messages are stored by the broker.

Protocols Supported by RabbitMQ

RabbitMQ supports multiple protocols. I am going to focus on protocols supported directly by the RabbitMQ and leave out the Plugins.

AMQP 0-9-1

AMQP 0-9-1 is a binary messaging protocol specification. This is the core protocol specification implemented in RabbitMQ. All other protocol support in RabbitMQ is through PlugIns.

Other Protocols supported are
  • STOMP – A text-based message protocol
  • MQTT – Binary protocol focusing mainly on Publish/Subscribe scenarios.
  • AMQP 1.0
  • HTTP and WebSocket

Installing RabbitMQ

For installing RabbitMQ, I am going to use a docker install. It is extremely easy to install RabbitMQ using docker. And you will find out that it literally takes a couple of minutes to install it.

The docker image for RabbitMQ image is available here: https://hub.docker.com/_/rabbitmq

There are multiple options available with docker for installation. I will use the following command for installation:

docker run -d --hostname my-rabbit --name ecomm-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-managementInstall Rabbitmq Docker

The above command will install RabbitMQ along with the Management web console. And we will map the ports 15672 and 5672 of the docker container to the host machine.

Once the container is installed and running we can go to the url: http://localhost:15672/ and check the management web console.

The default user id and password for management is guest/guest.

Creating an Application for with single Producer/Consumer with a Queue

Firstly, I will create a new ASP.Net Core Web API application. To do that, I will open up Visual Studio 2019. Once Visual Studio opens up, I will select the menu File -> New -> Project. This will open the Create a new Project project popup window.

Secondly, in the Create a new Project popup window, I will select the Console Application (.NET Core) from the project template and click on the Next button.

Thirdly, on the next page, I will provide the name of the application as RabbitMQ.Producerand click on the Create button.

Once the project is created I will create a new class QueueProducer. This class will have a single method to Publish. Inside of the Publish method, I will first declare a queue named “demo-queue”.

For that, I will queue the QueueDeclare method on IModel which is coming in as a parameter. This IModel will be created inside of the Main method, and we will discuss later.

And after that inside of a while loop, I will publish message in a continuous manner.

Next I will update the Main method in the Program.cs file to call this Publish method.

Inside of the Main method, first I will create a new instance of the ConnectionFactory class, passing the URL of the RabbitMQ server installed.

Secondly, I will call CreateConnection on the factory, to create a new connection.

Thirdly, I will call CreateModel on the connection object to create an instance of IModel.

And finally, I will call the Publish method of the QueueProducer class passing the instance of IModel object.

Creating a Consumer

Similar to the publisher project, I will create another .NET Core Console Application for consumer.

Firstly, I will create a new ASP.Net Core Web API application. To do that, I will open up Visual Studio 2019. Once Visual Studio opens up, I will select the menu File -> New -> Project. This will open the Create a new Project project popup window.

Secondly, in the Create a new Project popup window, I will select the Console Application (.NET Core) from the project template and click on the Next button.

Install rabbitmq docker software

Thirdly, on the next page, I will provide the name of the application as RabbitMQ.Consumerand click on the Create button.

Once the project is created, I will create a new class QueueConsumer. Inside of this class, I will have a single method Consume.

Inside of this method, first I will declare the queue “demo-queue”.

Secondly, I will create an instance of the EventingBasicConsumer class and attach a delegate to the Received event handler.

Finally, I will call BasicConsume on the IModel instance to start consuming message from the queue.

After that I will call the Consume method from the Main method of the Program.cs.

Now, if I run both the applications, I will see messages produced by the Producer are consumed by the Consumer.

What is an Exchange

Exchanges are, exactly what the name suggests. They are exchanges for messages. Just like we have stop exchange, where people exchange stocks, a seller sells stock to a buyer. And exchange acts as a router of the stocks.

Install Rabbitmq Docker

Similarly, Exchanges in RabbitMQ routes messages from a producer to a single or multiple consumers.

An Exchange uses header attributes, routing keys, and binding to route messages. (Will discuss in detail later).

In RabbitMQ, in fact, messages are never published to a queue, they always go through an Exchange. Even when we send a message to a queue it uses default exchange. [Exchange: (AMQP default)]

Type of Exchanges

There are four main types of exchanges. Direct, Topic, Header and Fanout.

  • Direct exchange uses a routing key in the header to identify which queue the message should be sent to. The routing key is a header value set by the producer. And consumer uses the routing key to bind to the queue. The exchange does the exact match of routing key values.
  • The topic exchange also uses a routing key, but it does not do an exact match on the routing key, instead, it does a pattern match based on the pattern.
  • Header exchange routes messages based on header values and are very similar to Topic exchange.
  • As the name suggests, Fanout exchange routes messages to all the queues bound to it.

I have walked through all exchange types in my YouTube video here:

The source code is available in my GitHub repo here.

Here at Architect, it's no secret that we love portable microservices. And what better way to makeyour services portable than by decoupling their interactions?

Today we talk about decoupling your services using a classic communication pattern: the messagequeue. In this tutorial, we'll show you how to get our favorite open source messagebroker--RabbitMQ--up and running in just 20 minutes. Then we'll use Architect to deploy the stack toboth your local and remote environments.

If you're following along with this tutorial at home, you'll need a few pre-reqs:

Install Rabbitmq Docker Image

  • Node >= 8.2.0
  • Python >= 3

RabbitMQ Instance with Docker

First, let's pull the RabbitMQ docker image. We'll use the 3-management version, so we get theManagement plugin pre-installed.

Now let's stand it up. We’ll map port 15672 for the management web app and port 5672 for themessage broker.

Assuming that ran successfully, you've got an instance of RabbitMQ running! Bounce over tohttp://localhost:15672 to check out the management web app.

Log in using the default username (guest) and password (guest) and explore the management app alittle bit. Here you can see an overview of your RabbitMQ instance and the message broker’s basiccomponents: Connections, Channels, Exchanges, and Queues.

Send Messages to RabbitMQ from a Producer

RabbitMQ is only interesting if we can send messages, so let's create an example publisher to pushmessages to RabbitMQ. In a new session (keep RabbitMQ running), we'll use the following directorystructure:

Our publisher will be a simple Node.js Express web application. Use the Express app generator tobootstrap a simple Express app. We'll use the amqplib Node library, the recommended AMQP client.

The plan is to add a route that accepts requests to POST /message with a body that looks somethinglike this: {'message': 'my message'}. That route will publish each message it receives to ourRabbitMQ instance.

First, create a new file called message.js next to index.js.

Import amqplib, set the URL to the location of the RabbitMQ instance, and give our queue a name:

Next, initialize the connection:

And it's important that we not forget to add an exit handler to close the channel:

Now for the meat. We'll add a route that receives the message, converts the body.message stringinto a Buffer, and sends it to the queue:

Last, we'll need to register our new route in app.js. We'll put it underneath the existing indexroute, and we'll nest our routes under /message:

We'll also add a simple HTML form to our view to post messages to the /messages endpoint. Replaceindex.js with the following:

That should do it! See the full Express appsource code here

Now we can run the sample producer app:

And we can visit it here: http://localhost:3000.

Try sending a message from the browser. It should send a POST HTTP request to the Express app,which will subsequently stick it on the queue. If you navigate to the RabbitMQ management app, youshould see traffic coming through http://localhost:15672.

Receive Messages from RabbitMQ with a Consumer

Now that we're publishing messages let's see if we can receive them with a consumer application.

We'll use Python for our sample consumer application. This illustrates the flexibility that amessage queue introduces to our stack. While we wouldn't suggest a multi-language stack just for thehell of it, using AMQP for inter-process communication does give us the polyglot option in a pinch!

In a new session (keep RabbitMQ and the producer running):

We'll use pika, a recommended AMQP Python client:

Install rabbitmq docker image

In consumer.py, first import pika:

And just like we did in the producer app, set the AMQP host and the queue name that our app willlisten on:

Next, define a method for handling the messages coming off the queue. For the sake of this tutorial,our app will simply log all received messages to stdout:

Let's create a main() method for the core logic. Here we create the connection and ensure thequeue exists. Last, we pass in the message handler and start consuming.

Finally, call the main() method:

See the full Python consumer appsource code here.

Now run the consumer:

If we navigate back to our producer webapp, we can publish a message. Thebrowser app posts the message to our Node Express server, which publishes the message to RabbitMQ.If you're watching the logs in our Python command line consumer app, you should see the message comeacross. Works like a charm!

Queues are a nifty decoupling mechanism. We've got a Node app and a Python app seamlesslycommunicating! Even better- neither one depends on the uptime of the other. Try killing the consumerapplication and then publish a message from the producer. Now restart the Python consumer. Youshould see the message come through! No traffic was lost while the consumer was down.

Deploy RabbitMQ with Docker

We already have three components in our stack: the web app producer, the message broker, and thecommand line consumer. How are we going to deploy this stack into a remote or productionenvironment?

First, let's make these applications a little bit more portable using docker. The RabbitMQ containeris already Dockerized, so we'll create Dockerfiles for both the Express server and the Pythonconsumer.

First, the producer Dockerfile:

Add the following contents:

Adding a .dockerignore file next to the producer Dockerfile will save us some build time heretoo:

Next, add the consumer Dockerfile:

Add the following:

Let's fire them all up locally. First, in one session, we'll run RabbitMQ:

Next, open up another session and run the producer. We'll map port 3000, so we can access ourExpress app.

Uh oh... that crashed. Looks like we got an error:

This makes sense: localhost no longer resolves to RabbitMQ now that we're running the producerinside a docker container. The RabbitMQ instance is running in a different container! We might makethis work by changing the URL to host.docker.internal, but this is just as brittle: now we can'trun the producer without docker. Further, once we try to deploy this remotely, we'll need to changethis again.

This sounds like a good candidate for an environment variable! So let's factor it out. While we'reat it, let's do the same for the queue name since this is liable to change across environments aswell.

In our Node.js producer message.js, we'll change the url and queue:

Likewise, in our Python consumer.py, we'll do the same (you'll need to import os at the top ofthe file).

Be sure the rabbitmq docker container is still running in another shell. Now, in a new shell, weshould be able to run the producer and pass in the environment variables:

Keep the producer running and open up a new shell. Then run the consumer:

Now, if you navigate to http://localhost:3000, you should be able topublish a message and watch that messages flow!

Deploy RabbitMQ with Docker and Architect

By Dockerizing our services, we've taken a great step toward a more portable application.

But deploying these services remotely still requires several steps with manual configuration. WhileDocker makes individual services more portable,it doesn't quite do the same for the full stack.Docker Compose gets us some of the way there for local development, but an important principle ofportable application development is that we operate our services the same way, regardless of theenvironment! A developer shouldn't have to change the way they deploy services between local andremote environments. Architect solves that.

Using Architect to make a fully deployable stack is easy:

  1. Mark up your services with a YML file
  2. Deploy

If you haven't signed up for Architect yet, do it now! It'sfast, free, and we'll blow you away with our deploy process!

Download the Architect CLI and log in to Architect Cloud:

To get started, we’ve composed an examplearchitect.ymlfor this rabbitmq stack. You can copy it to your directory with:

When you registered with Architect Cloud, you were prompted to create an account. Change the topline of the architect.yml to match your new account name:

Now, deploying the example RabbitMQ stack on your local environment becomes as easy as:

Then navigate to app.localhost to see your running producer ormgmt.localhost to see the RabbitMQ management webapp.

To deploy the same app to a remote staging or production environment, first register it withArchitect Cloud:

Then deploy with a similar command:

Architect Cloud will generate a deployment diff and prompt you to review it. Press Y to continueand deploy. Architect is now deploying your RabbitMQ stack--producer, consumer, and broker--to aremote Kubernetes cluster.

You can watch the deployment unfold here:https://cloud.architect.io/<your-account-name>/environments/example-environment/

While you’re waiting for that deployment to complete, you can also explore deploying this stack toyour infrastructure by registering your AWS account or Kubernetes Cluster with Architect:https://cloud.architect.io/<your-account-name>/platforms/new.

Once the deployment completes, you should be able to see the message consumer and broker runninglive:

https://app.example-environment.<your-account-name>.arc.domains/

Likewise, your RabbitMQ management interface:

https://mgmt.example-environment.<your-account-name>.arc.domains/

_Note: it takes a few minutes for new SSL certificates to propagate, so domains may appear to be insecure initially. Rest assured, they will propagate._

Finally, to clean up this environment and break down your deployed services, use:

Learn More About Architect and Modern Deployment Practices

We hope you enjoyed following along!

Check out our other tutorials and blog series.

If you'd like to play around with Architect some more,check out our Docs.

Install Rabbitmq Docker Free

And don't be afraid to reach out to the team with any questions or comments! You can find us onTwitter @architect_team.

  • Most Viewed News

    • Mac Os Office 2019 Download
    • Senuti Free
    • Hp Printer Scanner Software Mac