4.单例Quarkus应用连接Hono AMQP
将消息发送到 Hono 的 AMQP 适配器的协议网关服务
Hono用的架构是quarkus + vert.x , 我们想基于Hono的example去自定义gateway最好也使用该框架
构建一个Quarkus应用
- 可以使用Maven命令快速创建一个Quarkus应用,也可以自行搭建
mvn "io.quarkus:quarkus-maven-plugin:create" \
-DprojectGroupId="com.banff" \
-DprojectArtifactId="Hono-Gateway" \
-DprojectVersion="1.0-SNAPSHOT" \
-DclassName="HonoResource" \
-Dpath="actions"
注意 : 用的JDK版本是17
-
构建完成之后 ,观察一下项目结构 , 有docker目录,里面有几个Dockerfile文件应该是用来制作镜像
-
看
dependencyManagement
中quarkus使用了BOM管理, 可以方便的管理依赖-
<dependencyManagement> <dependencies> <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>${quarkus.platform.artifact-id}</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
-
还有quarkus的maven插件, 插件中貌似有很多功能
-
<plugin> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus.platform.version}</version> <extensions>true</extensions> <executions> <execution> <goals> <goal>build</goal> <goal>generate-code</goal> <goal>generate-code-tests</goal> </goals> </execution> </executions> </plugin>
-
启动服务(dev模式)
# dev模式可以看到更多内部信息,适合开发阶段使用,执行以下命令即可dev模式启动服务:
mvn compile quarkus:dev
# 控制台输出以下信息,表示服务已启动(还列出了已支持的feature),还问你要不要继续做单元测试:
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ Hono-Gateway ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Idea_project\Hono-Gateway\target\classes
[INFO]
[INFO] --- quarkus-maven-plugin:2.13.1.Final:dev (default-cli) @ Hono-Gateway ---
[INFO] Invoking org.apache.maven.plugins:maven-resources-plugin:2.6:testResources @ Hono-Gateway
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-10-12 18:12:47,871 INFO [io.quarkus] (Quarkus Main Thread) Hono-Gateway 1.0-SNAPSHOT on JVM (powered by Quarkus 2.13.1.Final) started in 2.292s. Listening on: http://localhost:8080
2022-10-12 18:12:47,885 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2022-10-12 18:12:47,886 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy-reactive, smallrye-context-propagation, vertx]
--
Tests paused
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>
浏览器访问
http://localhost:8080/
: 项目概括
http://localhost:8080/actions
: 自带的一个RESTful Web Services Demo
http://localhost:8080/q/dev/
: 也很多系统信息,该地址只在dev模式有效
生成jar包
刚才是启动开发环境服务命令 , 正式部署项目则是打成jar包运行
在application.properties
文件中添加 quarkus.package.type=uber-jar
# 终端输入
mvn clean package
uber-jar 指含依赖库的jar , 因为仅仅将HonoResource.java制作成jar是不够的,我们需要将所有HonoResource.java编译后与其他依赖jar合并在一起
PS D:\Idea_project\Hono-Gateway\target> java -jar .\Hono-Gateway-1.0-SNAPSHOT-runner.jar
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-10-12 19:17:13,921 INFO [io.quarkus] (main) Hono-Gateway 1.0-SNAPSHOT on JVM (powered by Quarkus 2.13.1.Final) started in 0.964s. Listening on: http://0.0.0.0:8080
2022-10-12 19:17:13,943 INFO [io.quarkus] (main) Profile prod activated.
2022-10-12 19:17:13,943 INFO [io.quarkus] (main) Installed features: [cdi, resteasy-reactive, smallrye-context-propagation, vertx]
制作二进制可执行文件
通过执行maven命令将项目制作成可执行文件, 确保docker运行正常 , quarkus的maven插件会下载一个docker镜像, 该镜像会创建一个容器,容器中有制作二进制可执行文件的环境 , 制作完成后 , 容器自动被销毁
在application.properties
文件中添加 quarkus.native.container-build=true
mvn package -Pnative
# quarkus的maven插件会下载一个docker镜像(ubi-quarkus-native-image)
Produced artifacts:
/project/Hono-Gateway-1.0-SNAPSHOT-runner (executable)
/project/Hono-Gateway-1.0-SNAPSHOT-runner.build_artifacts.txt (txt)
========================================================================================================================
Finished generating 'Hono-Gateway-1.0-SNAPSHOT-runner' in 1m 16s.
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildRunner] docker run --env LANG=C --rm -v /d/Idea_project/Hono-Gateway/target/Hono-Gateway-1.0-SNAPSHOT-native-image-source-jar:/project:z --entrypoint /bin/bash quay.io/quarkus/ubi-quarkus-native-image:22.2-java17 -c objcopy --strip-debug Hono-Gateway-1.0-SNAPSHOT-runner
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 209131ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:46 min
[INFO] Finished at: 2022-10-13T12:16:33+08:00
[INFO] ------------------------------------------------------------------------
PS D:\Idea_project\Hono-Gateway>
- 用WSL 执行以下这个文件 0.238s. 启动 , 很快
- 访问接口 , 正常
制作镜像
quarkus已经把Dockerfile都为我们准备好了
docker build \
-f src/main/docker/Dockerfile.native \
-t banfftech/hono-gateway:0.0.1 .
启动镜像
docker run -i --rm -p 8080:8080 banfftech/hono-gateway:0.0.1
接口请求一样正常
连接Hono
Quarkus的体验先到这里 , 现在开始基于这个模板项目和Hono进行交互
连接Sandbox
-
配置application.yml 内容
-
quarkus: http: port: 8081 package: type: uber-jar # jar类型 uber-jar值包括依赖的jar包 native: container-build: true # 支持了构建容器进行部署 vertx: prefer-native-transport: true # 启用本地传输 warning-exception-time: "PT1.5S" # 如果事件循环被阻止,则显示警告之前的时间量。 resolver: cache-max-time-to-live: 0 # 成功解析的地址将被缓存的最长时间(秒) log: console: color: true level: INFO min-level: TRACE category: "io.quarkus.vertx.core.runtime": level: DEBUG # 网关配置 gateway: tcp: insecurePort: 6666 insecurePortBindAddress: "127.0.0.1" amqp: host: hono.eclipseprojects.io port: 5672 username: gw@DEFAULT_TENANT password: gw-secret serverRole: AMQP adapter
-
-
配置pom依赖
-
<dependency> <groupId>org.eclipse.hono</groupId> <artifactId>hono-legal</artifactId> <version>${hono.version}</version> </dependency> <dependency> <groupId>org.eclipse.hono</groupId> <artifactId>hono-client-command</artifactId> <version>${hono.version}</version> </dependency> <dependency> <groupId>org.eclipse.hono</groupId> <artifactId>hono-client-common</artifactId> <version>${hono.version}</version> </dependency> <dependency> <groupId>org.eclipse.hono</groupId> <artifactId>hono-client-amqp-connection</artifactId> <version>${hono.version}</version> </dependency> <dependency> <groupId>org.eclipse.hono</groupId> <artifactId>hono-client-amqp-common</artifactId> <version>${hono.version}</version> </dependency> <dependency> <groupId>org.eclipse.hono</groupId> <artifactId>hono-client-device-amqp</artifactId> <version>2.2.0</version> <scope>system</scope> <systemPath>D:/Idea_project/Hono-Gateway/src/main/lib/hono-client-device-amqp-2.2.0-SNAPSHOT.jar </systemPath> </dependency> <!--vertx响应式框架--> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-vertx</artifactId> </dependency> <!--启用YAML配置--> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-config-yaml</artifactId> </dependency>
-
-
新增类
- 根据官网实例 将
ProtocolGateway
以及TcpServer
,ConfigurationProducer
类添加到你的项目中
- 根据官网实例 将
-
终端执行
mvn install
试试能不能将编译成功- 打完包后执行
java -jar .\target\Hono-Gateway-1.0-SNAPSHOT-runner.jar
- 打完包后执行
-
出现
successfully connected to Hono's AMQP adapter
表示连接成功
连接本地Hono
身份认证
# 网关配置
gateway:
tcp:
insecurePort: 6666
insecurePortBindAddress: "127.0.0.1"
amqp:
host: 127.0.0.1 #hono.eclipseprojects.io
port: 5672
# deviceId@tenantId
username: 2d5c083b-07a2-47d1-a58e-147dcc12d7de@391d85d4-1d6f-4d54-8050-21292809cf1a
password: this-is-my-password
serverRole: AMQP adapter
启动项目
mvn compile quarkus:dev
启动nc连接
报错
没有满足依赖关系
引用 hono-client-command-2.1.1.jar