<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello_native</name>
<properties>
<vertx.verticle>vertx.HTTPVerticle</vertx.verticle>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.testSource>1.8</maven.compiler.testSource>
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>4.0.0.CR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.reactiverse</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>1.0.18</version>
<executions>
<execution>
<id>vmp</id>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
</plugins>
</build>
</project>
使用 GraalVM 构建 Vert.x Native Image 原生镜像
本实战指南详细介绍了使用 GraalVM 将 Vert.x 应用程序编译为 Native Image 原生镜像所需的所有步骤、配置要点以及规避方案。
你将构建什么
-
你将编写一个包含客户端与服务端的完整多 Verticle 应用;
-
涵盖安全通信与 SSL/TLS 证书加解密;
-
借助 GraalVM 提前将应用程序 AOT 静态编译为独立的操作系统原生机器码。
前置要求
-
文本编辑器或 IDE
-
GraalVM 运行时环境(推荐 22.3.0 或更新版本)
-
Apache Maven
什么是 Native Image?为什么 Vert.x 是其绝佳搭档?
Native Image 是将 Java 应用程序直接优化翻译为特定操作系统与 CPU 架构的机器码二进制文件。 与传统的 JVM 应用程序相比,这种全新的原生程序具有截然不同的特性:
-
极速瞬时冷启动:毫秒级进入就绪状态,无需 JVM 解释器预热;
-
极小的物理内存占用:初始常驻内存(RSS)通常仅需十几兆。
因此,原生镜像极为契合 CLI 命令行工具、Serverless 极速扩缩容 以及 边缘计算 场景。
GraalVM 原生静态编译在设计上具有诸多反射与动态类加载限制,而 Vert.x Core 核心库在架构上天然追求极致轻量、避免滥用反射,因此 Vert.x 是构建原生镜像的极佳选手。
第一步:基础应用开发
首先创建一个最简的 HTTP 服务端:
pom.xml核心业务服务位于 src/main/java/vertx/HTTPVerticle.java:
package vertx;
import io.vertx.core.AbstractVerticle;
public class HTTPVerticle extends AbstractVerticle {
@Override
public void start() {
vertx.createHttpServer().requestHandler(req -> {
req.response()
.putHeader("content-type", "text/plain")
.end("Hello from Vert.x!");
}).listen(8080, listen -> {
if (listen.succeeded()) {
System.out.println("Server listening on http://localhost:8080/");
} else {
listen.cause().printStackTrace();
System.exit(1);
}
});
}
}
第二步:引入 HTTPS 安全套接字与反射配置
在原生镜像中使用 SSL/TLS 证书时,需要为原生编译器提供反射与资源描述清单。
配置文件存放在 src/main/resources/META-INF/native-image/ 目录下:
-
reflect-config.json:声明动态反射类; -
resource-config.json:声明打包进二进制文件的证书资源。
构建原生二进制可执行文件
运行 Maven 打包原生镜像:
mvn package -Pnative
GraalVM 原生编译器将执行全量静态分析并输出独立可执行文件 target/myapp。
启动运行:
./target/myapp
观察启动日志:从双击到端口监听完成耗时仅需几个毫秒!
总结
本文档展示了:
-
Vert.x 与 GraalVM Native Image 原生编译的适配哲学;
-
配置 SSL 加密与原生反射元数据;
-
编译生成无依赖、瞬时冷启动的高性能原生可执行文件。