GraalVM 指南
GraalVM Native Image
GraalVM native image 提前将 Java 代码编译为本地可执行文件,从而实现更快的启动速度和更低的内存使用。但是,本地镜像不支持运行时 JIT 编译或反射,除非进行显式配置。
Apache Fory™ 通过使用代码生成而非反射,可以完美地与 GraalVM native image 配合使用。所有序列化器代码都在构建时生成,在大多数情况下 无需反射配置文件。
工作原理
当您执行以下操作时,Fory 会在 GraalVM 构建时生成序列化代码:
- 将 Fory 创建为静态字段
- 在静态初始化器中注册所有类
- 调用
fory.ensureSerializersCompiled()来编译序列化器 - 通过
native-image.properties配置该类在构建时初始化
主要优势:对于大多数可序列化的类,您无需配置反射 json 或序列化 json。
注意:Fory 的 asyncCompilationEnabled 选项在 GraalVM native image 中会自动禁用,因为不支持运行时 JIT。
基础用法
步骤 1:创建 Fory 并注册类
import org.apache.fory.Fory;
public class Example {
// 必须是静态字段
static Fory fory;
static {
fory = Fory.builder().build();
fory.register(MyClass.class);
fory.register(AnotherClass.class);
// 在构建时编译所有序列化器
fory.ensureSerializersCompiled();
}
public static void main(String[] args) {
byte[] bytes = fory.serialize(new MyClass());
MyClass obj = (MyClass) fory.deserialize(bytes);
}
}
步骤 2:配置构建时初始化
创建 resources/META-INF/native-image/your-group/your-artifact/native-image.properties:
Args = --initialize-at-build-time=com.example.Example
ForyGraalVMFeature(可选)
对于大多数具有公共构造函数的类型,上述基本设置就足够了。但是,一些高级情况需要反射注册:
- 私有构造函数(没有可访问的无参构造函数的类)
- 私有内部类/记录
- 动态代理序列化
fory-graalvm-feature 模块会自动处理这些情况,无需手动配置 reflect-config.json。
添加依赖
<dependency>
<groupId>org.apache.fory</groupId>
<artifactId>fory-graalvm-feature</artifactId>
<version>${fory.version}</version>
</dependency>
启用功能
将以下内容添加到您的 native-image.properties:
Args = --initialize-at-build-time=com.example.Example \
--features=org.apache.fory.graalvm.feature.ForyGraalVMFeature
ForyGraalVMFeature 处理的内容
| 场景 | 不使用 Feature | 使用 Feature |
|---|---|---|
| 具有无参构造函数的公共类 | ✅ 可工作 | ✅ 可工作 |
| 私有构造函数 | ❌ 需要 reflect-config.json | ✅ 自动注册 |
| 私有内部记录 | ❌ 需要 reflect-config.json | ✅ 自动注册 |
| 动态代理 | ❌ 需要手动配置 | ✅ 自动注册 |
私有记录示例
public class Example {
// 私有内部记录 - 需要 ForyGraalVMFeature
private record PrivateRecord(int id, String name) {}
static Fory fory;
static {
fory = Fory.builder().build();
fory.register(PrivateRecord.class);
fory.ensureSerializersCompiled();
}
}
动态代理示例
import org.apache.fory.util.GraalvmSupport;
public class ProxyExample {
public interface MyService {
String execute();
}
static Fory fory;
static {
fory = Fory.builder().build();
// 为序列化注册代理接口
GraalvmSupport.registerProxySupport(MyService.class);
fory.ensureSerializersCompiled();
}
}
线程安全的 Fory
对于多线程应用程序,使用 ThreadLocalFory:
import org.apache.fory.Fory;
import org.apache.fory.ThreadLocalFory;
import org.apache.fory.ThreadSafeFory;
public class ThreadSafeExample {
public record Foo(int f1, String f2, List<String> f3) {}
static ThreadSafeFory fory;
static {
fory = new ThreadLocalFory(classLoader -> {
Fory f = Fory.builder().build();
f.register(Foo.class);
f.ensureSerializersCompiled();
return f;
});
}
public static void main(String[] args) {
Foo foo = new Foo(10, "abc", List.of("str1", "str2"));
byte[] bytes = fory.serialize(foo);
Foo result = (Foo) fory.deserialize(bytes);
}
}