正在显示
13 个修改的文件
包含
0 行增加
和
462 行删除
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | - <modelVersion>4.0.0</modelVersion> | ||
| 6 | - <parent> | ||
| 7 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | - <artifactId>lh-neutrino-proxy</artifactId> | ||
| 9 | - <version>1.0-SNAPSHOT</version> | ||
| 10 | - </parent> | ||
| 11 | - | ||
| 12 | - <artifactId>lh-neutrino-proxy-client</artifactId> | ||
| 13 | - | ||
| 14 | - <properties> | ||
| 15 | - <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | - <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | - </properties> | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - <dependencies> | ||
| 22 | - <dependency> | ||
| 23 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 24 | - <artifactId>lh-neutrino-proxy-common</artifactId> | ||
| 25 | - </dependency> | ||
| 26 | - </dependencies> | ||
| 27 | - | ||
| 28 | -</project> |
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | - <modelVersion>4.0.0</modelVersion> | ||
| 6 | - <parent> | ||
| 7 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | - <artifactId>lh-neutrino-proxy</artifactId> | ||
| 9 | - <version>1.0-SNAPSHOT</version> | ||
| 10 | - </parent> | ||
| 11 | - | ||
| 12 | - <artifactId>lh-neutrino-proxy-common</artifactId> | ||
| 13 | - | ||
| 14 | - <properties> | ||
| 15 | - <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | - <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | - </properties> | ||
| 19 | - | ||
| 20 | - <dependencies> | ||
| 21 | - <dependency> | ||
| 22 | - <groupId>org.slf4j</groupId> | ||
| 23 | - <artifactId>slf4j-api</artifactId> | ||
| 24 | - </dependency> | ||
| 25 | - | ||
| 26 | - <dependency> | ||
| 27 | - <groupId>ch.qos.logback</groupId> | ||
| 28 | - <artifactId>logback-classic</artifactId> | ||
| 29 | - </dependency> | ||
| 30 | - <!-- mqtt --> | ||
| 31 | - <dependency> | ||
| 32 | - <groupId>org.eclipse.paho</groupId> | ||
| 33 | - <artifactId>org.eclipse.paho.client.mqttv3</artifactId> | ||
| 34 | - </dependency> | ||
| 35 | - <dependency> | ||
| 36 | - <groupId>com.alibaba</groupId> | ||
| 37 | - <artifactId>fastjson</artifactId> | ||
| 38 | - </dependency> | ||
| 39 | - <dependency> | ||
| 40 | - <groupId>cn.hutool</groupId> | ||
| 41 | - <artifactId>hutool-all</artifactId> | ||
| 42 | - </dependency> | ||
| 43 | - </dependencies> | ||
| 44 | - | ||
| 45 | -</project> |
| 1 | -package com.zhonglai.luhui.neutrino.proxy.common.mqtt; | ||
| 2 | - | ||
| 3 | -import com.alibaba.fastjson.JSONObject; | ||
| 4 | -import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | ||
| 5 | -import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; | ||
| 6 | -import org.eclipse.paho.client.mqttv3.MqttMessage; | ||
| 7 | -import org.slf4j.Logger; | ||
| 8 | -import org.slf4j.LoggerFactory; | ||
| 9 | - | ||
| 10 | -import java.util.Set; | ||
| 11 | - | ||
| 12 | -/** | ||
| 13 | - * mqtt回调 | ||
| 14 | - */ | ||
| 15 | -public class MqttCallback implements MqttCallbackExtended { | ||
| 16 | - private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||
| 17 | - | ||
| 18 | - private MqttService mqttclient; | ||
| 19 | - public MqttCallback(MqttService mqttclient) | ||
| 20 | - { | ||
| 21 | - this.mqttclient = mqttclient; | ||
| 22 | - } | ||
| 23 | - @Override | ||
| 24 | - public void connectComplete(boolean b, String s) { | ||
| 25 | - log.info("连接成功"); | ||
| 26 | - Set<String> topics = mqttclient.subscribe(); | ||
| 27 | - log.info("-----------订阅成功:{}--------------------",topics); | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - @Override | ||
| 31 | - public void connectionLost(Throwable cause) { | ||
| 32 | - log.error("连接丢失重新链接",cause); | ||
| 33 | - while (!mqttclient.connect()) | ||
| 34 | - { | ||
| 35 | - try { | ||
| 36 | - Thread.sleep(60000); | ||
| 37 | - } catch (InterruptedException e) { | ||
| 38 | - e.printStackTrace(); | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - @Override | ||
| 44 | - public void messageArrived(String topic, MqttMessage message) throws Exception { | ||
| 45 | - log.info("mqtt发来消息>>>>>>>:{} 《===============》 字符串:【{}】",topic,message.toString()); | ||
| 46 | - String str = new String(message.getPayload()); | ||
| 47 | - mqttclient.operate(new Topic(topic),JSONObject.parseObject(str)); | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - @Override | ||
| 51 | - public void deliveryComplete(IMqttDeliveryToken token) { | ||
| 52 | - log.info("消息发送完成"); | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - | ||
| 56 | -} |
| 1 | -package com.zhonglai.luhui.neutrino.proxy.common.mqtt; | ||
| 2 | - | ||
| 3 | -import java.io.FileReader; | ||
| 4 | -import java.util.HashSet; | ||
| 5 | -import java.util.Properties; | ||
| 6 | -import java.util.Set; | ||
| 7 | - | ||
| 8 | -public class MqttConfig { | ||
| 9 | - public static String mqttBroker; | ||
| 10 | - public static String mqttUserName; | ||
| 11 | - public static String mqttPassword; | ||
| 12 | - public static String mqttClientId; | ||
| 13 | - | ||
| 14 | - public static Set<String> subTopic; | ||
| 15 | - | ||
| 16 | - public static void init() | ||
| 17 | - { | ||
| 18 | - Properties properties = loadProperties(); | ||
| 19 | - mqttBroker = properties.getProperty("broker"); | ||
| 20 | - mqttUserName = properties.getProperty("username"); | ||
| 21 | - mqttPassword = properties.getProperty("password"); | ||
| 22 | - mqttClientId = properties.getProperty("clientId"); | ||
| 23 | - subTopic = new HashSet<>(); | ||
| 24 | - | ||
| 25 | - String topicsStr = properties.getProperty("subTopic"); | ||
| 26 | - if(null != topicsStr && !"".equals(topicsStr)) | ||
| 27 | - { | ||
| 28 | - if (topicsStr != null && !topicsStr.trim().isEmpty()) { | ||
| 29 | - String[] topics = topicsStr.split(","); | ||
| 30 | - for (String topic : topics) { | ||
| 31 | - subTopic.add(topic.trim()); | ||
| 32 | - } | ||
| 33 | - } | ||
| 34 | - } | ||
| 35 | - } | ||
| 36 | - public static Properties loadProperties() | ||
| 37 | - { | ||
| 38 | - Properties properties = new Properties(); | ||
| 39 | - try { | ||
| 40 | - if(null != System.getProperty("configPath") && !"".equals(System.getProperty("configPath"))) | ||
| 41 | - { | ||
| 42 | - properties.load(new FileReader(System.getProperty("configPath")+"/mqtt.properties")); | ||
| 43 | - }else{ | ||
| 44 | - properties.load(MqttConfig.class.getClassLoader().getResourceAsStream("mqtt.properties")); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - } catch (Exception e) { | ||
| 48 | - e.printStackTrace(); | ||
| 49 | - } | ||
| 50 | - return properties; | ||
| 51 | - } | ||
| 52 | -} |
| 1 | -package com.zhonglai.luhui.neutrino.proxy.common.mqtt; | ||
| 2 | - | ||
| 3 | -import com.alibaba.fastjson.JSONObject; | ||
| 4 | -import org.eclipse.paho.client.mqttv3.MqttClient; | ||
| 5 | -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | ||
| 6 | -import org.eclipse.paho.client.mqttv3.MqttException; | ||
| 7 | -import org.eclipse.paho.client.mqttv3.MqttMessage; | ||
| 8 | -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; | ||
| 9 | -import org.slf4j.Logger; | ||
| 10 | -import org.slf4j.LoggerFactory; | ||
| 11 | - | ||
| 12 | -import javax.annotation.PreDestroy; | ||
| 13 | -import java.util.Set; | ||
| 14 | - | ||
| 15 | -/** | ||
| 16 | - * 通过mqtt实现消息推送,用来处理长链接 | ||
| 17 | - */ | ||
| 18 | -public class MqttService { | ||
| 19 | - private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||
| 20 | - | ||
| 21 | - private MqttClient mqttclient; | ||
| 22 | - private MqttConnectOptions options; | ||
| 23 | - | ||
| 24 | - private OperateService operateService; | ||
| 25 | - | ||
| 26 | - // 使用 volatile 确保多线程内存可见性和禁止指令重排 | ||
| 27 | - private static volatile MqttService instance; | ||
| 28 | - | ||
| 29 | - private MqttService(OperateService operateService) { | ||
| 30 | - this.operateService = operateService; | ||
| 31 | - // 初始化代码 | ||
| 32 | - try { | ||
| 33 | - sart(); | ||
| 34 | - } catch (MqttException e) { | ||
| 35 | - throw new RuntimeException(e); | ||
| 36 | - } | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - public static MqttService getInstance( OperateService operateService) { | ||
| 40 | - if (instance == null) { | ||
| 41 | - synchronized (MqttService.class) { | ||
| 42 | - if (instance == null) { | ||
| 43 | - instance = new MqttService(operateService); | ||
| 44 | - } | ||
| 45 | - } | ||
| 46 | - } | ||
| 47 | - return instance; | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - private void init() throws MqttException { | ||
| 51 | - if(null == mqttclient) | ||
| 52 | - { | ||
| 53 | - mqttclient = new MqttClient(MqttConfig.mqttBroker, MqttConfig.mqttClientId, new MemoryPersistence()); | ||
| 54 | - } | ||
| 55 | - options = new MqttConnectOptions(); | ||
| 56 | - options.setCleanSession(true); | ||
| 57 | - options.setConnectionTimeout(15); | ||
| 58 | - options.setKeepAliveInterval(60); // 添加心跳设置,单位为秒 | ||
| 59 | - //设置断开后重新连接 | ||
| 60 | - options.setAutomaticReconnect(true); | ||
| 61 | - mqttclient.setCallback(new MqttCallback(this)); | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - public boolean connect() { | ||
| 65 | - try { | ||
| 66 | - options.setUserName(MqttConfig.mqttUserName); | ||
| 67 | - options.setPassword(MqttConfig.mqttPassword.toCharArray()); | ||
| 68 | - mqttclient.connect(options); | ||
| 69 | - return true; | ||
| 70 | - } catch (MqttException e) { | ||
| 71 | - log.error("-----------mqtt连接服务器失败--------------------",e); | ||
| 72 | - } | ||
| 73 | - return false; | ||
| 74 | - } | ||
| 75 | - | ||
| 76 | - public void sart() throws MqttException{ | ||
| 77 | - MqttConfig.init(); | ||
| 78 | - log.info("-----------开始启动mqtt监听服务--------------------"); | ||
| 79 | - init(); | ||
| 80 | - connect(); | ||
| 81 | - log.info("-----------mqtt连接服务器成功--------------------"); | ||
| 82 | - | ||
| 83 | - } | ||
| 84 | - | ||
| 85 | - @PreDestroy | ||
| 86 | - public void stop() throws MqttException { | ||
| 87 | - if(null != mqttclient) | ||
| 88 | - { | ||
| 89 | - log.info("退出mqtt服务"); | ||
| 90 | - mqttclient.unsubscribe(MqttConfig.subTopic.toArray(new String[MqttConfig.subTopic.size()])); | ||
| 91 | - mqttclient.disconnect(); | ||
| 92 | - mqttclient.close(); | ||
| 93 | - } | ||
| 94 | - instance = null; | ||
| 95 | - } | ||
| 96 | - public void publish(String topic, String messageStr) throws MqttException { | ||
| 97 | - MqttMessage message = new MqttMessage(); | ||
| 98 | - message.setPayload(messageStr.getBytes()); | ||
| 99 | - mqttclient.publish(topic,message); | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | - public Set<String> subscribe() | ||
| 103 | - { | ||
| 104 | - try { | ||
| 105 | - mqttclient.subscribe(MqttConfig.subTopic.toArray(new String[MqttConfig.subTopic.size()])); | ||
| 106 | - } catch (MqttException e) { | ||
| 107 | - e.printStackTrace(); | ||
| 108 | - } | ||
| 109 | - return MqttConfig.subTopic; | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | - public boolean isConnect() | ||
| 113 | - { | ||
| 114 | - return mqttclient != null && mqttclient.isConnected(); | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - public void operate(Topic topic, JSONObject payloaddata) | ||
| 118 | - { | ||
| 119 | - operateService.operate(topic,payloaddata); | ||
| 120 | - } | ||
| 121 | -} |
| 1 | -package com.zhonglai.luhui.neutrino.proxy.common.mqtt; | ||
| 2 | - | ||
| 3 | -public class Topic { | ||
| 4 | - public static final String TOPIC_PUT = "PUT"; | ||
| 5 | - public static final String TOPIC_READ = "READ"; | ||
| 6 | - private String topicType; | ||
| 7 | - private String time; | ||
| 8 | - public Topic() | ||
| 9 | - { | ||
| 10 | - | ||
| 11 | - } | ||
| 12 | - public Topic(String stopicStr) | ||
| 13 | - { | ||
| 14 | - String[] strs = stopicStr.split("/"); | ||
| 15 | - topicType = strs[0]; | ||
| 16 | - time = strs[1]; | ||
| 17 | - } | ||
| 18 | - | ||
| 19 | - public String getTopicType() { | ||
| 20 | - return topicType; | ||
| 21 | - } | ||
| 22 | - | ||
| 23 | - public void setTopicType(String topicType) { | ||
| 24 | - this.topicType = topicType; | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - public String getTime() { | ||
| 28 | - return time; | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | - public void setTime(String time) { | ||
| 32 | - this.time = time; | ||
| 33 | - } | ||
| 34 | -} |
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | - <modelVersion>4.0.0</modelVersion> | ||
| 6 | - <parent> | ||
| 7 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | - <artifactId>lh-neutrino-proxy</artifactId> | ||
| 9 | - <version>1.0-SNAPSHOT</version> | ||
| 10 | - </parent> | ||
| 11 | - | ||
| 12 | - <artifactId>lh-neutrino-proxy-server</artifactId> | ||
| 13 | - | ||
| 14 | - <properties> | ||
| 15 | - <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | - <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | - </properties> | ||
| 19 | - | ||
| 20 | - <dependencies> | ||
| 21 | - <dependency> | ||
| 22 | - <groupId>io.netty</groupId> | ||
| 23 | - <artifactId>netty-all</artifactId> | ||
| 24 | - </dependency> | ||
| 25 | - <dependency> | ||
| 26 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 27 | - <artifactId>lh-neutrino-proxy-common</artifactId> | ||
| 28 | - </dependency> | ||
| 29 | - <dependency> | ||
| 30 | - <groupId>commons-io</groupId> | ||
| 31 | - <artifactId>commons-io</artifactId> | ||
| 32 | - </dependency> | ||
| 33 | - <dependency> | ||
| 34 | - <groupId>com.alibaba</groupId> | ||
| 35 | - <artifactId>fastjson</artifactId> | ||
| 36 | - </dependency> | ||
| 37 | - </dependencies> | ||
| 38 | - | ||
| 39 | -</project> |
| 1 | -package com.zhonglai.luhui.neutrino.proxy.server; | ||
| 2 | - | ||
| 3 | - | ||
| 4 | -import com.zhonglai.luhui.neutrino.proxy.common.mqtt.MqttConfig; | ||
| 5 | -import com.zhonglai.luhui.neutrino.proxy.common.mqtt.MqttService; | ||
| 6 | -import com.zhonglai.luhui.neutrino.proxy.server.operate.OperateServiceImpl; | ||
| 7 | -import org.eclipse.paho.client.mqttv3.MqttException; | ||
| 8 | -import org.slf4j.Logger; | ||
| 9 | -import org.slf4j.LoggerFactory; | ||
| 10 | - | ||
| 11 | -public class ControlServer { | ||
| 12 | - private static Logger logger = LoggerFactory.getLogger(ControlServer.class); | ||
| 13 | - public static void main(String[] args) throws Exception { | ||
| 14 | - MqttService mqttService = MqttService.getInstance(new OperateServiceImpl()); | ||
| 15 | - mqttService.sart(); | ||
| 16 | - | ||
| 17 | - // 使用Lambda表达式添加关闭钩子 | ||
| 18 | - Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
| 19 | - logger.info("正在关闭应用程序..."); | ||
| 20 | - try { | ||
| 21 | - mqttService.stop(); | ||
| 22 | - } catch (MqttException e) { | ||
| 23 | - logger.error("MQTT服务停止异常", e); | ||
| 24 | - } | ||
| 25 | - logger.info("应用程序关闭成功"); | ||
| 26 | - })); | ||
| 27 | - } | ||
| 28 | -} |
| 1 | -package com.zhonglai.luhui.neutrino.proxy.server.operate; | ||
| 2 | - | ||
| 3 | -import com.alibaba.fastjson.JSONObject; | ||
| 4 | -import com.zhonglai.luhui.neutrino.proxy.common.mqtt.OperateService; | ||
| 5 | -import com.zhonglai.luhui.neutrino.proxy.common.mqtt.Topic; | ||
| 6 | - | ||
| 7 | -public class OperateServiceImpl implements OperateService { | ||
| 8 | - @Override | ||
| 9 | - public void operate(Topic topic, JSONObject data) { | ||
| 10 | - | ||
| 11 | - } | ||
| 12 | -} |
lh-modules/lh-neutrino-proxy/pom.xml
已删除
100644 → 0
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | - <modelVersion>4.0.0</modelVersion> | ||
| 6 | - <parent> | ||
| 7 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | - <artifactId>lh-modules</artifactId> | ||
| 9 | - <version>1.0-SNAPSHOT</version> | ||
| 10 | - </parent> | ||
| 11 | - | ||
| 12 | - <artifactId>lh-neutrino-proxy</artifactId> | ||
| 13 | - <modules> | ||
| 14 | - <module>lh-neutrino-proxy-server</module> | ||
| 15 | - <module>lh-neutrino-proxy-client</module> | ||
| 16 | - <module>lh-neutrino-proxy-common</module> | ||
| 17 | - </modules> | ||
| 18 | - | ||
| 19 | - <properties> | ||
| 20 | - <maven.compiler.source>8</maven.compiler.source> | ||
| 21 | - <maven.compiler.target>8</maven.compiler.target> | ||
| 22 | - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 23 | - </properties> | ||
| 24 | - | ||
| 25 | - <packaging>pom</packaging> | ||
| 26 | - <description> | ||
| 27 | - 透传代理 | ||
| 28 | - </description> | ||
| 29 | -</project> |
| @@ -38,7 +38,6 @@ | @@ -38,7 +38,6 @@ | ||
| 38 | <module>lh-ssh-service-lesten</module> | 38 | <module>lh-ssh-service-lesten</module> |
| 39 | <module>lh-deviceInfo-sync</module> | 39 | <module>lh-deviceInfo-sync</module> |
| 40 | <module>lh-camera</module> | 40 | <module>lh-camera</module> |
| 41 | - <module>lh-neutrino-proxy</module> | ||
| 42 | </modules> | 41 | </modules> |
| 43 | 42 | ||
| 44 | <properties> | 43 | <properties> |
-
请 注册 或 登录 后发表评论