ThingsBoard——v3.3.3源码编译

Posted a10615

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThingsBoard——v3.3.3源码编译相关的知识,希望对你有一定的参考价值。

要二次开发,肯定少不了编译源码。编译的难点就是坑多,因为我们的环境不可能跟源码作者的一样,当然你的跟我的也可能不一样。我的环境:系统:macOS10.15.5,CPU i7

1 准备工作

1.1 编译环境

  • java版本:java 11
    $ java -version
    openjdk version "11.0.14.1" 2022-02-08 LTS
    OpenJDK Runtime Environment Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS)
    OpenJDK 64-Bit Server VM Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS, mixed mode)
    
  • node
    # 安装
    $ brew install node
    # 查看版本
    $ node -v
    
  • yarn
    # 全局安装yarn
    $ npm install -g yarn
    # 查看版本
    $ yarn -v
    
  • 安装IntelliJ IDEA 插件:Protobuf Support

1.2 数据库

直接使用docker容器来运行

1.2.1 启动PostgreSQL容器

$ docker run \\
    -d \\
    --name postgres \\
    -p 5432:5432 \\
    -e POSTGRES_USER=postgres \\
    -e POSTGRES_PASSWORD=postgres \\
    -v ~/docker/data/postgresql/data:/var/lib/postgresql/data \\
    postgres

1.2.2 数据库配置

主要创建数据库thingsboard,及配置pg_hba.conf

# 进入到docker中
$ docker exec -it postgres bash
# 切换到用户postgres
$ sudo su - postgres
# 进入数据库
$ psql
# 修改密码,密码为postgres
$ \\password
# 创建数据库thingsboard。查看数据库:\\l,切换数据库:\\c thingsboard
$ create database thingsboard;
# 退出
$ \\q

# Ctrl+D退出本用户,退到root用户
# 安装nano
$ apt-get update
$ apt-get install nano

$ nano /var/lib/postgresql/data/pg_hba.conf
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
==> 把trust改成md5
host    all             all             127.0.0.1/32            md5

# 退出后重启容器
$ docker restart posgres

2 下载源码编译

2.1 下载v3.3.3源码

$ git clone https://github.com/thingsboard/thingsboard.git
$ cd thingsboard
$ git checkout release-3.3

2.2 修改node和yarn版本

  • node:全局统一node版本(3个pom.xml)
    <nodeVersion>v16.13.0</nodeVersion>
    
  • yarn:全局统一yarn版本(3个pom.xml)
    <yarnVersion>v1.22.17</yarnVersion>
    

2.3 编译

$ mvn clean install -DskipTests -Ddockerfile.skip=false

编译成功,就能看到这漂亮的日志信息

3 创建数据表

3.1 拷贝数据表文件

dao/src/main/java/resources/sql整个目录,复制到application/src/main/data目录下

3.2 生成数据表

运行ThingsboardInstallApplication

4 启动

直接运行ThingsboardServerApplication
或通过jar包运行:

$ java -jar ./application/target/thingsboard-3.3.3-boot.jar

登录后台:http://localhost:8080
账号:sysadmin@thingsboard.org,密码:sysadmin

5 问题

5.1 编译报错:yarn install

[INFO] --- frontend-maven-plugin:1.12.0:yarn (yarn install) @ ui-ngx ---
[INFO] Running 'yarn install' in /Users/projects/thingsboard/ui-ngx
[INFO] yarn install v1.22.17
[INFO] [1/4] Resolving packages...
[INFO] [2/4] Fetching packages...
[INFO] error @geoman-io/leaflet-geoman-free@2.11.4: The engine "node" is incompatible with this module. Expected version ">=16.13 <17". Got "17.6.0"
[INFO] error Found incompatible module.
[INFO] info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Thingsboard 3.3.3:
...
[INFO] 
[INFO] Thingsboard ........................................ SUCCESS [ 15.752 s]
...
[INFO] Thingsboard SNMP Transport Service ................. SUCCESS [ 22.710 s]
[INFO] ThingsBoard Server UI .............................. FAILURE [  8.218 s]
[INFO] Thingsboard Server Tools ........................... SKIPPED
...

因为把nodeVersion改成了<nodeVersion>v17.6.0</nodeVersion>,而这里提示版本范围必须为:Expected version ">=16.13 <17",所以要把node版本降级到v16.13.0

5.2 编译报错:yarn run pkg

[INFO] --- frontend-maven-plugin:1.12.0:yarn (yarn pkg) @ js-executor ---
[INFO] Running 'yarn run pkg' in /Users/projects/thingsboard/msa/js-executor
[INFO] yarn run v1.22.17
[INFO] $ pkg -t node12-linux-x64,node12-win-x64 --out-path ./target . && node install.js
[INFO] > pkg@5.3.1
[INFO] > Fetching base Node.js binaries to PKG_CACHE_PATH
[INFO] node:internal/process/promises:246
[INFO]           triggerUncaughtException(err, true /* fromPromise */);
[INFO]           ^
[INFO] 
[INFO] [Error: ENOENT: no such file or directory, lstat '/Users/projects/thingsboard/msa/js-executor/target/thingsboard-js-executor-linux'] 
[INFO]   errno: -2,
[INFO]   code: 'ENOENT',
[INFO]   syscall: 'lstat',
[INFO]   path: '/Users/projects/thingsboard/msa/js-executor/target/thingsboard-js-executor-linux'
[INFO] 
[INFO] error Command failed with exit code 1.
[INFO] info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Thingsboard 3.3.3:
[INFO] 
[INFO] Thingsboard ........................................ SUCCESS [ 12.463 s]
...
[INFO] ThingsBoard Docker Images .......................... SUCCESS [02:44 min]
[INFO] ThingsBoard javascript Executor Microservice ....... FAILURE [14:26 min]
[INFO] ThingsBoard Web UI Microservice .................... SKIPPED
...

按这些方法一个一个尝试吧,也许有个能解决:

  • 删除~/.pkg-cache/v3.2,参考issues
  • 删除ui-ngx/node_modules,参考issues
  • 保持pom.xml和本地的node版本一致,参考issues
  • thingsboard/msa/web-ui/package.jsonthingsboard/msa/js-executor/package.json里的node12-linux-x64,node12-win-x64改成node14-linux-x64,node14-win-x64,参考issues

5.3 执行ThingsboardInstallApplication报错

thingsboard/common/message/src/main/java/org/thingsboard/server/common/msg/TbMsg.java:32:45
java: 程序包org.thingsboard.server.common.msg.gen不存在

先确认是否安装了插件Protobuf Support,如果安装了,编译时就会自动生成一些gen代码。把生成的代码common/message/target/generated-sources设置为源代码的生成目录:右键 —— Mark Directory as —— Generated Sources Root

还有些module下也需要设置,build的时候检查下

5.4 启动报错:admin_settings does not exist

Caused by: org.postgresql.util.PSQLException: ERROR: relation "admin_settings" does not exist
  位置:152

未先执行ThingsboardInstallApplication

5.5 启动报错:7070端口绑定失败

ERROR 21302 --- [           main] o.t.s.service.edge.rpc.EdgeGrpcService   : Failed to start Edge RPC server!

java.io.IOException: Failed to bind to address 0.0.0.0/0.0.0.0:7070
...
Caused by: java.lang.RuntimeException: Failed to start Edge RPC server!

查看7070端口是否被其它占用

$ lsof -i:7070
COMMAND    PID  USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
com.docke 5956 zjun  119u  IPv6 0x7a76609836e40ab      0t0  TCP *:arcp (LISTEN)

这里被docker中的mytb给占用了

$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED        STATUS                PORTS                                                                                                      NAMES
427bf1c7924e   1e6b071a2deb             "start-tb.sh"            4 days ago     Up 5 hours            0.0.0.0:1883->1883/tcp, 0.0.0.0:7070->7070/tcp, 0.0.0.0:9090->9090/tcp, 0.0.0.0:5683-5688->5683-5688/udp   mytb

5.6 启动报错:ExceptionInInitializerError

ERROR 21949 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rpcV1Controller': Unsatisfied dependency expressed through field 'deviceRpcService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultTbCoreDeviceRpcService' defined in URL [jar:file:/Users/projects/thingsboard/application/target/thingsboard-3.3.3-boot.jar!/BOOT-INF/classes!/org/thingsboard/server/service/rpc/DefaultTbCoreDeviceRpcService.class]: Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actorSystemContext': Unsatisfied dependency expressed through field 'jsSandbox'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nashornJsInvokeService': Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.2.16.RELEASE.jar!/:5.2.16.RELEASE]
	...
	Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultTbCoreDeviceRpcService' defined in URL [jar:file:/Users/projects/thingsboard/application/target/thingsboard-3.3.3-boot.jar!/BOOT-INF/classes!/org/thingsboard/server/service/rpc/DefaultTbCoreDeviceRpcService.class]: Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actorSystemContext': Unsatisfied dependency expressed through field 'jsSandbox'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nashornJsInvokeService': Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
	...
	Caused by: java.lang.RuntimeException: Neither JDK nor Standalone Nashorn was found. If running on JDK 15 or later, you must add standalone Nashorn to the classpath.
	at delight.nashornsandbox.internal.NashornDetection.<clinit>(NashornDetection.java:40) ~[delight-nashorn-sandbox-0.2.1.jar!/:na]

添加这个也不行

<dependency>
  <groupId>org.openjdk.nashorn</groupId>
  <artifactId>nashorn-core</artifactId>
  <version>15.0</version>
</dependency>

只是不报这句错误:If running on JDK 15 or later, you must add standalone Nashorn to the classpath

需要把JDK更换成11

  • 检查是否安装JDK 11版本。安装了就会打印安装目录
    $ /usr/libexec/java_home -v11
    /Users/zjun/Library/Java/JavaVirtualMachines/corretto-11.0.14.1/Contents/Home
    
  • 如果没有安装,可以通过idea来安装:Project Structure —— Platform Settings —— SDKs
  • 创建多个版本的java环境配置
    $ vi ~/.zshrc 
    
    export JAVA_15_HOME=$(/usr/libexec/java_home -v 15)
    export JAVA_11_HOME=$(/usr/libexec/java_home -v11) 
    export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) 
    
    alias java8='export JAVA_HOME=$JAVA_8_HOME'
    alias java11='export JAVA_HOME=$JAVA_11_HOME'
    alias java15='export JAVA_HOME=$JAVA_15_HOME'
    
    $ source ~/.zshrc 
    # 切换版本
    $ java11
    

6 参考

以上是关于ThingsBoard——v3.3.3源码编译的主要内容,如果未能解决你的问题,请参考以下文章

ThingsBoard——v3.3.3源码编译

物联网-thingsboard源码编译安装踩坑记录

ThingsBoard——自定义规则节点

ThingsBoard——自定义规则节点

ThingsBoard——自定义规则节点

Thingsboard源码分析-MQTT设备连接协议(下)