One, foreword
I’m MicA. We’ve been following and exploring GraalVM and Spring Native.
We have translated many articles:
- GraalVM Quick Reference
Today we are going to share how to compile micA-MQTT into a machine executable.
Second, the mica – MQTT
T-io is a high performance, low latency networking framework and is simple to use, with many built-in features not found in other frameworks but very useful. For more information about T-IO, visit www.tiocloud.com.
Mica-mqtt is an MQTT iot component developed by me based on T-IO implementation. Includes MQTT client and server, supports MQTT V3.1, V3.1.1 and V5.0 protocols,
In the last two weeks, I’ve been pressure-testing MicA-MQTT against some of the more active MQTT components on Github, including netty and reactor-Netty services, and micA-MQTT connections are much faster. Interested friends can view micA-MQTT related source: gitee.com/596392912/m…
Support GraalVM
Since t-IO relies less on fastjson and Caffeine, compiling t-IO into GraalVM Native Image is much easier.
3.1 Installing GraalVM, we won’t go into too much detail here, you can check out our previous article.
3.2 Adding a Dependency
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
<scope>provided</scope>
</dependency>
Copy the code
3.3 Adding a Maven plug-in
<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>
Copy the code
3.4 caffeine processing
For those of you who checked out my previous post “Caffeine”, I didn’t get my caffeine fix last time.
We first removed caffeine 2.x from T-IO, and then added dependency to Caffeine 3.x (caffeine 3.x reduces the need for reflection and Unsafe, and is better for GraalVM support).
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.0.3</version>
</dependency>
Copy the code
Why caffeine 3.x needs to add some 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}]Copy the code
3.5 Log Processing
To reduce the configuration, I added a JDK Logger to the GraalVM environment.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.31</version>
</dependency>
Copy the code
Four, compile,
Mica-mqtt adds mqTT-client-Graal and MQTT-server-Graal profiles.After package compilation, it can be seen that two executable files mqTT-server-GraalVM and MQTT-client-GraalVM are generated under target. Note: Under Windows it is.exe
.
Use upX to compress executable files. Upx is described in the previous translation, not explained here.You can see that the executable file is reduced from 30 or 40 MB to over 10 MB. Direct./ MQTT-server-graalVM startup, heavy t-IO startup logs can see that startup is very fast on the milliseconds level.
Let’s compare the jar startup log again:
Judge!!
5. Effect demonstration
We will continue to follow and use GraalVM and Spring Native. Welcome to visit us.