头图

I. Introduction

Hello everyone, my name is Dream Technology ( mica ), we have been following and exploring the use of GraalVM and Spring native .

We have translated several articles:

Today we are going to share that mica-mqtt compiled into a machine executable program.

Two, mica-mqtt

t-io is a high-performance, low-latency network framework and simple to use. It has many built-in functions that other frameworks do not have but are very useful.
For more information about t-io, please check the official website of t-io: https://www.tiocloud.com .

mica-mqtt is a mqtt IoT component based on t-io that I developed. Contains mqtt client and server, supports MQTT v3.1, v3.1.1 and v5.0 protocols,

In the first 2 weeks, I conducted a stress test comparison between mica-mqtt and the more active mqtt components on github, including services based on netty and reactor-netty. The connection speed of mica-mqtt is much faster than them, and interested friends can check it out. Relevant source code of mica-mqtt: https://gitee.com/596392912/mica-mqtt

Three, support GraalVM

Since t-io relatively few dependencies, it only relies on fastjson and caffeine , so the t-io be compiled into a lot of GraalVM Native Image.

3.1 Install GraalVM , do not do too much explanation here, you can check our previous article.

3.2 Add dependency

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

3.3 Add maven plugin

<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 handles

Friends who have viewed my previous article "" should know that the last time I tossed mica-caffeine was unsuccessful.

We first exclude the caffeine 2.x that comes with t-io, and then add the dependency of caffeine 3.x (caffeine 3.x reduces the use of reflection and Unsafe, and has better support for graalvm).

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

Of course, caffeine 3.x still needs to add some configuration reflect-config.json as follows:

[
  {
    "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 Log processing

In order to reduce the configuration, I added jdk logger to the GraalVM environment.

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

Four, compile

mica-mqtt added two profiles, mqtt-client-graal and mqtt-server-graal.
profiles.png
After the package is compiled, you can see that two executable files, mqtt-server-graalvm and mqtt-client-graalvm, have been generated under target. Note: .exe under Windows.

Use upx to compress executable files. It is introduced in the previous translation of upx, so I won't give too much explanation here.
mica-mqtt-upx.jpg
It can be seen that the executable file has been reduced from 30 and 40M to more than 10M. Start directly ./mqtt-server-graalvm, re-start the log of t-io, you can see that the startup is very fast at the millisecond level.
mica-mqtt-graalvm.png

Let's compare the startup log of the jar package:
mica-mqtt-jar.png

Sit down! ! !

Five, effect demonstration


https://www.bilibili.com/video/BV1jf4y1j72u/?aid=291677744&cid=370579266&page=1

Welcome to communicate with me, WeChat ID: JAVA Architecture Diary.


如梦技术
59 声望7 粉丝

人生就像愤怒的小鸟,当你失败时总又几只猪在笑。