t-io 之 GraalVM 本机可执行程序实战

JAVA架构日记

共 4430字,需浏览 9分钟

 ·

2021-07-19 18:52

一、前言

hello 小伙伴们大家好,我是如梦技术春哥mica 作者),我们一直在关注和探索 GraalVM 和 Spring native 的使用。

我们翻译了多篇文章:

  1. 【译】GraalVM 快速参考

  2. Spring Native 入门实战

  3. Spring Native 可以正式使用了么?

  4. 【译】使用 GraalVM 的高性能 Java

今天我们要分享的是将 mica-mqttt-io 应用)编译成本机可执行程序。

二、mica-mqtt

t-io 是一款高性能、低延迟的网络框架并且使用简单,内置了很多其它框架没有但是非常实用的功能。关于 t-io 更多信息,可以查看 t-io 官网:https://www.tiocloud.com

mica-mqtt 是我开发的基于 t-io 实现的 mqtt 物联网组件。包含了 mqtt 客户端和服务端,支持 MQTT v3.1、v3.1.1 以及 v5.0 协议。前 2 周我对 mica-mqtt 和 github 上较活跃的 mqtt 组件进行了压测对比,包括基于 netty 和 reactor-netty 的服务。mica-mqtt 在连接速度上要比它们快很多,感兴趣的朋友可以查看 mica-mqtt 源码(https://gitee.com/596392912/mica-mqtt并自行测试。

三、支持 GraalVM

由于 t-io 依赖比较少,仅仅依赖 fastjsoncaffeine,所以将 t-io 程序编译成本机可执行程序要轻松很多。

3.1 安装 GraalVM,这里不做过多的讲解,可以查看我们之前的文章。

注意:由于 caffeine 3.x 最低需要 java11,请下载 graalvm-ce-java11 版本。

3.2 添加依赖

<dependency>
 <groupId>org.graalvm.sdk</groupId>
 <artifactId>graal-sdk</artifactId>
 <version>${graalvm.version}</version>
 <scope>provided</scope>
</dependency>

3.3 添加 maven 插件

<plugin>
 <groupId>org.graalvm.nativeimage</groupId>
 <artifactId>native-image-maven-plugin</artifactId>
 <version>${graalvm.version}</version>
 <executions>
  <execution>
   <goals>
    <goal>native-image</goal>
   </goals>
   <phase>package</phase>
  </execution>
 </executions>
 <configuration>
  <skip>false</skip>
  <imageName>mqtt-server-graalvm</imageName>
  <mainClass>${mainClass.server}</mainClass>
  <buildArgs>
   --enable-all-security-services
   --report-unsupported-elements-at-runtime
   -H:+RemoveSaturatedTypeFlows
   --allow-incomplete-classpath
   -H:ReflectionConfigurationFiles=../graalvm/reflect-config.json
  </buildArgs>
 </configuration>
</plugin>

3.4 caffeine 处理

查看过我之前的文章《Spring Native 可以正式使用了么?》的朋友应该知道,我上次折腾 mica-caffeine 没有成功。

我们先排除 t-io 里自带的 caffeine 2.x,然后添加 caffeine 3.x 的依赖(caffeine 3.x 减少反射和 Unsafe 的使用,对 GraalVM 支持更好)。

<dependency>
 <groupId>com.github.ben-manes.caffeine</groupId>
 <artifactId>caffeine</artifactId>
 <version>3.0.3</version>
</dependency>

当然 caffeine 3.x 还是需要添加一些配置,reflect-config.json 内容如下:

[
  {
    "name""com.github.benmanes.caffeine.cache.PSW",
    "allDeclaredConstructors"true
  },
  {
    "name""com.github.benmanes.caffeine.cache.PSWMS",
    "allDeclaredConstructors"true
  },
  {
    "name""com.github.benmanes.caffeine.cache.SSLA",
    "allDeclaredConstructors"true
  },
  {
    "name""com.github.benmanes.caffeine.cache.SSLMSW",
    "allDeclaredConstructors"true
  },
  {
    "name""com.github.benmanes.caffeine.cache.SSMSW",
    "allDeclaredConstructors"true
  },
  {
    "name""com.github.benmanes.caffeine.cache.SSLMSA",
    "allDeclaredConstructors"true
  },
  {
    "name""com.github.benmanes.caffeine.cache.PSAMS",
    "allDeclaredConstructors"true
  }
]

3.5 日志的处理

为了减少配置,我为 GraalVM 环境添加的是 jdk logger。

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.31</version>
</dependency>

四、编译

mica-mqtt 添加了 mqtt-client-graal、mqtt-server-graal 2个 profile。

package 编译完成之后可以看到 target 目录下就有生成了 mqtt-server-graalvmmqtt-client-graalvm 2个可执行文件。Windows 下是 .exe文件。


使用 upx 压缩可执行文件,upx 之前译文中有介绍,这里不做过多的说明。

可以看到可执行文件从 30、40M 减小到了 10多M。

直接 ./mqtt-server-graalvm 启动,从启动日志可以看到启动非常快(毫秒级)

我们再来对比下 jar 包的启动日志:

高下立判!!!

五、效果演示


注意:其中有个 caffeine 的异常日志,实质是 caffeine 的一个反射检测,可忽略。

浏览 46
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报