Lädt...


🔧 Day 16 of 100 Days of Cloud: Deep Dive into RabbitMQ


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Welcome to day 16 of our 100 Days of Cloud journey! Today, we're going to explore RabbitMQ, a powerful open-source message broker that implements the Advanced Message Queuing Protocol (AMQP). We'll cover installation, basic concepts, and go through a hands-on tutorial to get you started with RabbitMQ.

What is RabbitMQ?

RabbitMQ is a message broker that acts as an intermediary for messaging. It provides a common platform for sending and receiving messages, allowing different parts of a distributed system to communicate asynchronously. RabbitMQ supports multiple messaging protocols, but primarily uses AMQP 0-9-1.

Key Concepts

Before we dive into the tutorial, let's familiarize ourselves with some key concepts:

  1. Producer: Application that sends messages
  2. Consumer: Application that receives messages
  3. Queue: Buffer that stores messages
  4. Exchange: Receives messages from producers and pushes them to queues
  5. Binding: Link between an exchange and a queue

Installation

Let's start by installing RabbitMQ on an Ubuntu 20.04 server. We'll use the apt package manager for this.

  1. Update the package index:
   sudo apt-get update
  1. Install dependencies:
   sudo apt-get install curl gnupg apt-transport-https -y
  1. Add the RabbitMQ repository:
   curl -fsSL https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add -
  1. Add the RabbitMQ repository to the apt sources:
   echo "deb https://dl.bintray.com/rabbitmq-erlang/debian focal erlang" | sudo tee /etc/apt/sources.list.d/bintray.erlang.list
   echo "deb https://dl.bintray.com/rabbitmq/debian focal main" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list
  1. Update the package index again:
   sudo apt-get update
  1. Install RabbitMQ server:
   sudo apt-get install rabbitmq-server -y
  1. Start the RabbitMQ service:
   sudo systemctl start rabbitmq-server
  1. Enable RabbitMQ to start on boot:
   sudo systemctl enable rabbitmq-server
  1. Check the status of RabbitMQ:
   sudo systemctl status rabbitmq-server

Setting Up RabbitMQ

Now that we have RabbitMQ installed, let's set it up for use.

  1. Enable the RabbitMQ management plugin:
   sudo rabbitmq-plugins enable rabbitmq_management
  1. Create a new user (replace 'myuser' and 'mypassword' with your desired credentials):
   sudo rabbitmqctl add_user myuser mypassword
  1. Set the user as an administrator:
   sudo rabbitmqctl set_user_tags myuser administrator
  1. Set permissions for the user:
   sudo rabbitmqctl set_permissions -p / myuser ".*" ".*" ".*"

You can now access the RabbitMQ management interface by navigating to http://your_server_ip:15672 in your web browser. Log in with the credentials you just created.

Hands-on Tutorial: Publishing and Consuming Messages

Let's create a simple Python script to publish messages to RabbitMQ and another to consume those messages.

First, install the Python client for RabbitMQ:

pip install pika

Publisher Script

Create a file named publisher.py with the following content:

import pika
import sys

# Establish a connection with RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Create a queue named 'hello'
channel.queue_declare(queue='hello')

# Get the message from command line argument
message = ' '.join(sys.argv[1:]) or "Hello World!"

# Publish the message
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body=message)

print(f" [x] Sent '{message}'")

# Close the connection
connection.close()

Consumer Script

Create a file named consumer.py with the following content:

import pika

# Establish a connection with RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Ensure the queue exists
channel.queue_declare(queue='hello')

# Callback function to process received messages
def callback(ch, method, properties, body):
    print(f" [x] Received {body.decode()}")

# Set up the consumer
channel.basic_consume(queue='hello',
                      auto_ack=True,
                      on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')

# Start consuming messages
channel.start_consuming()

Running the Scripts

  1. In one terminal, start the consumer:
   python consumer.py
  1. In another terminal, run the publisher:
   python publisher.py "Hello from RabbitMQ!"

You should see the message being sent in the publisher terminal and received in the consumer terminal.

Advanced Concepts

Now that we've covered the basics, let's briefly touch on some advanced concepts:

  1. Exchanges: RabbitMQ supports different types of exchanges (direct, topic, headers, and fanout) for more complex routing scenarios.

  2. Durability: You can make queues and messages durable to survive broker restarts.

  3. Acknowledgments: Consumers can send acknowledgments to ensure messages are processed successfully.

  4. Prefetch: You can control how many messages a consumer gets at once with the prefetch count.

  5. Dead Letter Exchanges: Messages that can't be delivered can be sent to a special exchange.

  6. Clustering: RabbitMQ can be clustered for high availability and scalability.

Conclusion

We've covered the basics of RabbitMQ, from installation to creating a simple publisher-consumer setup. RabbitMQ is a powerful tool for building distributed systems, microservices architectures, and handling asynchronous communication between different parts of your application.

In future posts, we'll dive deeper into advanced RabbitMQ concepts and explore how it integrates with cloud services. Stay tuned for more cloud adventures in our 100 Days of Cloud journey!

...

🔧 Day 16 of 100 Days of Cloud: Deep Dive into RabbitMQ


📈 65.89 Punkte
🔧 Programmierung

🔧 RabbitMQ Monitoring | Key Metrics & In-Built RabbitMQ Monitoring Tools


📈 36.05 Punkte
🔧 Programmierung

🔧 A Deep Dive Into Recommendation Algorithms With Netflix Case Study and NVIDIA Deep Learning Technology


📈 33.59 Punkte
🔧 Programmierung

🔧 Deep Dive into apple-app-site-association file: Enhancing Deep Linking on iOS


📈 33.59 Punkte
🔧 Programmierung

🔧 Deep Dive into apple-app-site-association file: Enhancing Deep Linking on iOS


📈 33.59 Punkte
🔧 Programmierung

🎥 Deep dive into Flutter deep linking


📈 33.59 Punkte
🎥 Video | Youtube

🔧 Diversifying in the Cloud Age: A Deep Dive Into Multi-Cloud Architecture


📈 32.94 Punkte
🔧 Programmierung

🔧 Deep Dive Into AI’s Inheritance Into Software Development


📈 31.27 Punkte
🔧 Programmierung

🔧 Day 2: Deep Dive into JavaScript Data Types, Strict Mode, and Basic Operations


📈 29.81 Punkte
🔧 Programmierung

🔧 Day 13: Deep Dive into Object Properties, Getters & Setters, and Lexical Scope in JavaScript 🎉


📈 29.81 Punkte
🔧 Programmierung

🔧 Day 7: Deep Dive into Docker and Container Orchestration


📈 29.81 Punkte
🔧 Programmierung

🔧 Docker DevTools Day 2.0: Dive Deep into the Docker Developer Ecosystem


📈 29.81 Punkte
🔧 Programmierung

🎥 Tales from a cloud CSIRT – let's deep dive into a Kubernetes k8s infection - Santiago Abastante


📈 29.09 Punkte
🎥 IT Security Video

🔧 Deep Dive into Elastic Cloud Enterprise (ECE)


📈 29.09 Punkte
🔧 Programmierung

🔧 Effortlessly Orchestrating Workflows in the Cloud: A Deep Dive into AWS Managed Apache Airflow


📈 29.09 Punkte
🔧 Programmierung

🔧 Unpacking Cloud Infrastructure and Virtualization: A Deep Dive into Their Differences


📈 29.09 Punkte
🔧 Programmierung

📰 Und Microsoft so: Cloud, Cloud, Cloud, Cloud, Cloud, Cloud, Cloud


📈 26.99 Punkte
📰 IT Security Nachrichten

🔧 Day 11 of 100 Days of Cloud: Exploring UmbrelOS - A Revolutionary Cloud Concept


📈 26.49 Punkte
🔧 Programmierung

🔧 Day 6 of 100 days of Cloud:GitHub Actions, Docker, and Cloud Sharing


📈 26.49 Punkte
🔧 Programmierung

🔧 Deep-dive into Vegeta - HTTP load testing tool and library


📈 25.23 Punkte
🔧 Programmierung

📰 Deep Dive into LlaMA 3 by Hand ✍️


📈 25.23 Punkte
🔧 AI Nachrichten

🔧 Deep Dive into Multistage Dockerfile with a Golang App ⚙️🚢


📈 25.23 Punkte
🔧 Programmierung

📰 BtcTurk Hack: A Deep Dive into the Incident, Lessons Learned, and Technical Recommendations


📈 25.23 Punkte
📰 IT Security Nachrichten

🔧 Mastering React Hooks: A Deep Dive into useState and useEffect (Part 1 of 3)


📈 25.23 Punkte
🔧 Programmierung

🔧 Deep Dive Into The Consensus Mechanism of Kaia Chain


📈 25.23 Punkte
🔧 Programmierung

🔧 Dive into the World of TensorFlow: Comprehensive Deep Learning Courses


📈 25.23 Punkte
🔧 Programmierung

🪟 A deep-dive into WinUI 3 in desktop apps


📈 25.23 Punkte
🪟 Windows Tipps

🔧 A Deep Dive into NLP with PyTorch


📈 25.23 Punkte
🔧 Programmierung

📰 Pictory Review (2024): A Deep Dive Into Its Capabilities


📈 25.23 Punkte
📰 IT Nachrichten

📰 "Dive Into Deep Learning": Ein interaktives Buch für DL


📈 25.23 Punkte
📰 IT Nachrichten

📰 17K DFIR Summit Registrations and Counting! Deep-Dive into this content before you join us next week!


📈 25.23 Punkte
📰 IT Security

🔧 A Deep Dive Into Different Types of Caching in Presto


📈 25.23 Punkte
🔧 Programmierung

📰 A Deep Dive into Stream-Jacking Attacks on YouTube and Why They're So Popular


📈 25.23 Punkte
📰 IT Security Nachrichten

matomo