mqtt+java+spring(含断线重连)

本文详细介绍如何在Spring Boot项目中集成MQTT协议,包括添加依赖、配置参数、编码MQTT客户端、配置类及测试发布订阅功能。通过具体代码示例,展示了如何实现与MQTT服务端的连接、消息发布和订阅。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.pom.xml中添加mqtt的依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
        </dependency>

二.application.properties配置文件中添加配置(其中MQTT的服务端使用的是ActiveMQ):

## MQTT##
mqtt.host=tcp://127.0.0.1:1883
mqtt.clientId=mqttClient
mqtt.username=admin
mqtt.password=admin
mqtt.topicName1=topic1
mqtt.topicName2=topic2
mqtt.timeout=1000
mqtt.keepalive=2000

三.编码自己的MQTT客户端:

package mqtt2;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * description:
 * author: yangzihe
 * date: 2018-12-17 17:33
 **/
public class MyMQTTClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMQTTClient.class);

    private static MqttClient client;

    public static MqttClient getClient() {
        return client;
    }

    public static void setClient(MqttClient client) {
        MyMQTTClient.client = client;
    }

    private String host;
    private String username;
    private String password;
    private String clientId;
    private int timeout;
    private int keepalive;

    public MyMQTTClient(String host, String username, String password, String clientId, int timeOut, int keepAlive) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.clientId = clientId;
        this.timeout = timeOut;
        this.keepalive = keepAlive;
    }

    /**
     * 设置mqtt连接参数
     *
     * @param username
     * @param password
     * @param timeout
     * @param keepalive
     * @return
     */
    public MqttConnectOptions setMqttConnectOptions(String username, String password, int timeout, int keepalive) {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        options.setConnectionTimeout(timeout);
        options.setKeepAliveInterval(keepalive);
        options.setCleanSession(false);
        return options;
    }

    /**
     * 连接mqtt服务端,得到MqttClient连接对象
     */
    public void connect() throws MqttException {
        if (client == null) {
            client = new MqttClient(host, clientId, new MemoryPersistence());
            client.setCallback(new MyMQTTCallback(MyMQTTClient.this));
        }
        MqttConnectOptions mqttConnectOptions = setMqttConnectOptions(username, password, timeout, keepalive);
        if (!client.isConnected()) {
            client.connect(mqttConnectOptions);
        } else {
            client.disconnect();
            client.connect(mqttConnectOptions);
        }
        LOGGER.info("MQTT connect success");//未发生异常,则连接成功
    }

    /**
     * 发布,默认qos为0,非持久化
     *
     * @param pushMessage
     * @param topic
     */
    public void publish(String pushMessage, String topic) {
        publish(pushMessage, topic, 0, false);
    }

    /**
     * 发布消息
     *
     * @param pushMessage
     * @param topic
     * @param qos
     * @param retained:留存
     */
    public void publish(String pushMessage, String topic, int qos, boolean retained) {
        MqttMessage message = new MqttMessage();
        message.setPayload(pushMessage.getBytes());
        message.setQos(qos);
        message.setRetained(retained);
        MqttTopic mqttTopic = MyMQTTClient.getClient().getTopic(topic);
        if (null == mqttTopic) {
            LOGGER.error("topic is not exist");
        }
        MqttDeliveryToken token;//Delivery:配送
        synchronized (this) {//注意:这里一定要同步,否则,在多线程publish的情况下,线程会发生死锁,分析见文章最后补充
        try {
            token = mqttTopic.publish(message);//也是发送到执行队列中,等待执行线程执行,将消息发送到消息中间件
            token.waitForCompletion(1000L);
        } catch (MqttPersistenceException e) {
            e.printStackTrace();
        } catch (MqttException e) {
            e.printStackTrace();
        }
        }
    }

    /**
     * 订阅某个主题,qos默认为0
     *
     * @param topic
     */
    public void subscribe(String topic) {
        subscribe(topic, 0);
    }

    /**
     * 订阅某个主题
     *
     * @param topic
     * @param qos
     */
    public void subscribe(String topic, int qos) {
        try {
            MyMQTTClient.getClient().subscribe(topic, qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

四.编码自己的MQTT回调对象:

package mqtt2;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * description:
 * author: yangzihe
 * date: 2018-12-17 17:37
 **/
public class MyMQTTCallback implements MqttCallback {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMQTTCallback.class);

    private MyMQTTClient myMQTTClient;

    public MyMQTTCallback(MyMQTTClient myMQTTClient) {
        this.myMQTTClient = myMQTTClient;
    }

    /**
     * 丢失连接,可在这里做重连
     * 只会调用一次
     *
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        LOGGER.error("连接断开,下面做重连...");
        long reconnectTimes = 1;
        while (true) {
            try {
                if (MyMQTTClient.getClient().isConnected()) {
                    LOGGER.warn("mqtt reconnect success end");
                    return;
                }
                LOGGER.warn("mqtt reconnect times = {} try again...", reconnectTimes++);
                MyMQTTClient.getClient().reconnect();
            } catch (MqttException e) {
                LOGGER.error("", e);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                //e1.printStackTrace();
            }
        }
    }

    /**
     * 消息到达后
     * subscribe后,执行的回调函数
     *
     * @param s
     * @param mqttMessage
     * @throws Exception
     */
    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        // subscribe后得到的消息会执行到这里面
        LOGGER.info("接收消息主题 : {},接收消息内容 : {}", s, new String(mqttMessage.getPayload()));
    }

    /**
     * publish后,配送完成后回调的方法
     *
     * @param iMqttDeliveryToken
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
//        LOGGER.info("==========deliveryComplete={}==========", iMqttDeliveryToken.isComplete());
    }
}

五.编码MQTT配置类,将自己的MQTTClient客户端对象注入到spring容器中:

package mqtt2;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description:
 * author: yangzihe
 * date: 2018-12-17 17:36
 **/
@Configuration
@Slf4j
public class MqttConfiguration {

    @Value("${mqtt.host}")
    String host;
    @Value("${mqtt.username}")
    String username;
    @Value("${mqtt.password}")
    String password;
    @Value("${mqtt.clientId}")
    String clientId;
    @Value("${mqtt.timeout}")
    int timeOut;
    @Value("${mqtt.keepalive}")
    int keepAlive;

    @Bean//注入spring
    public MyMQTTClient myMQTTClient() {
        MyMQTTClient myMQTTClient = new MyMQTTClient(host, clientId, username, password, timeOut, keepAlive);
        for (int i = 0; i < 10; i++) {
            try {
                myMQTTClient.connect();
                return myMQTTClient;
            } catch (MqttException e) {
                log.error("MQTT connect exception,connect time = " + i);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                //e.printStackTrace();
            }
        }
        return myMQTTClient;
    }

}

六.编写测试类,测试发布以及订阅:

package demo;

import mqtt2.MyMQTTClient;
import mqtt2.SpringBootApplicationMQTT2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * description:
 * author: yangzihe
 * date: 2018-12-17 17:37
 **/
@SpringBootTest(classes = SpringBootApplicationMQTT2.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class MQTTTest2 {

    @Autowired
    private MyMQTTClient myMQTTClient;

    @Test
    public void testProduce() throws Exception {
        long l = System.nanoTime();
        for (int i = 0; i < 20000; i++) {
            String topicName = "topic11";
            //myMQTTClient.subscribe(topicName);
            myMQTTClient.publish(topicName + "发送消息" + i, topicName);
        }
        long l1 = System.nanoTime();
        double d = ((double) (l1 - l)) / 1000000000;
        long l2 = System.nanoTime();
        for (int i = 0; i < 20000; i++) {
            String topicName = "topic22";
            //myMQTTClient.subscribe(topicName);
            myMQTTClient.publish(topicName + "发送消息" + i, topicName);
        }
        double d2 = ((double) (System.nanoTime() - l2)) / 1000000000;
        System.err.println("=====================第一个topic发送2万数据花费时间:=================" + d + "秒");//2秒多
        System.err.println("=====================第二个topic发送2万数据花费时间:=================" + d2 + "秒");
    }

    /**
     * 分级发布与订阅
     * @throws Exception
     */
    @Test
    public void testTopic() throws Exception {
        int count = 10;
        String topicName1 = "topic33/type/name";
        String topicName2 = "topic33/type";
        String topicName3 = "topic33";
        myMQTTClient.subscribe(topicName3+"/#");
        for (int i = 0; i < count; i++) {
            myMQTTClient.publish(topicName1 + "发送消息" + i, topicName1);
        }
        for (int i = 0; i < count; i++) {
            myMQTTClient.publish(topicName2 + "发送消息" + i, topicName2);
        }
        for (int i = 0; i < count; i++) {
            myMQTTClient.publish(topicName3 + "发送消息" + i, topicName3);
        }
    }

}

 其中SpringBootApplicationMQTT2是springboot的启动类:

package mqtt2;

import org.springframework.boot.SpringApplication;

/**
 * ClassName: SpringBootApplication
 * description:
 * author: yangzihe
 * date: 2018-09-30 09:15
 **/
@org.springframework.boot.autoconfigure.SpringBootApplication//@EnableAutoConfiguration @ComponentScan
public class SpringBootApplicationMQTT2 {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplicationMQTT2.class, args);
    }


}

运行结果:

 

参考博客:https://blog.youkuaiyun.com/hao114500043/article/details/81742849

 

补充:为什么publish时一定要同步?

            synchronized (this) {
                try {
                    token = mqttTopic.publish(message);
                    token.waitForCompletion(1000L);
                } catch (MqttPersistenceException e) {
                    e.printStackTrace();
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }

查看mqttTopic.publish(message)源码:

    public MqttDeliveryToken publish(MqttMessage message) throws MqttException, MqttPersistenceException {
        MqttDeliveryToken token = new MqttDeliveryToken(this.comms.getClient().getClientId());
        token.setMessage(message);
        this.comms.sendNoWait(this.createPublish(message), token);
        token.internalTok.waitUntilSent();
        return token;
    }

每次publish都是new MqttDeliveryToken,然后查看token.internalTok.waitUntilSent()源码:

    public void waitUntilSent() throws MqttException {
        Object var1 = this.sentLock;
        synchronized(this.sentLock) {
            Object var2 = this.responseLock;
            synchronized(this.responseLock) {
                if (this.exception != null) {
                    throw this.exception;
                }
            }

            while(!this.sent) {
                try {
                    log.fine(CLASS_NAME, "waitUntilSent", "409", new Object[]{this.getKey()});
                    //消息未发送到队列中,sentLock就wait,线程进入等待,同时释放了锁sentLock
                    this.sentLock.wait();
                } catch (InterruptedException var3) {
                    ;
                }
            }

            if (!this.sent) {
                if (this.exception == null) {
                    throw ExceptionHelper.createMqttException(6);
                } else {
                    throw this.exception;
                }
            }
        }
    }

this.sentLock.wait()在当前线程还未将消息发送到队列中去时,当前线程进入等待状态,在有其他线程唤醒时,代码继续往下执行。一直到消息发送到队列中标识sent为true时,线程结束循环,结束函数。若没有其他线程唤醒或者消息发送标识sent=false,线程则一直等待下去。

因为是new MqttDeliveryToken,Token也是new的,所以sent不会受多线程影响(每条消息有着自己的sent)。

所以猜测是没有线程唤醒本线程,那么什么时候才会有线程唤醒本线程?

猜测:        this.comms.sendNoWait(this.createPublish(message), token);    中有taskExecutor去执行“将消息发送到队列中”task,task执行完成后,会将sent=true,然后notifyAll()。然而,在多线程的时候,this.comms.sendNoWait(this.createPublish(message), token)的this是同一个MqttTopic时(topic名相同时),线程1等待时被线程2抢到cpu执行权,线程2会将taskExecutor执行的task更新为线程2的task,线程1再执行时没有了task,不会notifyAll(),就会一直等待下去了。

 

<think>我们使用以下技术栈: - Spring Boot:作为后端框架 - MyBatis:用于数据库操作 - MQTT:用于从KepServer接收实时数据 - KepServer:作为OPC UA服务器,与西门子PLC通信,并将数据通过MQTT发布 - MySQL:存储数据 - Kafka:可选,用于大规模数据缓冲(根据需求,这里用户要求使用Kafka,所以我们将数据先通过MQTT接收,再发送到Kafka,然后由消费者存入MySQL) 步骤概述: 1. 配置KepServer,使其通过MQTT发布西门子PLC的数据。 2. 创建Spring Boot项目,集成MQTT客户端,订阅KepServer发布的主题,接收数据。 3. 将接收到的数据发送到Kafka(可选,但用户要求,所以加入)。 4. 创建Kafka消费者,将数据解析并存储到MySQL数据库,使用MyBatis进行数据操作。 5. 配置MySQL数据库。 详细步骤: ### 1. 配置KepServer发布MQTT数据 - 在KepServer中,配置一个MQTT的客户端通道,设置MQTT代理(broker)的地址(比如本地的mosquitto或者EMQX等)。 - 在KepServer中创建标签(Tag)对应PLC的数据点,并设置发布到MQTT的主题(Topic)和格式(例如JSON)。 ### 2. 创建Spring Boot项目 使用Spring Initializr(https://start.spring.io/)创建项目,选择依赖: - Spring Web - MyBatis Framework - MySQL Driver - Spring for Apache Kafka - Spring Integration (用于MQTT) 或者手动添加依赖(Maven): ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Spring Kafka --> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <!-- Spring Integration MQTT --> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency> <!-- Lombok(可选,简化代码) --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> ``` ### 3. 配置MQTT订阅 创建MQTT配置类,订阅KepServer发布的主题。 ```java import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.core.MessageProducer; import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; import org.springframework.integration.mqtt.core.MqttPahoClientFactory; import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter; import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; @Configuration public class MqttConfig { private static final String MQTT_BROKER_URL = "tcp://localhost:1883"; private static final String CLIENT_ID = "spring-boot-client"; private static final String TOPIC = "kepserver/data"; @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(new String[]{MQTT_BROKER_URL}); // 如果有用户名和密码 // options.setUserName("username"); // options.setPassword("password".toCharArray()); factory.setConnectionOptions(options); return factory; } @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } @Bean public MessageProducer inbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(CLIENT_ID, mqttClientFactory(), TOPIC); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); adapter.setOutputChannel(mqttInputChannel()); return adapter; } @Bean @ServiceActivator(inputChannel = "mqttInputChannel") public MessageHandler handler() { return message -> { // 这里接收到消息,将消息发送到Kafka String payload = message.getPayload().toString(); // 假设我们有一个Kafka生产者服务 kafkaProducerService.sendMessage(payload); }; } } ``` ### 4. 创建Kafka生产者服务 创建一个Kafka生产者服务,用于将MQTT接收到的消息发送到Kafka主题。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; @Service public class KafkaProducerService { private static final String TOPIC = "plc_data_topic"; @Autowired private KafkaTemplate<String, String> kafkaTemplate; public void sendMessage(String message) { kafkaTemplate.send(TOPIC, message); } } ``` 配置Kafka(application.properties): ```properties # Kafka配置 spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer ``` ### 5. 创建Kafka消费者,存储到MySQL 创建Kafka消费者,监听Kafka主题,将消息解析后存储到数据库。 首先,创建实体类(根据PLC数据结构定义): ```java import lombok.Data; import java.util.Date; @Data public class PlcData { private Long id; private String tagName; private Double value; private Date timestamp; // 其他字段... } ``` 创建MyBatis Mapper接口: ```java import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; @Mapper public interface PlcDataMapper { @Insert("INSERT INTO plc_data (tag_name, value, timestamp) VALUES (#{tagName}, #{value}, #{timestamp})") void insert(PlcData plcData); } ``` 创建Kafka消费者服务: ```java import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.ObjectMapper; @Service public class KafkaConsumerService { @Autowired private PlcDataMapper plcDataMapper; @KafkaListener(topics = "plc_data_topic", groupId = "group_id") public void consume(String message) { try { ObjectMapper objectMapper = new ObjectMapper(); PlcData plcData = objectMapper.readValue(message, PlcData.class); plcData.setTimestamp(new Date()); // 如果消息中没有时间戳,则用当前时间 plcDataMapper.insert(plcData); } catch (Exception e) { e.printStackTrace(); } } } ``` ### 6. 配置MySQL 在application.properties中配置MySQL: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/plc_data?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml ``` 创建数据库表: ```sql CREATE TABLE plc_data ( id BIGINT AUTO_INCREMENT PRIMARY KEY, tag_name VARCHAR(255) NOT NULL, value DOUBLE NOT NULL, timestamp DATETIME NOT NULL ); ``` ### 7. 运行与测试 1. 启动KepServer,确保PLC连接正常,并且MQTT发布正常。 2. 启动Kafka和Zookeeper(如果Kafka还未启动)。 3. 启动MySQL数据库。 4. 启动Spring Boot应用。 此时,数据流为:PLC -> KepServer -> MQTT Broker -> Spring Boot MQTT订阅 -> Kafka生产者 -> Kafka -> Kafka消费者 -> MySQL。 ### 注意事项 - 确保所有中间件(MQTT Broker, Kafka, MySQL)已正确安装和配置。 - 根据实际PLC数据结构调整实体类和JSON解析。 - 考虑异常处理、数据持久化、消费者幂等性等问题。 ### 性能优化建议 - 对于高频数据,考虑批量插入数据库。 - 调整Kafka的分区和消费者数量。 - 使用连接池配置数据库连接。 §§相关问题§§ 1. 如何配置KepServer以通过MQTT发布PLC数据? 2. Spring Boot集成MQTT时如何保证消息不丢失? 3. 如何优化Kafka消费者写入数据库的性能? 4. 在实时数据采集中,如何确保数据的一致性和可靠性? 5. 如何监控这个数据采集系统的运行状态?</think>### 西门子PLC数据实时采集存储方案(SpringBoot+MyBatis+MQTT+KepServer+MySQL+Kafka) #### 1. 整体架构流程 ```mermaid graph LR A[西门子PLC] -->|工业协议| B(KepServer OPC UA) B -->|MQTT发布| C(MQTT Broker) C -->|订阅| D(SpringBoot MQTT Client) D -->|写入| E(Kafka) E -->|消费| F(SpringBoot Kafka Consumer) F -->|MyBatis持久化| G(MySQL) ``` #### 2. 详细实现步骤 **步骤1:配置KepServer数据源** 1. 安装KEPServerEX 6+[^1] 2. 创建通道:`Channel > 右键添加通道 > 选择Siemens TCP/IP` 3. 配置设备:`Device > 设置PLC IP地址和机架/插槽号` 4. 创建标签:`Tag > 添加需要采集的PLC变量地址(如DB1.DBD0)` 5. 启用MQTT传输:`项目树 > 配置 > 启用MQTT引擎 > 设置Broker地址` **步骤2:SpringBoot集成MQTT客户端** ```java // MQTT配置类 @Configuration public class MqttConfig { @Value("${mqtt.broker}") private String brokerUrl; @Bean public MqttPahoClientFactory mqttFactory() { DefaultMqttPahoClientFactory factory = new DefaultMttPahoClientFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(new String[]{brokerUrl}); factory.setConnectionOptions(options); return factory; } @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } @Bean public MessageProducer inbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("clientId", mqttFactory(), "kepserver/data/#"); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); adapter.setOutputChannel(mqttInputChannel()); return adapter; } } ``` **步骤3:MQTT消息处理(Kafka写入)** ```java @Service public class MqttService { @Autowired private KafkaTemplate<String, String> kafkaTemplate; @ServiceActivator(inputChannel = "mqttInputChannel") public void handleMessage(Message<?> message) { String payload = message.getPayload().toString(); // 示例payload: { "tag": "DB1.DBD0", "value": 25.6, "timestamp": 1625097600 } kafkaTemplate.send("plc_raw_data", payload); // 写入Kafka原始数据 } } ``` **步骤4:Kafka消费者处理(MyBatis持久化)** ```java @KafkaListener(topics = "plc_raw_data", groupId = "plc-group") public void consume(String message) { PlcData data = parseData(message); // JSON解析 plcDataMapper.insert(data); // MyBatis写入 } // MyBatis Mapper接口 @Mapper public interface PlcDataMapper { @Insert("INSERT INTO plc_data(tag_name, value, timestamp) VALUES(#{tagName}, #{value}, #{timestamp})") void insert(PlcData data); } ``` **步骤5:MySQL表结构设计** ```sql CREATE TABLE plc_data ( id BIGINT AUTO_INCREMENT PRIMARY KEY, tag_name VARCHAR(50) NOT NULL COMMENT 'PLC标签地址', value DOUBLE NOT NULL COMMENT '采集数值', timestamp DATETIME NOT NULL COMMENT '采集时间戳', created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` **步骤6:关键配置文件(application.yml)** ```yaml mqtt: broker: tcp://192.168.1.100:1883 kafka: bootstrap-servers: localhost:9092 producer: key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer spring: datasource: url: jdbc:mysql://localhost:3306/plc_db username: root password: yourpassword driver-class-name: com.mysql.cj.jdbc.Driver ``` #### 3. 性能优化要点 1. **Kafka分区策略**:按PLC设备ID分区,保证数据顺序性 ```java kafkaTemplate.send("plc_raw_data", deviceId, payload); ``` 2. **批量写入**:配置MyBatis批量插入 ```xml <insert id="batchInsert" parameterType="list"> INSERT INTO plc_data(...) VALUES <foreach collection="list" item="item" separator=","> (#{item.tagName},...) </foreach> </insert> ``` 3. **连接池配置**:使用HikariCP提升数据库性能 ```yaml spring: datasource: hikari: maximum-pool-size: 20 connection-timeout: 30000 ``` #### 4. 异常处理机制 - MQTT断线连 ```java options.setAutomaticReconnect(true); // 自动连 ``` - Kafka消费幂等性 ```java @KafkaListener(idempotent = true) // 防止复消费 ``` - 数据库写入试 ```java @Retryable(value = SQLException.class, maxAttempts = 3) ``` #### 5. 验证测试流程 1. 使用MQTT.fx模拟KepServer发布数据 2. 通过Kafka Tool查看消息队列状态 3. 监控MySQL写入性能: ```sql SHOW GLOBAL STATUS LIKE 'Innodb_rows_inserted'; ``` > **注意**:实际部署时需开启防火墙端口(1883 MQTT/9092 Kafka/3306 MySQL)[^2]
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值