BladeX开发入门(记录)

BladeX 物联网平台是一款高度集成的物联网解决方案,涵盖设备管理、数据采集、实时监控、数据分析以及开放API服务等核心功能。

平台经过精心设计与开发,提供了全面的品类、产品和设备支持。设备注册成功后,能够轻松桥接至其他物联网云平台,实现设备的无缝集成。

同时提供服务端订阅功能,支持MQTT与AMQP两者方式将设备数据订阅转发至自建服务端,实现设备数据的自定义监控与分析。

官方环境要求

基础开发环境

  • Java: 17
  • NodeJs: 18
  • Maven: 3.6+
  • MySql: 5.7+
  • Redis: 5.0+
  • InfluxDB: 1.8.x+

IDE插件

  • Lombok Plugin (必装)
  • MybatisX Plugin (idea选装)

推荐IDE

  • 后端使用 IntelliJ IDEA
  • 前端使用 IntelliJ WebStorm

本机测试环境

操作系统:Windows 10

IDE使用:IntelliJ IDEA

前端使用:VSCODE

一、环境准备

1 下载安装JDK17

下载openjdk17,前往下载地址找到符合自己机器的版本并安装配置环境变量,三种推荐版本选其一或公司选型的Jdk均可

 选择Azul Zulu,找到windows版本进行下载:

下载zip版本,加压缩,验证版本信息:

定位到<installation_folder>\bin目录,执行:java -version

 (可选)添加<installation_folder>\bin到您的PATH系统变量,以便您可以java从任何目录执行而无需指定完整路径:

我的电脑->属性->高级系统设置->环境变量->系统变量->Path->编辑

在打开的对话框中,添加bin文件夹路径(Azul Zulu 安装文件.zip文件解压到的目录)。

注意:确保bin文件夹位于列表顶部(避免多个jdk版本导致不生效)

 

 

2 安装Maven

由于电脑已经安装3.9.4版本,略过:

 

3 NodeJs

由于电脑已经安装18.16.0,略过:

 

4 安装MySQL(用docker可不安装)

(1) 下载

下载地址:https://dev.mysql.com/downloads/mysql/

默认是最新版本,可以选择下载msi或者zip:

 由于环境要求是5.7+,最后选择5.7版本的最新版本(根据网上的建议)

切换到档案Archives,选择5.7.44版本:

解压缩到目录下(my.ini是新建的,详见下文)

 (2) 配置

配置环境变量:将MySQL安装路径下的bin目录配置到Path变量中

Path添加:D:\Program Files\mysql\mysql-5.7.44-winx64\bin

在【安装目录】内,新建 my.ini 空文件,添加以下内容(替换【安装目录】):

[mysql]  
# 设置 mysql 客户端默认字符集  
default-character-set=utf8  

[mysqld]  
#设置 3306 端口  
port = 3306  

# 设置 mysql 的安装目录  
basedir= 【安装目录】

# 设置 mysql 数据库的数据的存放目录  
datadir= 【安装目录】\data  

# 允许最大连接数  
max_connections=200  

# 服务端使用的字符集默认为 8 比特编码的 latin1 字符集  
character-set-server=utf8  

# 创建新表时将使用的默认存储引擎  
default-storage-engine=INNODB

如下:

注意:我在windows server 2016安装mysql时,将my.ini拷贝到服务器上,修改路径后运行报错:

 原因:文件编码问题,Windows Server会比Windows更为严格,一般注意编码问题,检查是否有多余的字符或者隐藏的字符,通常是多余的空格可能会造成影响,或者检查路径,尝试使用/

通过排查,将文件编码从UTF-8转换成ANSI编码,发现每行末尾都添加了几个空格,将这几个空格去掉,再次运行解决了问题

(3) 安装

以管理员身份运行cmd,进入到MySQL的bin目录,执行初始化命令:

mysqld --initialize --user=mysql --console

 该命令用来初始化数据,在5.7以前的版本是不需要执行该命令的。初始化完成后会提供一个临时的root密码,如下图红色方框,记下该密码:0uNDD&/lq<Tr

再执行如下命令进行MySQL服务安装:

mysqld –install mysql

mysql为默认的服务名,可不写,若安装成功则有如下提示:

 需要注意的是一定要以管理员身份运行cmd,否则会出现如下图所示提示:

(4) 启动

 启动服务

net start mysql

 当出现如下图所示提示时表示MySQL安装并启动成功。

MySQL5.7以前的版本在安装时不需要执行第四步中的mysqld –initialize命令,

5.7版本如果漏掉该命令后在此步骤会出现如下错误:

 因为该版本的压缩包里面有些文件和文件夹(如data文件夹)需要在执行mysqld -initialize命令后才创建。

(5) 登录

mysql -u root -p

提示输入密码,输入前面安装的时候记录下的密码,按回车后出现如下页面表示登录成功,并进入了MySQL命令行模式。

(6) 修改密码

ALTER USER ‘root’@’localhost’  IDENTIFIED BY ‘new_password’

 修改成功会提示:Query OK, 0 rows affected (0.00 sec)

注意:sql命令要以分号结束,如果第一次没有结束,第二行再次执行还是会报错,分号结束后再执行就正常了,如上图所示。

如果没有修改密码就执行其它的SQL命令,则会提示如下错误:

(7) 操作数据库

安装和启动已经完成后,可输入下列语句,显示已有数据库:

show databases;

 退出命令行模式,可输入以下任一一个命令:

exit
quit
\q

停止数据库服务,输入:

net stop mysql

 

5 Redis(用docker可不安装)

参考文档:https://blog.csdn.net/ljj20161221/article/details/135134670

Window下Redis的安装和部署详细图文教程(包含Redis Desktop Manage安装和使用):https://www.jb51.net/database/320870vp2.htm

 

6 安装Docker

使用docker挂在mysql、redis、influxdb,宿主机器可无需安装mysql、redis

下载地址:https://docs.docker.com/desktop/install/windows-install/

下载完后按照默认安装好,修改 docker desktop 配置

 1)开启WSL 2运行Docker,正常是默认开启的

 2)修改资源存储目录:

 3)配置国内镜像源,可以添加多个,json格式

"registry-mirrors": [
    "https://mirror.ccs.tencentyun.com",
    "http://hub-mirror.c.163.com",
    "https://registry.docker-cn.com"
  ]

 具体可参考:https://blog.csdn.net/mst_sun/article/details/135087651

二、Bladex物联网平台工程运行

1、中间件运行

根据需要先创建挂载目录:

 注:以下命令在命令行中执行,为避免出现其他问题,建议以管理员身份运行

docker启动mysql并挂载文件(关闭大小写敏感)

docker run ^
  --name mysql -d -p 13306:3306 ^
  -v D:\develop\docker\data\mysql\data:/var/lib/mysql ^
  -v D:\develop\docker\data\mysql\conf:/etc/mysql/conf.d ^
  -e MYSQL_ROOT_PASSWORD=mysql123456 ^
  --privileged=true ^
  mysql:5.7.44 ^
  --lower_case_table_names=1

  注意:

  • 运行前先创建好目录:D:\develop\docker\data\mysql\data和D:\develop\docker\data\mysql\conf
  • 因本机安装了mysql,端口3306被占用,改用了13306
  • Windows下换行用^;Linux底下换行用\

创建数据库并导入sql:

  • 服务启动成功后打开数据库,创建数据库名bladex_iot(字符集采用utf8mb4)
  • 导入BladeX-Links工程根目录下/doc/sql文件夹的bladex.mysql.iot.create.sql文件与bladex.mysql.sys.create.sql

  以navicat为例,新建完后右键运行SQL文件,然后找到指定文件运行即可。  

  

docker启动redis(注意生产环境必须关闭端口映射并且设置密码)

docker run --name redis -d -p 6379:6379 -v D:\develop\docker\data\redis\data:/data redis:7.2.4

  注:开发环境就没设置密码了

docker启动influxdb并挂载数据

docker run -d --name influxdb ^
  -p 8086:8086 ^
  -v D:\develop\docker\data\influxdb:/var/lib/influxdb ^
  -e INFLUXDB_DB=links ^
  -e INFLUXDB_HTTP_AUTH_ENABLED=true ^
  -e INFLUXDB_ADMIN_USER=admin ^
  -e INFLUXDB_ADMIN_PASSWORD=links ^
  influxdb:1.8.10

可以在docker中看到启动情况:

2、 后端运行

(1)获取BladeX-Links代码,使用IDEA打开工程,点击maven刷新按钮下载依赖

(2)修改配置前先获取秘钥:

bladex-links\blade-server\src\test\java\org\springblade\test\Sm2KeyGenerator.java

bladex-links\blade-server\src\test\java\org\springblade\test\SignKeyGenerator.java

 右键运行:

 在输出中可以看到public-key和private-key

 sign-key和aes-key:

(3)修改BladeX-Links工程下的blade-server配置

修改application.yml配置文件,修改密钥保持与blade-broker一致

bladex-links\blade-server\src\main\resources\application.yml:

blade-server的端口号默认是80,根据实际情况选择是否修改(测试因80端口被占用,改用8099端口):

 

修改application-dev.yml配置文件,修改数据库、redis、influxdb、minio等配置

bladex-links\blade-server\src\main\resources\application-dev.yml:端口密码等配置信息根据自己安装时候的实际情况修改

 

(4)修改BladeX-Links工程下的blade-broker配置

修改application.yml配置文件,修改密钥保持与blade-server一致

bladex-links\blade-broker\src\main\resources\application.yml:

修改application-dev.yml配置文件,修改数据库、redis、influxdb、minio等配置

bladex-links\blade-broker\src\main\resources\application-dev.yml:

(5)启动服务端

启动BladeX-Links工程下的blade-broker模块的BrokerApplication启动类,看到如下日志,则说明启动成功

   ____  _           _     __   __    _     _       _
  |  _ \| |         | |    \ \ / /   | |   (_)     | |
  | |_) | | __ _  __| | ___ \ V /    | |    _ _ __ | | _____
  |  _ <| |/ _` |/ _` |/ _ \ > <     | |   | | '_ \| |/ / __|
  | |_) | | (_| | (_| |  __// . \    | |___| | | | |   <\__ \
  |____/|_|\__,_|\__,_|\___/_/ \_\   |_____|_|_| |_|_|\_\___/


:: SpringBlade :: blade-broker:dev :: Running SpringBoot 3.2.4 :: 

2024-06-03 00:26:26.238  INFO 21536 --- [kground-preinit] o.h.validator.internal.util.Version      : HV000001: Hibernate Validator 8.0.1.Final
2024-06-03 00:26:26.265  INFO 21536 --- [           main] o.s.mqtt.broker.BrokerApplication        : Starting BrokerApplication using Java 17.0.10 with PID 21536 (/Users/chill/Workspaces/product/BladeX-IOT/blade-broker/target/classes started by chill in /Users/chill/Workspaces/product/BladeX-IOT)
2024-06-03 00:26:26.265 DEBUG 21536 --- [           main] o.s.mqtt.broker.BrokerApplication        : Running with Spring Boot v3.2.4, Spring v6.1.5
2024-06-03 00:26:26.266  INFO 21536 --- [           main] o.s.mqtt.broker.BrokerApplication        : The following 1 profile is active: "dev"
2024-06-03 00:26:26.887  INFO 21536 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2024-06-03 00:26:26.888  INFO 21536 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-06-03 00:26:26.904  INFO 21536 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 Redis repository interfaces.
2024-06-03 00:26:26.956  WARN 21536 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'mqttAccountMapper' and 'org.springblade.mqtt.broker.auth.mapper.MqttAccountMapper' mapperInterface. Bean already defined with the same name!
2024-06-03 00:26:26.956  WARN 21536 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'mqttAuthLogMapper' and 'org.springblade.mqtt.broker.auth.mapper.MqttAuthLogMapper' mapperInterface. Bean already defined with the same name!
2024-06-03 00:26:26.957  WARN 21536 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'deviceMapper' and 'org.springblade.mqtt.broker.device.mapper.DeviceMapper' mapperInterface. Bean already defined with the same name!
2024-06-03 00:26:26.998  INFO 21536 --- [           main] s.c.l.p.BladePropertySourcePostProcessor : BladePropertySourcePostProcessor init.
2024-06-03 00:26:27.021  INFO 21536 --- [           main] s.c.l.p.BladePropertySourcePostProcessor : BladePropertySourcePostProcessor process @BladePropertySource bean.
2024-06-03 00:26:27.336  WARN 21536 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2024-06-03 00:26:27.343  INFO 21536 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2024-06-03 00:26:27.343  INFO 21536 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1057 ms
2024-06-03 00:26:27.503  INFO 21536 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2024-06-03 00:26:27.541  INFO 21536 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2024-06-03 00:26:28.647  INFO 21536 --- [           main] n.d.i.m.core.server.MqttServerCreator    : Mica mqtt http api enable:false websocket enable:true
2024-06-03 00:26:29.059  INFO 21536 --- [           main] o.s.core.boot.config.RetryConfiguration  : configServerRetryInterceptor: Changing backOffOptions to initial: 1000, multiplier: 1.2, maxInterval: 5000
2024-06-03 00:26:29.072  INFO 21536 --- [           main] o.s.core.log.logger.BladeLogger          : blade-broker: BladeLogger init success!
2024-06-03 00:26:29.459  INFO 21536 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2024-06-03 00:26:29.633  INFO 21536 --- [           main] io.undertow                              : starting server: Undertow - 2.3.12.Final
2024-06-03 00:26:29.636  INFO 21536 --- [           main] org.xnio                                 : XNIO version 3.8.8.Final
2024-06-03 00:26:29.640  INFO 21536 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.8.Final
2024-06-03 00:26:29.651  INFO 21536 --- [           main] org.jboss.threads                        : JBoss Threads version 3.5.0.Final
2024-06-03 00:26:29.668  INFO 21536 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port 8888 (http)
2024-06-03 00:26:29.670  INFO 21536 --- [       Thread-1] org.tio.utils.timer.TimingWheelThread    : Starting
2024-06-03 00:26:29.670  INFO 21536 --- [sync-executor-1] o.s.core.launch.StartEventListener       : ---[BLADE-BROKER]---启动完成,当前使用的端口:[8888],环境变量:[dev]---
2024-06-03 00:26:29.672  INFO 21536 --- [           main] org.tio.server.TioServer                 : 
|----------------------------------------------------------------------------------------|
| TioConfig name    | BladeX-Mqtt-Server                                                 |
| Mica net version  | 1.0.0                                                              |
| Started at        | 2024-06-03 00:26:29                                                |
| Listen on         | 0.0.0.0:1883                                                       |
| Main Class        | org.springblade.mqtt.broker.BrokerApplication                      |
| Jvm start time    | 4318ms                                                             |
| Tio start time    | 2ms                                                                |
| Pid               | 21536                                                              |
|----------------------------------------------------------------------------------------|

2024-06-03 00:26:29.673  INFO 21536 --- [           main] org.tio.server.TioServer                 : 
|----------------------------------------------------------------------------------------|
| TioConfig name    | BladeX-Mqtt-Server-HTTP/Websocket                                  |
| Mica net version  | 1.0.0                                                              |
| Started at        | 2024-06-03 00:26:29                                                |
| Listen on         | 0.0.0.0:8083                                                       |
| Main Class        | org.springblade.mqtt.broker.BrokerApplication                      |
| Jvm start time    | 4319ms                                                             |
| Tio start time    | 1ms                                                                |
| Pid               | 21536                                                              |
|----------------------------------------------------------------------------------------|

2024-06-03 00:26:29.684  INFO 21536 --- [           main] o.s.mqtt.broker.BrokerApplication        : Started BrokerApplication in 3.669 seconds (process running for 4.348)
2024-06-03 00:26:29.957  INFO 21536 --- [-192.168.31.254] io.undertow.servlet                      : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-03 00:26:29.958  INFO 21536 --- [-192.168.31.254] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-03 00:26:29.958  INFO 21536 --- [-192.168.31.254] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

 

启动BladeX-Links工程下的blade-server模块的ServerApplication启动类 ,看到如下日志,则说明启动成功

   ____  _           _     __   __    _     _       _
  |  _ \| |         | |    \ \ / /   | |   (_)     | |
  | |_) | | __ _  __| | ___ \ V /    | |    _ _ __ | | _____
  |  _ <| |/ _` |/ _` |/ _ \ > <     | |   | | '_ \| |/ / __|
  | |_) | | (_| | (_| |  __// . \    | |___| | | | |   <\__ \
  |____/|_|\__,_|\__,_|\___/_/ \_\   |_____|_|_| |_|_|\_\___/


:: SpringBlade :: blade-server:dev :: Running SpringBoot 3.2.4 :: 

2024-06-03 00:26:26.462  INFO 21541 --- [kground-preinit] o.h.validator.internal.util.Version      : HV000001: Hibernate Validator 8.0.1.Final
2024-06-03 00:26:26.493  INFO 21541 --- [           main] org.springblade.ServerApplication        : Starting ServerApplication using Java 17.0.10 with PID 21541 (/Users/chill/Workspaces/product/BladeX-IOT/blade-server/target/classes started by chill in /Users/chill/Workspaces/product/BladeX-IOT)
2024-06-03 00:26:26.493 DEBUG 21541 --- [           main] org.springblade.ServerApplication        : Running with Spring Boot v3.2.4, Spring v6.1.5
2024-06-03 00:26:26.493  INFO 21541 --- [           main] org.springblade.ServerApplication        : The following 1 profile is active: "dev"
2024-06-03 00:26:27.153  INFO 21541 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2024-06-03 00:26:27.154  INFO 21541 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-06-03 00:26:27.192  INFO 21541 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 32 ms. Found 0 Redis repository interfaces.
2024-06-03 00:26:27.237  WARN 21541 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[org.springblade]' package. Please check your configuration.
2024-06-03 00:26:27.298  INFO 21541 --- [           main] s.c.l.p.BladePropertySourcePostProcessor : BladePropertySourcePostProcessor init.
2024-06-03 00:26:27.320  INFO 21541 --- [           main] s.c.l.p.BladePropertySourcePostProcessor : BladePropertySourcePostProcessor process @BladePropertySource bean.
2024-06-03 00:26:27.637  WARN 21541 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2024-06-03 00:26:27.643  INFO 21541 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2024-06-03 00:26:27.643  INFO 21541 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1133 ms
2024-06-03 00:26:27.813  INFO 21541 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2024-06-03 00:26:27.847  INFO 21541 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2024-06-03 00:26:30.954  INFO 21541 --- [           main] o.s.core.boot.config.RetryConfiguration  : configServerRetryInterceptor: Changing backOffOptions to initial: 1000, multiplier: 1.2, maxInterval: 5000
2024-06-03 00:26:30.962  INFO 21541 --- [           main] o.s.core.log.logger.BladeLogger          : blade-server: BladeLogger init success!
2024-06-03 00:26:31.395  INFO 21541 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2024-06-03 00:26:31.866  INFO 21541 --- [           main] io.undertow                              : starting server: Undertow - 2.3.12.Final
2024-06-03 00:26:31.869  INFO 21541 --- [           main] org.xnio                                 : XNIO version 3.8.8.Final
2024-06-03 00:26:31.872  INFO 21541 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.8.Final
2024-06-03 00:26:31.882  INFO 21541 --- [           main] org.jboss.threads                        : JBoss Threads version 3.5.0.Final
2024-06-03 00:26:31.899  INFO 21541 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port 80 (http)
2024-06-03 00:26:31.901  INFO 21541 --- [sync-executor-1] o.s.core.launch.StartEventListener       : ---[BLADE-SERVER]---启动完成,当前使用的端口:[80],环境变量:[dev]---
2024-06-03 00:26:31.910  INFO 21541 --- [           main] org.springblade.ServerApplication        : Started ServerApplication in 5.638 seconds (process running for 5.923)
2024-06-03 00:26:32.271  INFO 21541 --- [-192.168.31.254] io.undertow.servlet                      : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-03 00:26:32.271  INFO 21541 --- [-192.168.31.254] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-03 00:26:32.273  INFO 21541 --- [-192.168.31.254] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms

 

注意: 

  • 由于采用Java17,Java 模块化系统(Java Module System)的安全限制导致的针对反射等场景有可能会出现如下错误:
    • Cause: java.lang.reflect.InaccessibleObjectException: Unable to make field protected java.lang.reflect.InvocationHandler java.lang.reflect.Proxy.h accessible: module java.base does not "opens java.lang.reflect" to unnamed module @223aa2f7
    • 解决方案:在启动参数中添加--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED

       

3、 前端运行

获取前端代码bladex-links-ui,根据自己的习惯选择IDE打开(本人使用VSCODE)。

修改website.js配置文件,bladex-links-ui\src\config\website.js(需要和后端保持一致):

blade-server默认为80端口,如果不是80端口,请修改vite.config.mjs中的proxy配置:

 然后再终端执行启动web端:

npm install
npm run dev

 

最终运行成功页面:

默认用户密码登录后:

 

<本文完>

 

posted @ 2024-09-26 15:58  孤草之魂  阅读(250)  评论(0编辑  收藏  举报