环境准备

采用thingsboard-rrelease-3.2版本进行编译
jdk:openjdk11 (下载地址 http://jdk.java.net/archive/
nodejs:v16.13.0
yarn: 1.22.18
安装方式:

  1. npm install -g yarn --registry=https://registry.npm.taobao.org
  2. 配置源
  3. yarn config set registry https://registry.npm.taobao.org -g
  4. yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g

git: 不限制

编译环境步骤

下载thingsboard

通过idea下载thingsboard程序
github : https://github.com/thingsboard/thingsboard.git
gitee: https://gitee.com/mirrors/ThingsBoard.git

确认项目编译jdk版本

此处可用根据 根目录下pom.xml中的 maven-compiler-plugin 指定的jdk版本来调整自己本地编译环境的版本。
注: 3.0版本以及以前版本使用的为jdk1.8版本;3.0版本以后使用的为jdk11版本
1652259044(1).png
确认好jdk版本后,需要确认idea编译时jdk版本
1652259117(1).png
1652259150(1).png
1652259176(1).png
最好每个项目都确认下jdk的版本

取消license的验证

在根目录 pom.xml 中注释掉 license-maven-plugin 插件
1652259376(1).png

更换 frontend-maven-plugin 为 exec-maven-plugin

这里网上好多人都说是修改 frontend-maven-plugin 中配置的 nodeVersion ,yarnVersion为自己本地的nodejs 和 yarn版本一样就能可用,但是我本地修改后依然无法编译,并且windows会弹出
1652260017(1).png
对话框
原因:
由于在编译的时候 frontend-maven-plugin 会根据配置的nodeversion ,yarnversion版本去下载nodejs,,然后使用这个nodejs去编译 ui-ngx这个项目,但是它下载的nodejs都是32位的,而我操作系统是64位的,所以总是报错,这个我改变策略使用 exec-maven-plugin 插件,exec-maven-plugin可用指导maven在编译ui-ngx时使用本地已经安装的nodejs , yarn避免了以上问。

解决方式:
在ui-ngx 中的pom.xml 中首先注释掉 frontend-maven-plugin 插件;
注意:这里包括 build 和 profiles 两个节点中的 frontend-maven-plugin 配置
然后引入 exec-maven-plugin 插件

  1. <!--
  2. Copyright © 2016-2021 The Thingsboard Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. -->
  13. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  15. <modelVersion>4.0.0</modelVersion>
  16. <parent>
  17. <groupId>org.thingsboard</groupId>
  18. <version>3.2.2</version>
  19. <artifactId>thingsboard</artifactId>
  20. </parent>
  21. <groupId>org.thingsboard</groupId>
  22. <artifactId>ui-ngx</artifactId>
  23. <packaging>jar</packaging>
  24. <name>ThingsBoard Server UI</name>
  25. <url>https://thingsboard.io</url>
  26. <properties>
  27. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  28. <main.dir>${basedir}/..</main.dir>
  29. </properties>
  30. <build>
  31. <resources>
  32. <resource>
  33. <directory>${project.build.directory}/generated-resources</directory>
  34. </resource>
  35. </resources>
  36. <plugins>
  37. <!-- 修改使用 exec-maven-plugin 插件编译前端项目 start -->
  38. <plugin>
  39. <groupId>org.codehaus.mojo</groupId>
  40. <artifactId>exec-maven-plugin</artifactId>
  41. <version>1.6.0</version>
  42. <executions>
  43. <execution>
  44. <id>exec-npm-install</id>
  45. <phase>prepare-package</phase>
  46. <goals>
  47. <goal>exec</goal>
  48. </goals>
  49. <configuration>
  50. <executable>yarn</executable>
  51. <arguments>
  52. <argument>install</argument>
  53. </arguments>
  54. <workingDirectory>${basedir}</workingDirectory>
  55. </configuration>
  56. </execution>
  57. <execution>
  58. <id>exec-npm-run-build</id>
  59. <phase>prepare-package</phase>
  60. <goals>
  61. <goal>exec</goal>
  62. </goals>
  63. <configuration>
  64. <executable>yarn</executable>
  65. <arguments>
  66. <argument>run</argument>
  67. <argument>build:prod</argument>
  68. </arguments>
  69. <workingDirectory>${basedir}</workingDirectory>
  70. </configuration>
  71. </execution>
  72. <execution>
  73. <id>exec-npm-run-start</id>
  74. <phase>prepare-package</phase>
  75. <goals>
  76. <goal>exec</goal>
  77. </goals>
  78. <configuration>
  79. <executable>yarn</executable>
  80. <arguments>
  81. <argument>run</argument>
  82. <argument>start</argument>
  83. </arguments>
  84. <workingDirectory>${basedir}</workingDirectory>
  85. </configuration>
  86. </execution>
  87. </executions>
  88. </plugin>
  89. <!-- 修改使用 exec-maven-plugin 插件编译前端项目 end -->
  90. </plugins>
  91. </build>
  92. <profiles>
  93. <!-- 修改使用 exec-maven-plugin 插件编译前端项目 start -->
  94. <!--考虑到window 和linux环境 npm命令格式的问题,使用maven的profile实现动态指定命令-->
  95. <profile>
  96. <id>window</id>
  97. <properties>
  98. <npm>npm.cmd</npm>
  99. </properties>
  100. <activation>
  101. <activeByDefault>true</activeByDefault>
  102. </activation>
  103. </profile>
  104. <profile>
  105. <id>linux</id>
  106. <properties>
  107. <npm>npm</npm>
  108. </properties>
  109. </profile>
  110. <!-- 修改使用 exec-maven-plugin 插件编译前端项目 end -->
  111. </profiles>
  112. </project>

更换 github组件引用

由于在ui-ngx/package.json中有3个组件时要到github中去下载的,但是国内访问github不方便,因此需要将github上面的组件修改到 gitee上。
涉及的组件分别为:
flot : 修改后地址 https://gitee.com/freedom2015/flot.git
flot.curvedlines : 修改后地址 https://gitee.com/bingmaxx/CurvedLines.git
ngx-flowchart: 修改后地址 https://gitee.com/bingmaxx/ngx-flowchart.git
修改后,如图:
1652261047(1).png
这里对应的将 yarn.lock 文件中这三个组件的地址也修改掉

在maven的setting.xml中增加国内镜像地址

在maven的setting.xml中的mirrors 节点中增加如下内容

  1. <mirror>
  2. <id>mirrorId</id>
  3. <mirrorOf>repositoryId</mirrorOf>
  4. <name>Human Readable Name for this Mirror.</name>
  5. <url>http://my.repository.com/repo/path</url>
  6. </mirror>
  7. <mirror>
  8. <id>mirror</id>
  9. <mirrorOf>central,jcenter,!rdc-releases,!rdc-snapshots</mirrorOf>
  10. <name>mirror</name>
  11. <url>https://maven.aliyun.com/nexus/content/groups/public</url>
  12. </mirror>
  13. <mirror>
  14. <id>gradle-plugin</id>
  15. <mirrorOf>gradle-plugin</mirrorOf>
  16. <name>gradle-plugin</name>
  17. <url>https://maven.aliyun.com/repository/gradle-plugin</url>
  18. </mirror>

开始编译

这里由于我本地有多个jdk版本,因此我采用了在idea中配置编译指令的方式确保jdk版本
配置方式:
1652261432(1).png
配置后指令后在右侧maven栏中双击启动指令,即可开始编译
在编译窗口中开始会显示使用的jdk版本
1652261545(1).png
确认无误基本编译就能完成

在编译过程中在执行
“node —max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve —host 0.0.0.0 —open” 指令时可能会出现
[error] Error: spawn EPERM
。。。。。。

这样的错误这里需要删除掉ui-ngx目录中node_modules 这个目录然后重新开始编译,多搞几次 它就过了我也没搞明白什么问题

至此,编译完成后浏览器会自动开启登录界面
http://localhost:4200/login
1652262058(1).png
这样编译环境完成,后续就是要开始准备开发环境了

开发环境搭建

搭建kafka单机环境

搭建时
zookeeper 选择3.5.9 版本
kafka 选择2.8 版本
由于thingsboard中使用的 curator-recipes:4.2.0对zookeeper版本有要求

搭建postgresql数据库

安装timescal插件

使用postgres 12 版本

初始化数据库

修改数据库连接地址

修改thingsboard.yml配置文件中数据库连接地址,redis地址,kafka地址
复制初始化脚本
将dao/src/main/resources中的sql 目录复制到 application/src/main/data 目录中

启动application程序

启动程序 application/src/main/java/org/thingsboard/server/ThingsboardInstallApplication.java类
在执行该类时在启动参数中加入 install.load_demo=true 可在初始化完成后加载测试用数据
1652694715(1).png
检查数据库
数据库中已经出现表

启动application项目

启动程序application/src/main/java/org/thingsboard/server/ThingsboardServerApplication.java

参考自:
本地开发环境搭建: https://www.jianshu.com/p/8e8458699aa4