본문 바로가기
AI

RabbitMQ는 MQTT 브로커 역할과 AMQP 역할을 동시에 할 수 있는가

by david100gom 2025. 2. 22.

RabbitMQ는 AMQP(Message Queue)와 MQTT(Message Broker) 두 가지 역할을 동시에 수행할 수 있습니다. RabbitMQ는 기본적으로 AMQP(Advanced Message Queuing Protocol) 기반의 메시지 브로커이지만, 플러그인을 활성화하면 MQTT 브로커 기능도 사용할 수 있습니다.


🚀 RabbitMQ의 두 가지 역할

역할 설명 RabbitMQ에서 어떻게 지원하는가?

1. AMQP 브로커 메시지 큐를 기반으로 Publisher/Consumer 간 비동기 메시징을 제공 기본적으로 지원 (amqp:// 프로토콜 사용)
2. MQTT 브로커 IoT 기기와 같은 경량 메시징 시스템에서 사용되는 MQTT 프로토콜 지원 rabbitmq_mqtt 플러그인을 활성화하면 MQTT 브로커로 사용 가능 (mqtt:// 프로토콜 사용)

즉, RabbitMQ는 두 가지 역할을 모두 수행할 수 있으며, AMQP와 MQTT를 동시에 사용할 수도 있습니다! 🚀


🔹 1. RabbitMQ의 AMQP 역할 (메시지 큐)

RabbitMQ는 기본적으로 AMQP 기반 메시지 브로커이며, 다음과 같은 방식으로 동작합니다.

✅ AMQP의 핵심 개념

개념 설명

Exchange 메시지를 라우팅하는 역할 (Direct, Fanout, Topic, Headers)
Queue 메시지를 저장하는 공간 (Consumer가 메시지를 가져감)
Routing Key 메시지를 특정 Queue로 전달하는 기준
Consumer Queue에서 메시지를 가져가 처리하는 역할

📌 AMQP를 사용하는 Node.js 예제

1️⃣ Producer (메시지 전송)

const amqp = require('amqplib');

async function sendMessage() {
    const connection = await amqp.connect('amqp://localhost');
    const channel = await connection.createChannel();
    const queue = 'amqp_queue';

    await channel.assertQueue(queue, { durable: true });

    channel.sendToQueue(queue, Buffer.from("Hello AMQP!"));
    console.log("Sent: Hello AMQP!");

    setTimeout(() => {
        connection.close();
    }, 500);
}

sendMessage();

2️⃣ Consumer (메시지 수신)

const amqp = require('amqplib');

async function startConsumer() {
    const connection = await amqp.connect('amqp://localhost');
    const channel = await connection.createChannel();
    const queue = 'amqp_queue';

    await channel.assertQueue(queue, { durable: true });

    console.log(`Waiting for messages in ${queue}`);

    channel.consume(queue, (msg) => {
        if (msg !== null) {
            console.log("Received:", msg.content.toString());
            channel.ack(msg);
        }
    });
}

startConsumer();

AMQP를 사용하면 메시지를 Queue에 저장하고, 여러 Consumer가 이를 가져와 처리할 수 있음.


🔹 2. RabbitMQ의 MQTT 역할 (IoT 메시징)

RabbitMQ는 rabbitmq_mqtt 플러그인을 활성화하면 MQTT 메시지 브로커로 동작할 수 있습니다.

✅ RabbitMQ에서 MQTT 브로커 활성화

rabbitmq-plugins enable rabbitmq_mqtt
rabbitmqctl set_permissions -p / guest ".*" ".*" ".*"
  • MQTT 클라이언트가 RabbitMQ에 연결할 수 있도록 플러그인을 활성화.

📌 MQTT를 사용하는 Node.js 예제

1️⃣ MQTT Publisher (메시지 전송)

const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://localhost');

client.on('connect', () => {
    console.log('Connected to MQTT Broker');
    client.publish('mqtt_topic/data', 'Hello MQTT!');
    console.log('Message Sent: Hello MQTT!');
});

2️⃣ MQTT Subscriber (메시지 수신)

const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://localhost');

client.on('connect', () => {
    client.subscribe('mqtt_topic/data');
    console.log('Subscribed to MQTT topic');
});

client.on('message', (topic, message) => {
    console.log(`Received from ${topic}: ${message.toString()}`);
});

RabbitMQ를 MQTT 브로커로 사용하면 일반적인 Mosquitto 같은 MQTT 브로커처럼 동작할 수 있음.


🔹 RabbitMQ에서 MQTT와 AMQP를 동시에 사용할 수 있을까?

네, RabbitMQ는 MQTT와 AMQP를 동시에 사용할 수 있으며, 서로 메시지를 주고받을 수도 있습니다!
이를 통해 IoT 기기에서 MQTT로 메시지를 보내고, RabbitMQ의 AMQP Queue로 변환하여 백엔드 시스템에서 처리하는 구조를 만들 수 있습니다.

✅ MQTT 메시지를 AMQP Queue로 변환하는 방법

RabbitMQ에서 MQTT 메시지를 AMQP 메시지로 변환하려면 mqtt_exchange를 사용하면 됩니다.

rabbitmqctl set_parameter mqtt default_exchange amq.topic
  • MQTT 메시지가 RabbitMQ의 Topic Exchange(amq.topic)로 들어가도록 설정.

📌 MQTT → AMQP 변환 예제

1️⃣ MQTT Publisher (IoT 기기에서 메시지 전송)

const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://localhost');

client.on('connect', () => {
    client.publish('sensor/temperature', '25.5°C');
    console.log('MQTT Message Sent: Temperature = 25.5°C');
});

2️⃣ AMQP Consumer (RabbitMQ에서 메시지를 수신)

const amqp = require('amqplib');

async function startConsumer() {
    const connection = await amqp.connect('amqp://localhost');
    const channel = await connection.createChannel();
    const queue = 'sensor_queue';

    await channel.assertQueue(queue, { durable: true });

    await channel.bindQueue(queue, 'amq.topic', 'sensor/temperature');

    console.log(`Waiting for messages in ${queue}`);

    channel.consume(queue, (msg) => {
        if (msg !== null) {
            console.log("Received from MQTT:", msg.content.toString());
            channel.ack(msg);
        }
    });
}

startConsumer();

이제 MQTT 메시지가 AMQP Queue로 변환되어 백엔드 시스템에서 쉽게 처리 가능!


🎯 최종 정리

기능 설명 RabbitMQ에서의 역할

AMQP 메시지 브로커 일반적인 메시지 큐 역할 (Publisher/Consumer) 기본 지원 (amqp://)
MQTT 메시지 브로커 IoT 장치용 메시지 브로커 rabbitmq_mqtt 플러그인 활성화 (mqtt://)
MQTT ↔ AMQP 변환 MQTT 메시지를 AMQP Queue로 변환 amq.topic Exchange 사용

🚀 결론

✅ RabbitMQ는 AMQP와 MQTT를 동시에 사용할 수 있으며, 서로 변환도 가능
IoT 시스템에서는 MQTT → RabbitMQ → AMQP → 백엔드로 연결하는 구조가 효과적
✅ rabbitmq_mqtt 플러그인을 활성화하면 RabbitMQ를 Mosquitto 같은 MQTT 브로커처럼 활용 가능
✅ amq.topic을 사용하면 MQTT 메시지를 AMQP 메시지로 변환 가능

 

댓글