项目&代码
项目结构
一、项目结构
fbmq - application:Spring Boot 启动模块,负责整个项目的启动和初始化操作。
fbmq - bridge - source:数据源桥接模块,用于连接不同的数据源,并进行数据的转换和传递。
fbmq - component:通用组件模块,存放项目中的可复用组件,例如工具类、配置类等。
fbmq - consume:数据消费模块,用于处理从 MQTT 服务器接收的数据,包括解析、处理和存储等操作。
fbmq - mqtt:MQTT 核心模块,负责处理与 MQTT 协议相关的操作,如连接、消息发布和订阅等。
fbmq - route:路由模块,用于处理网络请求的路由,决定请求的转发方向。
fbmq - rules:业务规则模块,存放项目的各种业务规则,确保项目按照特定规则运作。
fbmq - service:业务服务模块,实现具体的业务功能,可能会调用其他模块完成复杂操作。
fbmq - vue:前端 Vue 模块,包含 Vue 组件、视图、路由等前端代码,用于构建用户界面。
fbmq - webflux:Spring WebFlux 模块,用于构建响应式 Web 应用,处理异步请求和事件驱动操作。
二、配置文件
配置文件如下:
spring.webflux 配置webFlux静态路由路径
server.post 可视化监控平台服务端口
fastbee.jwt 登录认证
fastbee.user 可视化平台登录默认账号$密码
fastbee.datasource mysql配置
fastbee.servers mqtt&webSocket启动配置
fastbee.cluster.localAddress 本地节点配置
fastbee.cluster.addresses 集群节点配置
fastbee.cluster.node-ip 节点名称
spring:
application:
name: fbmq
webflux:
static-path-pattern: /static/** #配置webflux的前端资源路径
profiles:
active: dev
server:
port: 18083
fastbee:
jwt: #jwt认证
secret: com.fastbee.jwt
expireTime: 14400000
user: #默认的登录账号密码,可修改
username: admin
password: admin123
datasource: # 配置信息存在的数据库,目前只支持mysql
url: jdbc:mysql://81.71.xx.xx:3306/fbmq?useSSL=false
username: root
password: 123456
servers:
- port: 1883
auth: true # 是否开启认证
- port: 8083
openws: true # 是否使用websocket
path: /mqtt # ws路径
auth: true
router: #线程池配置
batchSize: 200
logging: #日志级别配置,建议设置为 error
level:
root: info
com.fastbee: error
org.springframework.boot.autoconfigure: error
org.apache.kafka: error
org.apache.ignite: error
fastbee:
cluster: # 集群配置
localAddress: 127.0.0.1
addresses: [ "127.0.0.1" ] # 内网地址
node-id: fastbee # 节点id三、webFlux整合前端资源部署
整合前端
前端打包好的文件复制至SpringBoot启动类所在的 resources/static下
方式1
假设已经装好前端配置环境的情况下, 直接前端打包,在终端中 cd到 fbmq-vue项目,假设已经执行了pnpm install下载了前端依赖 ,再执行 pnpm build将前端项目打包
方式2 (推荐)
无需安装前端配置环境,在springBoot 父类maven中运行 maven clean 清除maven缓存,再执行 maven package 运行成功后,前端文件会自动复制至 SpringBoot启动类所在的 resources/static下
后端使用的是WebFlux,需要配置前端文件过滤和请求等
开启webFlux跨域过滤
@Configuration
@EnableWebFlux
public class WebFluxCorsConfig implements WebFluxConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") // 允许所有来源,实际应用中需精确配置具体的前端域名
.allowedMethods(HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.OPTIONS.name())
.allowedHeaders("*")
.allowCredentials(false);
}
}配置前端路由
@Configuration
@EnableWebFlux
public class WebFluxMvcConfig implements WebFluxConfigurer {
@Bean
public RouterFunction<ServerResponse> staticResourcesRouter() {
ClassPathResource classPathResource = new ClassPathResource("static/");
return resources("/**", classPathResource);
}
}重定向&启动页
@Configuration
public class WebFluxRedirectConfig {
@Bean
public WebFilter redirectFilter() {
return (ServerWebExchange exchange, WebFilterChain chain) -> {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
// 判断请求路径是否为根路径("/")
if ("/".equals(request.getPath().pathWithinApplication().value())) {
// 设置重定向的状态码为302(临时重定向,也可根据需求换为301等)
response.setStatusCode(HttpStatus.FOUND);
// 设置重定向的目标地址为 "/index.html"
response.getHeaders().setLocation(request.getURI().resolve("/index.html"));
return Mono.empty();
}
return chain.filter(exchange);
};
}
}
