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

828 阅读2分钟

一、前言

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

我们翻译了多篇文章:

今天我们要分享得是将 mica-mqtt 编译成本机可执行程序。

二、mica-mqtt

t-io 是一款高性能、低延迟的网络框架并且使用简单,内置了很多其它框架没有但是非常实用的功能。 关于 t-io 更多信息,可以查看 t-io 官网: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 的相关源码:gitee.com/596392912/m…

三、支持 GraalVM

由于 t-io 依赖比较少,仅仅依赖 fastjsoncaffeine,所以将 t-io 程序编译成 GraalVM Native Image 要轻松很多。

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

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 处理

查看过我之前的文章“”的朋友应该知道,我上次折腾 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。 profiles.png package 编译完成之后可以看到 target 下就有生成了 mqtt-server-graalvm、mqtt-client-graalvm 2个可执行文件。注意:Windows 下是 .exe

使用 upx 压缩可执行文件,upx 之前译文中有介绍,这里不做过多的说明。 mica-mqtt-upx.jpg 可以看到可执行文件从 30、40M 减小到了 10多M。直接 ./mqtt-server-graalvm 启动,重 t-io 的启动日志可以看到启动非常快毫秒级别。 mica-mqtt-graalvm.png

我们再来对比下 jar 包的启动日志: mica-mqtt-jar.png

高下立判!!!

五、效果演示

mica-mqtt.gif

我们会持续关注和使用 GraalVM、Spring native,欢迎关注我们。