change - broker添加配置中心

pull/1/head
Wen JY 1 year ago
parent 372bbee821
commit dccb4e52fb

@ -1,68 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--<parent>
<parent>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-modules</artifactId>
<version>3.6.3</version>
</parent>-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hw-mqtt-broker</artifactId>
<description>MQTT Broker</description>
<description>
Mqtt Broker
</description>
<properties>
<revision>2.1.0</revision>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mica.version>2.7.9</mica.version>
<spring.boot.version>2.7.9</spring.boot.version>
<fastjson.version>1.2.83</fastjson.version>
<tinylog.version>2.6.0</tinylog.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- SpringCloud Alibaba Nacos -->
<!--<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>-->
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-server-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-lite</artifactId>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--Mica-Mqtt-->
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-logging</artifactId>
<artifactId>mica-mqtt-server-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-redis</artifactId>
<version>2.7.9</version>
</dependency>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-openapi</artifactId>
</dependency>
<!-- 开启 prometheus 指标收集,详见: http://localhost:30012/actuator/prometheus -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<artifactId>mica-logging</artifactId>
<version>2.7.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@ -70,52 +67,6 @@
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-bom</artifactId>
<version>${mica.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-server-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<!-- tinylog 内存占用更小、性能更好,适合边缘设备 -->
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>slf4j-tinylog</artifactId>
<version>${tinylog.version}</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>${tinylog.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>

@ -1,5 +1,6 @@
package com.hw.mqtt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@ -1,81 +0,0 @@
package com.hw.mqtt.controller;
import com.hw.mqtt.domain.AjaxResult;
import com.hw.mqtt.service.IMqttBrokerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* Broker Http
*/
@Tag(name = "Mqtt::服务端")
@RequestMapping("/mqtt/server")
@RestController
public class ServerController {
@Autowired
private IMqttBrokerService service;
@Operation(summary = "Broker发布消息")
@PostMapping("/publish")
public AjaxResult publish(String topic,String payload) {
AjaxResult ajaxResult;
boolean result = false;
try{
result = service.publish(topic,payload);
ajaxResult = AjaxResult.success(result);
}catch (Exception ex){
ajaxResult = AjaxResult.error("Broker发布消息异常"+ex.getMessage(),result);
}
return ajaxResult;
}
@Operation(summary = "获取在线客户端数量")
@GetMapping("/getOnlineClientSize")
public AjaxResult getOnlineClientSize() {
AjaxResult ajaxResult;
long onlineClientSize = 0;
try{
onlineClientSize = service.getOnlineClientSize();
ajaxResult = AjaxResult.success(onlineClientSize);
}catch (Exception ex){
ajaxResult = AjaxResult.error("获取在线客户端数量异常:"+ex.getMessage(),onlineClientSize);
}
return ajaxResult;
}
@Operation(summary = "获取在线客户端ID")
@GetMapping("/getOnlineClients")
public AjaxResult getOnlineClients() {
AjaxResult ajaxResult;
List<String> nodes = new ArrayList<>();
try{
nodes = service.getOnlineClients();
ajaxResult = AjaxResult.success(nodes);
}catch (Exception ex){
ajaxResult = AjaxResult.error("获取在线客户端ID异常"+ex.getMessage(),nodes);
}
return ajaxResult;
}
@Operation(summary = "主动关闭指定客户端连接")
@PostMapping("/closeClientById")
public AjaxResult closeClientById(String clientId) {
AjaxResult ajaxResult;
boolean result = false;
try{
result = service.closeClientById(clientId);
ajaxResult = AjaxResult.success(result);
}catch (Exception ex){
ajaxResult = AjaxResult.error("主动关闭指定客户端异常:"+ex.getMessage(),result);
}
return ajaxResult;
}
}

@ -24,6 +24,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;

@ -1,29 +0,0 @@
package com.hw.mqtt.service;
import net.dreamlu.iot.mqtt.spring.server.MqttServerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
/**
*
* @author WenJY
* @date 2023-03-14 12:19
* @param null
* @return null
*/
@Service
public class ServerService {
private static final Logger logger = LoggerFactory.getLogger(ServerService.class);
@Autowired
private MqttServerTemplate server;
public boolean publish(String body) {
boolean result = server.publishAll("/test/message", body.getBytes(StandardCharsets.UTF_8));
logger.info("Mqtt publishAll result:{};payload:{}", result,body);
return result;
}
}

@ -0,0 +1,22 @@
# Tomcat
server:
port: 9206
# Spring
spring:
application:
# ????
name: hw-mqtt-broker
profiles:
# ????
active: dev
cloud:
nacos:
config:
# ??????
server-addr: huawei-nacos:8848
# ??????
file-extension: yml
# ????
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
Loading…
Cancel
Save