Django集成Mqtt协议与Bpringboot 集成MQtt实现跨系统跨网络实现信息通信(工作日常学习笔记分享工作优先每天会更新一点直到功能完整)

MQTT(消息队列遥测传输)是ISO 标准(ISO/IEC PRF 20922)下基于发布/订阅范式的消息协议。它工作在 TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而设计的发布/订阅型消息协议,为此,注意重点它需要一个消息中间件 。 我要说的是大家注意黑色加粗部分的文字,
上代码,首先说下我的需求,公司项目 分为两部分我这里标注为A和B (说明下A是基于springboot的后管理系统,B是基于Django框架的python数据采集系统),B被分别部署在多个(超过300个虚拟机)服务器虚拟机上运行,现在要实现的是在A的系统中发送一条信息个就能同时去分别自动更新这多个虚拟机上的B服务代码并且自动重启服务。(经过学习筛选我选择MQTT协议简单方便)他的工作原理简单: 在我的项目中serverMqtt.py

在这里插入图片描述

首先在记录在Django中是如何实现集成mqtt的上代码:

# 为了能在外部脚本中调用Django ORM模型,必须配置脚本环境变量,将脚本注册到Django的环境变量中
import os, sys
import django

# 第1个参数固定,第2个参数是工程名称.settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '自己的项目.settings')
django.setup()

# 引入mqtt包
import paho.mqtt.client as mqtt
# 使用独立线程运行
from threading import Thread
# from app名 import models
import time
import json

# 建立mqtt连接
def on_connect(client, userdata, flag, rc):
    print("Connect with the result code——001 " + str(rc))
    client.subscribe('test_ljz/#', qos=2)

# 接收、处理mqtt消息
def on_message(client, userdata, msg):
    out = str(msg.payload.decode('utf-8'))
    print(str(msg.topic))
    print(out)
    out = json.loads(out)
    download_and_unzip(out)
    # 收到消息后执行任务
    if msg.topic == 'test_001':
        print(str(out))

# mqtt客户端启动函数
def mqttfunction():
    global client
    # 使用loop_start 可以避免阻塞Django进程,使用loop_forever()可能会阻塞系统进程
    # client.loop_start()
    # client.loop_forever() 有掉线重连功能
    client.loop_forever(retry_first_connection=True)

client = mqtt.Client(client_id="test", clean_session=False)

# 启动函数
def mqtt_run():
    client.on_connect = on_connect
    client.on_message = on_message
    # 绑定 MQTT 服务器地址
    broker = '***.***.21.***'
    # MQTT服务器的端口号
    client.connect(broker, 1883, 62)
    client.username_pw_set('user_001', 'user_001')
    client.reconnect_delay_set(min_delay=1, max_delay=2000)
    # 启动
    mqttthread = Thread(target=mqttfunction)
    mqttthread.start()

# 启动 MQTT
# mqtt_run()

# if __name__ == "__main__":
#     mqtt_run()
#根据端口号pid关闭进程
import os
import subprocess
def kill_process_by_port(port):
    try:
        # 查找在指定端口上运行的进程的PID
        output = subprocess.check_output(['netstat', '-ano'], universal_newlines=True)
        for line in output.split('\n'):
            if 'LISTENING' in line and f':{port}' in line:
                pid = line.split()[-1]
                break
        else:
            print(f"No process found on port {port}")
            return
        # 使用PID终止进程
        os.system(f"taskkill /F /PID {pid}")
        print(f"Process with PID {pid} killed")
    except subprocess.CalledProcessError as e:
        print("Error:", e)

#Springboot部分代码

yml文件代码:
  mqtt:
    username: forest_meetingSever01 # 用户名
    password: 12***6 # 密码
    hostUrl: tcp://112.***.***:1883 # tcp://ip:端口
    #hostUrl: tcp://localhost:1883 # tcp://ip:端口
    clientId: clientId_meetingserver # 客户端id
    defaultTopic: test_ljz # 订阅主题,如果多个,则以,(英文逗号)分开
    timeout: 100 # 超时时间 (单位:秒)
    keepalive: 60 # 心跳 (单位:秒)
    enabled: true # 是否使能mqtt功能
消息发送
package com.njclw.common.mqtt;

import static com.njclw.common.mqtt.AjaxResult.error;
import static com.njclw.common.mqtt.AjaxResult.success;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MqttPushClient {
	private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);

	@Autowired
	private PushCallback pushCallback;

	private static MqttClient client;

	private static MqttClient getClient() {
		return client;
	}

	private static void setClient(MqttClient client) {
		MqttPushClient.client = client;
	}

	/**
	 * 客户端连接
	 *
	 * @param host ip+端口
	 * @param clientID 客户端Id
	 * @param username 用户名
	 * @param password 密码
	 * @param timeout 超时时间
	 * @param keepalive 保留数
	 */
	public void connect(String host, String clientID, String username, String password, int timeout, int keepalive) throws Exception {
		MqttClient client;
		// try {
		client = new MqttClient(host, clientID, new MemoryPersistence());

		MqttConnectOptions options = new MqttConnectOptions();
		options.setCleanSession(true);
		options.setUserName(username);
		options.setPassword(password.toCharArray());
		options.setConnectionTimeout(timeout);
		options.setKeepAliveInterval(keepalive);
		options.setAutomaticReconnect(true); // todo ygz 2022.5.25 设置为自动连接,还未测试

		MqttPushClient.setClient(client);

		// try {

		client.setCallback(pushCallback);
		client.connect(options);

//			} catch (Exception e) {
//				logger.error(e.getMessage() + " " + e.toString());
//			}
//		} catch (Exception e) {
//			logger.error(e.getMessage() + " " + e.toString());
//		}
	}

	/**
	 * 发布
	 *
	 * @param qos 连接方式
	 * @param retained 是否保留
	 * @param topic 主题
	 * @param pushMessage 消息体
	 */
	public AjaxResult publish(int qos, boolean retained, String topic, String pushMessage) {
		try {
			MqttMessage message = new MqttMessage();
			message.setQos(qos);
			message.setRetained(retained);
			message.setPayload(pushMessage.getBytes("UTF-8"));
			MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic);
			if (null == mTopic) {
				logger.error("topic not exist");
			}
			MqttDeliveryToken token;
			token = mTopic.publish(message);
			token.waitForCompletion();
			return success();
		} catch (MqttPersistenceException e) {
			logger.error(e.getMessage() + " " + e.toString());
			return error();
		} catch (MqttException e) {
			logger.error(e.getMessage() + " " + e.toString());
			return error();
		} catch (Exception e) {
			return error();
		}
	}

	public void publishLoginToApp(String userId, String jsonMsg) {
		publish(2, false, "cncuserid_" + userId, jsonMsg); // 2: 确保信息可靠, false: 避免设备上线收到老消息
	}

	/**
	 * 订阅某个主题
	 *	 * @param topic 主题
	 * @param qos 连接方式
	 */

	public void subscribe(String topic, int qos) throws MqttException {
		//try {
			MqttPushClient.getClient().subscribe(topic, qos);
//		} catch (MqttException e) {
//			logger.error(e.getMessage() + " " + e.toString());
//			throw e;
//		}
	}

}


消息接收
package com.njclw.common.mqtt;

import java.util.concurrent.TimeUnit;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
//import com.njclw.ssap.cncMachine.service.impl.CncMachineService;
//import com.njclw.ssap.uveLog.service.impl.UveLogService;

@Component
public class PushCallback implements MqttCallbackExtended {

	private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);

	private static MqttClient client;
	private static String _topic;
	private static String _qos;
	private static String _msg;
	
	
	@Autowired
	private MqttConfig mqttConfig;

	/*@Autowired
	private CncMachineService cncMachineService;

	@Autowired
	private UveLogService uveLogService;*/

	@Override
	public void connectionLost(Throwable throwable) {

		logger.info("连接断开,可以做重连");

		if (client != null && client.isConnected()) {
			return;
		}

		while (true) {
			try {
				mqttConfig.getMqttPushClient();
				break;
			} catch (Exception e) {
				logger.error(e.getMessage());
				
				//e.printStackTrace();
				
				//uveLogService.saveContent("重新连接emqx时报错:" + e.getMessage());

				try {
					TimeUnit.SECONDS.sleep(30);
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}

	}

	@Override
	public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {

		// subscribe后得到的消息会执行到这里面
		// logger.error("接收消息主题 : " + topic);
		// logger.error("接收消息Qos : " + mqttMessage.getQos());
		// logger.error("接收消息内容 : " + new String(mqttMessage.getPayload()));

		_topic = topic;
		_qos = mqttMessage.getQos() + "";
		_msg = new String(mqttMessage.getPayload());

		//cncMachineService.processMqttListen(_msg);
	}

	@Override
	public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
		logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
	}

	// 别的Controller层会调用这个方法来 获取 接收到的硬件数据
	public String receive() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("topic", _topic);
		jsonObject.put("qos", _qos);
		jsonObject.put("msg", _msg);
		return jsonObject.toString();
	}

	@Override
	public void connectComplete(boolean reconnect, String serverURI) {
		logger.info("reconnect=" + reconnect);
		logger.info("serverURI=" + serverURI);
	}

}

链接:
@Bean
	public MqttPushClient getMqttPushClient() throws MqttException, Exception {
		if (enabled == true) {
			String mqtt_topic[] = defaultTopic.split(","); // StringUtils.split(defaultTopic, ",");
			mqttPushClient.connect(hostUrl, clientId + "_" + StringUtil.getUUID2String(), username, password, timeout, keepalive);// 连接
			for (int i = 0; i < mqtt_topic.length; i++) {
				mqttPushClient.subscribe(mqtt_topic[i], 2);// 订阅主题
			}
		}
		return mqttPushClient;
	}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java000I

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值