★★★★★
★★★★★
★★★★
★★★★★
★★★★★
★★★★★
# 方式一:Buildpacks(推荐)
mvn -Pnative spring-boot:build-image
# 方式二:GraalVM直接编译
mvn -Pnative package
native-image -jar target/app.jar
★★★★
★★★★
★★★
★★★
Context context = Context.create();
context.eval("js", "console.log('Hello from JS')");
Value result = context.eval("python", "2 + 3");
★★★★
FROM ghcr.io/graalvm/native-image-community:21 AS builder
WORKDIR /app
COPY . .
RUN ./mvnw -Pnative package
FROM gcr.io/distroless/java-base
COPY --from=builder /app/target/app /app
ENTRYPOINT ["/app"]
★★★★★
[
{
"name": "com.example.MyClass",
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allDeclaredFields": true
}
]
★★★★
★★★★
{
"resources": {
"includes": [
{"pattern": "application.yml"},
{"pattern": "templates/.*"}
]
}
}
★★★★
★★★★★
# 多阶段构建
FROM ghcr.io/graalvm/native-image-community:21 AS build
COPY . .
RUN ./mvnw -Pnative package
FROM gcr.io/distroless/java-base-debian12
COPY --from=build /app/target/app /app
EXPOSE 8080
ENTRYPOINT ["/app"]
★★★★
★★★★
★★★
★★★★
★★★★
★★★★
★★★★★
★★★★
★★★★
FROM eclipse-temurin:21-jre AS builder
WORKDIR /app
COPY target/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract
FROM eclipse-temurin:21-jre
COPY --from=builder /app/dependencies/ ./
COPY --from=builder /app/spring-boot-loader/ ./
COPY --from=builder /app/snapshot-dependencies/ ./
COPY --from=builder /app/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
★★★★
★★★★
★★★★
resources:
requests: { cpu: "500m", memory: "512Mi" }
limits: { cpu: "2000m", memory: "1Gi" }
★★★★
★★★★
★★★★
★★★★
★★★★★
★★★★★
★★★★
★★★★
★★★★
★★★★★
★★★★
★★★★
★★★
★★★★
★★★★★
★★★★
★★★★
# GitLab CI示例
native-build:
stage: build
image: ghcr.io/graalvm/native-image-community:21
script:
- ./mvnw -Pnative package
artifacts:
paths: [target/app]
cache:
key: native-image-cache
paths: [.native-image-cache/]
docker-build:
stage: package
script:
- docker build -t registry.example.com/app:native .
- docker push registry.example.com/app:native
★★★★
★★★★
★★★
★★★★
| 维度 | Java Native Image | Go | Rust |
|---|---|---|---|
| 启动时间 | 50ms | 10ms | 5ms |
| 内存占用 | 50-100MB | 10-30MB | 5-20MB |
| 峰值性能 | 高 | 中 | 最高 |
| 开发效率 | 高(Spring生态) | 高 | 低(学习曲线陡) |
| 生态 | 最丰富 | 云原生丰富 | 增长中 |
| 人才 | 最多 | 多 | 少 |
★★★★★