๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Server/MQTT

[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

by ๋ฆฌ์š”_ 2025. 1. 2.
๋ฐ˜์‘ํ˜•

[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

๐ŸŒŸ INDEX


[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

์ด ๊ธ€์—์„œ๋Š” Java Spring Boot๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MQTT ๊ธฐ๋ฐ˜์˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. MQTT ๋ธŒ๋กœ์ปค๋ฅผ ํ†ตํ•ด ๋‹จ๋ง๊ธฐ์™€์˜ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹  ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ , ์ˆ˜์ง‘๋œ ์ •๋ณด๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๋Š” ๊ณผ์ •์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 

MqttConfig.java

์ „๋ฐ˜์ ์œผ๋กœ mqtt Broker์—์„œ ๋ฐœํ–‰ ๋ฐ ๊ตฌ๋…๋˜๋Š” message๋ฅผ ์ฝ๊ณ , ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์„ MqttConfig ์—์„œ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

(์ถ”ํ›„์— ๋ฆฌํŽ™ํ† ๋ง์ด ํ•„์š”ํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.)

[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

MqttConfig.java ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , @Log4j2, @Configuration ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

@Log4j2

ํด๋ž˜์Šค์— ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด๋ฉด Log4j 2 ์˜ ๋กœ๊ฑฐ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ ๋‚ด์—์„œ ๋กœ๊ทธ๋ฅผ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋ฉฐ, ๋‹ค์–‘ํ•œ ๋กœ๊ทธ๋ ˆ๋ฒจ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

 

@Configuration

ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ์˜ Spring Bean์„ ์ •์˜ํ•˜๋Š” ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ด ํด๋ž˜์Šค๋Š” Spring ์˜ IoC ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ •์„ Java ์ฝ”๋“œ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ๋ฉฐ, @Bean ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•  ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ ํ†ตํ•ด XML ๊ธฐ๋ฐ˜ ์„ค์ •์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


build.gradle ์— MQTT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธํ•˜๊ธฐ

Spring Integration MQTT๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด mqtt ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

dependencies {
    implementation 'org.springframework.integration:spring-integration-mqtt'
}

 

Maven, Gradle ๋ฌด๊ด€ํ•˜๋ฉฐ, ์‚ฌ์šฉํ•˜๋Š” ์„ค์ •์— ๋งž์ถฐ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

application.yml ์— MQTT URL ์ •๋ณด ์„ค์ •ํ•˜๊ธฐ

MQTT ๋ธŒ๋กœ์ปค URL ์ •๋ณด๋ฅผ ์„ค์ • ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. application.properties ๋“ฑ ์‚ฌ์šฉํ•˜๋Š” ์„ค์ •์— ๋งž๊ฒŒ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค

spring.mqtt.url=tcp://localhost:1883
spring.mqtt.clientId=myClientId
spring.mqtt.username=myUsername // ํ•„์š”ํ•œ ๊ฒฝ์šฐ
spring.mqtt.password=myPassword // ํ•„์š”ํ•œ ๊ฒฝ์šฐ
spring.mqtt.subtopic: LIYO/V1/liyo/+/OUTBOX
pubTopicAddDeviceId: LIYO // ํ•„์š”ํ•œ ๊ฒฝ์šฐ
spring.mqtt.qos: 1

 

URL ์™ธ์—๋„ subtopic๊ณผ qos ์ •๋ณด๋„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

**Topic

MQTT์—์„œ ํ† ํ”ฝ์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „์†ก๋˜๋Š” ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ๋Š” ํŠน์ • ํ† ํ”ฝ์„ ๊ตฌ๋…(subscribe)ํ•˜๊ฑฐ๋‚˜, ํ•ด๋‹น ํ† ํ”ฝ์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœํ–‰(publish)ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ† ํ”ฝ์€ ๊ณ„์ธต๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด ๋ฉ”์‹œ์ง€๋ฅผ ์กฐ์ง์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์™€์ผ๋“œ์นด๋“œ

+ : ํ•œ ๋ ˆ๋ฒจ์„ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. (ex: LIYO/+/liyo)

# : ์—ฌ๋Ÿฌ ๋ ˆ๋ฒจ์„ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. (ex: LIYO/#)

 

QoS ๋ ˆ๋ฒจ

MQTT์˜ QoS(Quality of Service)๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ์ „์†ก๋˜๋Š” ๋ฐฉ์‹๊ณผ ์‹ ๋ขฐ์„ฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

 

QoS 0 : ๋ฉ”์‹œ์ง€๊ฐ€ ํ•œ ๋ฒˆ๋งŒ ์ „์†ก๋˜๋ฉฐ ๊ฐ€์žฅ ๋น ๋ฅด์ง€๋งŒ ์‹ ๋ขฐ์„ฑ์ด ๊ฐ€์žฅ ๋‚ฎ์œผ๋ฉฐ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆฌ๋ฐ ๋“ฑ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

QoS 1 : ๋ฉ”์‹œ์ง€๊ฐ€ ์ตœ์†Œํ•œ ํ•œ ๋ฒˆ ์ด์ƒ ์ „์†ก๋˜๋ฉฐ ์ˆ˜์‹ ์ž๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ฐ›์•„์•ผ ํ•˜๋ฉฐ, ์ค‘๋ณต ์ˆ˜์‹ ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒํƒœ ์—…๋ฐ์ดํŠธ์™€ ๊ฐ™์€ ์ค‘์š”ํ•œ ๋ฉ”์‹œ์ง€์— ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

QoS 2 : ๋ฉ”์‹œ์ง€๊ฐ€ ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ๋งŒ ์ „์†ก๋˜๋ฉฐ ๊ฐ€์žฅ ๋†’์€ ์‹ ๋ขฐ์„ฑ์„ ์ œ๊ณตํ•˜๋ฉฐ ์ค‘๋ณต์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ „์†ก ๊ณผ์ •์ด ๋ณต์žกํ•˜์—ฌ ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธˆ์œต ๊ฑฐ๋ž˜์™€ ๊ฐ™์ด ์ค‘๋ณต์ด ์ ˆ๋Œ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

package com.ebab.beacnd.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Configuration
public class MqttConfig {

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

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

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

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

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

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

 

ํ•„๋“œ์— ์„ค์ •ํŒŒ์ผ์—์„œ ๊ฐ’์„ ์ฃผ์ž…๋ฐ›์•„ ํ•ด๋‹น ๊ฐ’๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.


MQTT ํด๋ผ์ด์–ธํŠธ ์„ค์ •

[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

MQTT_CLIENT_ID ์™€ MQTT_CLIENT_ID2 ๋ฅผ static final ๋ณ€์ˆ˜๋กœ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ ID๊ฐ€ ๋™์ผํ•˜๋ฉด MQTT ๋ธŒ๋กœ๊ฑฐ๊ฐ€ ์—ฐ๊ฒฐ์„ ๋Š์œผ๋ฏ€๋กœ ๋ฐœํ–‰๊ณผ ๊ตฌ๋… ํด๋ผ์ด์–ธํŠธ ID๋ฅผ ๋”ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค


๋ฉ”์‹œ์ง€ ๋ฐœํ–‰ ๋นˆ(Bean) ์ •์˜

MQTT ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ - mqttClientFactory

[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

์ถ”๊ฐ€๋˜๋Š” ์ฝ”๋“œ๋“ค์€ mqttConfig ์ฝ”๋“œ์˜ ํ•˜๋‹จ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ „์ฒด ์ด๋ฏธ์ง€๋Š” ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

// ๋ฉ”์‹œ์ง€ ๋ฐœํ–‰ (Publish)
@Bean
public MqttPahoClientFactory mqttClientFactory() {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    MqttConnectOptions options = new MqttConnectOptions();
    options.setServerURIs(new String[] { brokerUrl });
    options.setUserName(username);
    options.setPassword(password.toCharArray());
    factory.setConnectionOptions(options);
    return factory;
}

MqttPahoClientFactory๋ฅผ ์„ค์ •ํ•˜์—ฌ MQTT ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

MqttConnectOptions๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋กœ์ปค์˜ URL, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋ฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

ํŒจ์Šค์›Œ๋“œ๋Š” toCharArray() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ž์—ด(String)์€ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋กœ, ๋ฉ”๋ชจ๋ฆฌ์— ๋ฌธ์ž์—ด์ด ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ์–ด ์•”ํ˜ธํ™” ๊ฐ™์€ ๋ฏผ๊ฐํ•œ ์ •๋ณด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‰ฝ๊ฒŒ ์ ‘๊ทผ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. char[] ๋ฐฐ์—ด ๊ฐ์ฒด ์‚ฌ์šฉ์œผ๋กœ ์‚ฌ์šฉ ํ›„ ์ฆ‰์‹œ ๋‚ด์šฉ์„ ์ง€์›Œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‚จ์•„ ์žˆ์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.


MQTT ๋ฉ”์‹œ์ง€ ๋ฐœํ–‰ ์ฑ„๋„ ์„ค์ • - mqttOutbound

@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound() {
    MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(MQTT_CLIENT_ID2, mqttClientFactory());
    messageHandler.setAsync(true);
    messageHandler.setDefaultTopic(pubTopicAddDeviceId);
    messageHandler.setCompletionTimeout(1000);
    return messageHandler;
}

๋ฐœํ–‰ ์ฑ„๋„์„ ์„ค์ •ํ•˜๋Š” ๋นˆ์ž…๋‹ˆ๋‹ค. MqttPahoMessageHandler๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋น„๋™๊ธฐ๋กœ ๋ฐœํ–‰ํ•ฉ๋‹ˆ๋‹ค. 

๊ธฐ๋ณธ ๋ฐœํ–‰ ํ† ํ”ฝ์€ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ด ๋‘” pubTopicAddDevice ๋กœ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


MQTT ๋ฉ”์‹œ์ง€ ๋ฐœํ–‰ ์ฑ„๋„ - mqttOutboundChannel

@Bean
public MessageChannel mqttOutboundChannel() {
    return new DirectChannel();
}

๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ฑ„๋„์ž…๋‹ˆ๋‹ค.


MQTT ๋ฉ”์‹œ์ง€ ์ „์†ก ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • - MyGateway

@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MyGateway {
    void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic);
}

 

๋ฉ”์‹œ์ง€๋ฅผ MQTT๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.

@MessageGatewqy ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด Spring Integration์˜ ๋ฉ”์‹œ์ง€ ๊ฒŒ์ดํŠธ์›จ์ด๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

 


๋ฉ”์‹œ์ง€ ๊ตฌ๋… ๋นˆ(Bean) ์ •์˜

๋ฉ”์‹œ์ง€ ๊ตฌ๋… ์„ค์ • - mqttInputChannel

@Bean
public MessageChannel mqttInputChannel() {
    return new DirectChannel();
}

MQTT ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•  ์ฑ„๋„์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. DirectChannel์€ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋™๊ธฐ์  ์ฑ„๋„์ž…๋‹ˆ๋‹ค.


MQTT ๋ฉ”์‹œ์ง€ ์ƒ์‚ฐ์ž ์„ค์ • - inboundChannel

@Bean
public MessageProducer inboundChannel() {
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(brokerUrl, MQTT_CLIENT_ID, subTopic);
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(qos);
    adapter.setOutputChannel(mqttInputChannel());
    return adapter;
}

MQTT ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ์–ด๋Œ‘ํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

  • brokerUrl: MQTT ๋ธŒ๋กœ์ปค์˜ URL.
  • MQTT_CLIENT_ID: ํด๋ผ์ด์–ธํŠธ ID.
  • subTopic: ๊ตฌ๋…ํ•  ์ฃผ์ œ.
  • setCompletionTimeout(5000): ๋ฉ”์‹œ์ง€ ์ „์†ก ์™„๋ฃŒ ๋Œ€๊ธฐ ์‹œ๊ฐ„ ์„ค์ • (5์ดˆ).
  • setConverter: ๋ฉ”์‹œ์ง€ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • setQos(qos): QoS(์„œ๋น„์Šค ํ’ˆ์งˆ) ์ˆ˜์ค€ ์„ค์ •.
  • setOutputChannel(mqttInputChannel()): ์ˆ˜์‹ ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ mqttInputChannel๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

MQTT ํ†ต์‹  ์ด๋ ฅ DB ์ €์žฅ - mqttServiceProcess

public void mqttServiceProcess(MqttDto mdto, String topic, String inOugGb, String payload) {
    mdto.setTopic(topic);
    mdto.setDirection_cd(inOugGb);
    mdto.setPayload(payload);
    mqttService.insertACND0910(mdto);
}

MQTT ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ mdto๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

ํ† ํ”ฝ ์—ฐ๊ฒฐ ํ™•์ธ

๋ณธ๋ฌธ๋‚ด์šฉ ์‚ฝ์ž…

public boolean isTopicConnection(String topic) {
    boolean result = false;
    return result;
}

ํŠน์ • ํ† ํ”ฝ์˜ ์—ฐ๊ฒฐ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ํ•ญ์ƒ false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด ๋ฐ˜ํ™˜

public String booleanString(String bool) {
    String returnStr = "N";
    if ("true".equals(bool)) {
        returnStr = "Y";
    }
    return returnStr;
}

๋ฌธ์ž์—ด "ture"๋ฅผ ํ™•์ธํ•˜์—ฌ "Y" ๋˜๋Š” "N"์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ Boolean ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 


MQTT ์š”์ฒญ ์ฒ˜๋ฆฌ - callMqttservice

๋ณธ๋ฌธ๋‚ด์šฉ ์‚ฝ์ž…

// MQTT ์š”์ฒญ
public String callMqttService(SubPayLoad subPayLoad, String inboxGb) {
    String resMessage = "";
    try {
        log.info(":::::::::::::::::::::::MQTT ์ „์†ก ์ „ getDeviceId {}", subPayLoad.getDeviceId());
        // data์— request data ์กฐํšŒํ•˜์—ฌ ๊ฐ€์ ธ์˜ด
        
        log.info("resMessage: {}", resMessage);
        myGateway.sendToMqtt(resMessage, pubTopicAddDeviceId);

    } catch (Exception e) {
        log.info(":::::::::::::::::::::::callMqttService Exception : {}", e.toString());
        resMessage = null;
        return resMessage;
    }

    return resMessage;
}

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋ฐœํ–‰์€ ๋”ฐ๋กœ ํ•˜์ง€ ์•Š๊ณ , ๊ตฌ๋…ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ์ €์žฅํ•˜๊ธฐ ๊นŒ์ง€๋งŒ ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋‚ด์šฉ์„ ๋‹ด์€ Mqtt ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ MQTT ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ์„ค์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


MQTT ๋ฉ”์‹œ์ง€ ๊ตฌ๋… ํ›„ ์ฒ˜๋ฆฌ - inboundMessageHandler

[SpringBoot] MQTT ๊ธฐ๋ฐ˜ ๋‹จ๋ง๊ธฐ ํ†ต์‹  API ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

MQTT ๋ฉ”์‹œ์ง€ ์ˆ˜์‹  ๋ฐ ๋กœ๊ทธ ์ถœ๋ ฅ

String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
log.info(":::::::::::::::::::::::Topic:{}", topic);
log.info(":::::::::::::::::::::::Payload:{}", message.getPayload());

MQTT ๋ฉ”์‹œ์ง€์˜ ํ† ํ”ฝ๊ณผ Payload๋ฅผ ๋กœ๊น…ํ•ด ์ค๋‹ˆ๋‹ค. ๋ธŒ๋กœ์ปค๋กœ๋ถ€ํ„ฐ ์ˆ˜์‹ ํ•œ ๋ฉ”์‹œ์ง€์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


Payload JSON ํŒŒ์‹ฑ

String payload = message.getPayload().toString();
ObjectMapper mapper = new ObjectMapper();
SubPayLoad subPayLoad = mapper.readValue(payload, SubPayLoad.class);

ํŽ˜์ด๋กœ๋“œ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ  ์ด๋ฅผ SubPayload ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด ์ค๋‹ˆ๋‹ค.

ObjectMapper ๋ฅผ ์‚ฌ์šฉํ•ด์„œ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋งคํ•‘ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.


๋ฉ”์‹œ์ง€ ํƒ€์ž…์— ๋”ฐ๋ฅธ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ

String messageType = subPayLoad.getMessageType();
switch (messageType) {
    case "EVENT_BOOTUP":
        // ๋””๋ฐ”์ด์Šค ๋ถ€ํŒ… ์ฒ˜๋ฆฌ
        break;
    case "EVENT_SENSOR_REPORT":
        // ์„ผ์‹ฑ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
        break;
    default:
        break;
}

 

subPayLoad์—์„œ MessageType ์„ ์ถ”์ถœํ•˜์—ฌ ๋ฉ”์‹œ์ง€ ํƒ€์ž…๋ณ„ ๋ถ„๊ธฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

์ €๋Š” ๋‹จ๋ง๊ธฐ์˜ ๋ถ€ํŒ…๊ณผ, ์„ผ์‹ฑ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” MQTT ์„œ๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋””๋ฐ”์ด์Šค ๋ถ€ํŒ…, ์„ผ์‹ฑ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๋ฉ”์‹œ์ง€๋กœ ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ์ •๋ณด๋“ค์„ ํ†ตํ•ด ๋‹จ๋ง๊ธฐ ์„ค์ • ์ •๋ณด, ํ˜„์žฌ ์ƒํƒœ, ์—ฐ๊ฒฐ ์ด๋ ฅ, ์„ผ์‹ฑ ๋ฐ์ดํ„ฐ ์ด๋ ฅ ๋“ฑ์„ ์ €์žฅํ•˜์—ฌ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


๊ฐœ๋ฐœ ํ™˜๊ฒฝ

  • Frontend
    • Framework: Vue 3
    • Build Tool: Vite
  • Backend
    • Framework: Spring Boot 3.2.2
    • Language: Java
    • Gateway: Spring Cloud Gateway
  • Database
    • DBMS: MS SQL Server
    • Container: Docker
  • Development Tools
    • IDE: Visual Studio Code (VSCode)
๋ฐ˜์‘ํ˜•