๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BE/๐Ÿƒ Spring

[MQTT] Spring Boot ์—์„œ MQTT(mosquitto) ์‚ฌ์šฉํ•˜๊ธฐ

by ํ‹ด๋”” 2024. 11. 13.
728x90
๋ฐ˜์‘ํ˜•

ํ”„๋ก ํŠธ์—์„œ ํŠน์ • api๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ  mqtt๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์œˆ๋„์šฐ PC๋‚˜ ํ‚ค์˜ค์Šคํฌ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ–ˆ๋‹ค. ์ž‘์€ ์–‘์˜ ๋ฐ์ดํ„ฐ ์˜€๊ณ , ์ด์ „์— ๋น„์Šทํ•œ ํ”„๋กœ์ ํŠธ์—์„œ mosquitto๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ์žˆ์–ด MQTT๋กœ ์ด๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ Spring Boot ์›น ์„œ๋ฒ„๋Š” publisher๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ณ , ์œˆ๋„์šฐ PC๋Š” subscriber, mosquitto๋Š” broker ์—ญํ• ์„ ํ•œ๋‹ค

 

MQTT ํŠน์ง•

  • ๊ฒฝ๋Ÿ‰ ํ”„๋กœํ† ์ฝœ 
    • ์ตœ์†Œํ™” ๋œ ํ—ค๋”
    • 2๋ฐ”์ดํŠธ ์ •๋„์˜ ์ ์€ ์šฉ๋Ÿ‰ ๋ฉ”์„ธ์ง€ -> ์ œํ•œ๋œ ๋Œ€์—ญํญ ํ™˜๊ฒฝ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 
  • Publish/Subscribe ๋ชจ๋ธ
    • Publisher (๋ฐœํ–‰ ์ฃผ์ฒด) <-> Broker (์ค‘๊ฐ„์„œ๋ฒ„) <-> Subscriber (๊ตฌ๋… ์ฃผ์ฒด)
    • Publisher๋Š” ํŠน์ • topic์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœํ–‰ํ•จ
    • Subscriber๋Š” ์›ํ•˜๋Š” ์ฃผ์ œ์— ๋Œ€ํ•ด ๊ตฌ๋…์„ ์š”์ฒญ
    • Broker๋Š” ํ•ด๋‹น topic์˜ ๋ฉ”์„ธ์ง€๊ฐ€ ๋ฐœํ–‰๋˜๋ฉด ๊ตฌ๋…์ž์—๊ฒŒ ์ „๋‹ฌ
    • Publisher์™€ Subscriber๋Š” ์„œ๋กœ ๋ชฐ๋ผ๋„ ๋œ๋‹ค

 

QoS (Quality of Service) Level

mqtt ํ”„๋กœํ† ์ฝœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค์–‘ํ•œ ๋„คํŠธ์›Œํฌ ๋ฐ ํ†ต์‹  ์˜์—ญ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด๋กœ์„œ ์ž์„ธํ•œ ์„ค๋ช…๊ณผ ์‚ฌ์ง„์€ ์•„๋ž˜ ์ปค๋ฎค๋‹ˆํ‹ฐ ์‚ฌ์ดํŠธ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

What is MQTT Quality of Service (QoS) 0,1, & 2? – MQTT Essentials: Part 6

Learn the basics of MQTT Quality of Service (QoS) levels 0,1, & 2, and what each level represents.

www.hivemq.com

 

QoS 0

  • ๋ฉ”์„ธ์ง€๋Š” ์ตœ๋Œ€ ํ•œ๋ฒˆ ์ „์†กํ•˜๋ฉฐ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ์ด ๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋Š” ํ™•์ธํ•˜์ง€ ์•Š์Œ
  • ๊ฐ€์žฅ ๋‚ฎ์€ ์ˆ˜์ค€์˜ ๋ณด์žฅ ์„œ๋น„์Šค
  • ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค
  • find and forget์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ํ›„์† ์กฐ์น˜๋‚˜ ํ™•์ธ ์—†์ด ๋ฉ”์„ธ์ง€๊ฐ€ ์ „์†ก๋˜๋Š” ๊ธฐ๋ณธ TCP ํ”„๋กœํ† ์ฝœ๊ณผ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ

Qos 1

  • ์ค‘๊ฐ„ ์ˆ˜์ค€์˜ ์„œ๋น„์Šค
  • ์ ์–ด๋„ ํ•œ๋ฒˆ ์ด์ƒ ํด๋ผ์ด์–ธํŠธ ์ธก์œผ๋กœ ์ „๋‹ฌ๋˜๋„๋ก ๋ณด์žฅ
  • ์ „๋‹ฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด์„œ ๋ฉ”์„ธ์ง€๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ „๋‹ฌ๋˜์ง€ ๋ชปํ•˜๋ฉด ๋ฉ”์„ธ์ง€๋ฅผ ๋‹ค์‹œ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์‹œ๋„ํ•œ๋‹ค
  • ๋ฉ”์„ธ์ง€๋Š” ์ค‘๋ณต๋˜์–ด ๋ณด๋‚ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•œ ๋ฒˆ๋งŒ ๋„์ฐฉํ•œ๋‹ค๋Š” ๋ณด์žฅ์€ ์—†๋‹ค
  • ๊ณผ์ •
    • QoS 1๋กœ ๋ฉ”์„ธ์ง€๊ฐ€ published
    • Publisher๋Š” ์ˆ˜์‹ ์ž๋กœ ๋ถ€ํ„ฐ PUBACK ํŒจํ‚ท์„ ์ˆ˜์‹ ํ•  ๋•Œ ๊นŒ์ง€ ๋ฉ”์‹œ์ง€์˜ ์‚ฌ๋ณธ์„ ๋ณด๊ด€
    • Publisher๊ฐ€ ๋งŒ์•ฝ ์ ์ ˆํ•œ ์‹œ๊ฐ„ ๋‚ด์— PUBACK ํŒจํ‚ท์„ ์ˆ˜์‹ ํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋ฉ”์„ธ์ง€๋ฅผ ์žฌ์ „์†กํ•œ๋‹ค
    • ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก๋ฐ›์€ ์ˆ˜์‹ ์ž์ธ MQTT ๋ธŒ๋กœ์ปค๋Š” ๋ชจ๋“  ๊ตฌ๋… ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐฐํฌ
    • ๋ธŒ๋กœ์ปค๋Š” ๋ฉ”์„ธ์ง€ ์ˆ˜์‹  ํ™•์ธ์„ ์œ„ํ•ด Publisher์—๊ฒŒ PUBACK ํŒจํ‚ท์œผ๋กœ ์‘๋‹ต

QoS 2

  • ๊ฐ€์žฅ ๋†’์€ ์ˆ˜์ค€์˜ ๋ณด์žฅ ์„œ๋น„์Šค
  • ๊ฐ ๋ฉ”์„ธ์ง€๊ฐ€ ์˜๋„ํ•œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ํ•œ ๋ฒˆ๋งŒ ์ •ํ™•ํžˆ ์ˆ˜์‹ ๋˜๋„๋ก ํ•จ
  • ๋ฐœ์‹ ์ž์™€ ์ˆ˜์‹ ์ž(๋ธŒ๋กœ์ปค)๊ฐ„์— 4๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋œ ํ•ธ๋“œ์„ธ์ดํฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ •ํ™•ํžˆ ๋ฉ”์„ธ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ • ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋‹ค๋ฅธ ๋ ˆ๋ฒจ์— ๋น„ํ•ด ๋Š๋ฆฌ๋‹ค
  • ๊ณผ์ •
    • broker๊ฐ€ publisher๋กœ ๋ถ€ํ„ฐ PUBLISH ํŒจํ‚ท์„ ๋ฐ›๋Š”๋‹ค
    • broker๋Š” ๊ฒŒ์‹œ ๋ฉ”์„ธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  publisher์—๊ฒŒ ๊ฒŒ์‹œ ํŒจํ‚ท์„ ์Šน์ธํ•˜๋Š” PUBREC ํŒจํ‚ท์œผ๋กœ ์‘๋‹ตํ•œ๋‹ค.
    • publisher๊ฐ€ broker๋กœ ๋ถ€ํ„ฐ PUBREC ํŒจํ‚ท์„ ๋ฐ›์ง€ ๋ชปํ•˜๋ฉด ์ˆ˜์‹ ์ž๊ฐ€ ํ™•์ธ์„ ๋ฐ›์„ ๋•Œ๊นŒ์ง€ DUP ํ”Œ๋ž˜๊ทธ๊ฐ€ ํฌํ•จ๋œ PUBLISH ํŒจํ‚ท์„ ์žฌ์ „์†กํ•œ๋‹ค
    • publisher๋Š” broker๋กœ ๋ถ€ํ„ฐ PUBREC ํŒจํ‚ท์„ ๋ฐ›์œผ๋ฉด publisher๋Š” ์ดˆ๊ธฐ PUBLISHํŒจํ‚ท์„ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ญ์ œํ•œ๋‹ค
    • publisher๋Š” brocker๋กœ ๋ถ€ํ„ฐ PUBRECํŒจํ‚ท์„ ๋ฐ›์•„ ์ €์žฅํ•˜๊ณ  PUBREL ํŒจํ‚ท์œผ๋กœ ์‘๋‹ตํ•œ๋‹ค
    • broker๋Š” PUBREL์„ ๋ฐ›์œผ๋ฉด ์ €์žฅ๋œ ๋ชจ๋“  ์ƒํƒœ๋ฅผ ์‚ญ์ œํ•˜๊ณ  PUBCOMP ํŒจํ‚ท ์‘๋‹ต์„ ๋ณด๋‚ธ๋‹ค

 

ํ† ํ”ฝ์ด๋ž€

  • MQTT ๋ฉ”์„ธ์ง€๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ๋กœ
  • home/factory1/temperature ์™€ ๊ฐ™์€ ํ˜•์‹์˜ ๊ณ„์ธต ๊ตฌ์กฐ์ด๋‹ค
  • ๋ฉ”์„ธ์ง€๋ฅผ ์–ด๋–ค ์šฉ๋„๋กœ ์‚ฌ์šฉํ• ์ง€์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜ํ•ด์ฃผ๋Š” ์—ญํ• 
  • ํด๋ผ์ด์–ธํŠธ๋Š” ํŠน์ • ํ† ํ”ฝ์„ ๊ตฌ๋…ํ•˜๊ณ  ํ•ด๋‹น ํ† ํ”ฝ์—์„œ ๋ฐœํ–‰๋˜๋Š” ๋ฉ”์„ธ์ง€๋งŒ ์ˆ˜์‹ ํ•œ๋‹ค

=> ๋ฉ”์‹œ์ง€๋ฅผ ํŠน์ • ์ฃผ์ œ๋‚˜ ๋ฒ”์ฃผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ์ฃผ์†Œ ์—ญํ• 

 

ํŽ˜์ด๋กœ๋“œ

  • ๋ฉ”์‹œ์ง€์˜ ์‹ค์ œ ๋‚ด์šฉ
  • ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„
  • home/factory1/temperature ํ† ํ”ฝ์— {"temperature": 30.1} ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ํŽ˜์ด๋กœ๋“œ์— ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ
  • ํŠน์ • ํ† ํ”ฝ์—์„œ ๋ฐœํ–‰๋œ ๋ฉ”์‹œ์ง€๊ฐ€ ์–ด๋–ค ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒฐ์ •

=> MQTT ๋ฉ”์‹œ์ง€๊ฐ€ ์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•จ

 

ํด๋ผ์ด์–ธํŠธ ID

  • ๊ณ ์œ  ID๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค
  • ์ค‘๋ณต๋œ ํด๋ผ์ด์–ธํŠธ๋Š” id ์—ฐ๊ฒฐ ์ถฉ๋Œ์„ ์ผ์œผ์ผœ ๋‚˜์ค‘์— ์ ‘์†ํ•˜๋Š” client๋Š” disconnect ๋  ์ˆ˜ ์žˆ๋‹ค

 

์ฃผ์š” MQTT

  • Mosquitto
  • EMQX
  • RabbitMQ

๋“ฑ๋“ฑ

 

Spring Boot ๊ตฌํ˜„

์˜์กด์„ฑ ์ถ”๊ฐ€

dependencies {
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
}

 

MQTT Configuration ์„ค์ •

@Configuration
public class MqttConfig {
    @Value("${mqtt.server}")
    private String BROKER_URL;

    @Value("${mqtt.client-id}")
    private String CLIENT_ID;

    @Value("${mqtt.user}")
    private String userName;

    @Value("${mqtt.password}")
    private String password;

    @Bean
    public MqttClient mqttClient() throws MqttException {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setKeepAliveInterval(30);
        options.setConnectionTimeout(10);
        options.setUserName(userName);
        options.setPassword(password.toCharArray());

        MqttClient client = new MqttClient(BROKER_URL, CLIENT_ID, new MemoryPersistence());
        client.connect(options);
        client.setCallback(new MqttCallback());

        return client;
    }
}

 

  • ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” client๋ž€ MQTT server์— ์ ‘์†ํ•œ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์˜๋ฏธ (spring boot ์›น ์„œ๋ฒ„)
  • username๊ณผ password๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ MqttConnectOptions์— ์ถ”๊ฐ€ํ•ด ์ค˜์•ผ ํ•œ๋‹ค.

 

MqttCallback

@Configuration
public class MqttConfig {
    @Value("${mqtt.server}")
    private String BROKER_URL;

    @Value("${mqtt.client-id}")
    private String CLIENT_ID;

    @Value("${mqtt.user}")
    private String userName;

    @Value("${mqtt.password}")
    private String password;

    @Bean
    public MqttClient mqttClient() throws MqttException {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setKeepAliveInterval(30);
        options.setConnectionTimeout(10);
        options.setUserName(userName);
        options.setPassword(password.toCharArray());

        MqttClient client = new MqttClient(BROKER_URL, CLIENT_ID, new MemoryPersistence());
        client.connect(options);
        client.setCallback(new MqttCallback());

        return client;
    }
}

 

MqttPublisherService

@Slf4j
@Service
@RequiredArgsConstructor
public class MqttPublisherService {
    private final MqttClient mqttClient;

    @Value("${mqtt.qos}")
    private int qos;

    public void publish(String topic, String payload) throws MqttException {
        MqttMessage message = new MqttMessage(payload.getBytes());
        message.setQos(qos);
        mqttClient.publish(topic, message);
        log.info("MQTT message published to topic {} :{}", topic, payload);
    }
}

 

  • ํ˜„์žฌ client์ธ spring boot ์›น ์„œ๋ฒ„๋Š” publisher๋กœ์„œ ๋ฉ”์„ธ์ง€๋ฅผ brocker server์— publishํ•œ๋‹ค
mqttPublisherService.publish(MQTT_TOPIC, ttMsg.toString());
  • ์‹ค์ œ ์‚ฌ์šฉ์‹œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค

 

๊ตฌ๋™ ํ™•์ธ ํ•˜๊ธฐ & MQTT Explorer

https://mqtt-explorer.com/

 

MQTT Explorer

An all-round MQTT client that provides a structured topic overview

mqtt-explorer.com

  • ์œ„ ์‚ฌ์ดํŠธ์—์„œ MQTT Explorer๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ mqtt ์„œ๋ฒ„์— publish ๋˜๋Š” ํŽ˜์ด๋กœ๋“œ์™€ ํ† ํ”ฝ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค
sudo apt update
sudo apt install mosquitto-clients
mosquitto_sub -h {mqtt ip} -p {port} -t {topic}

 

  • MQTT Explorer๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋กœ์ปฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•
  • broker ์„œ๋ฒ„์— ํ•ด๋‹น topic์„ subscribeํ•œ๋‹ค

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€