Spring boot 3 && GraalVM Native Image

Spring boot 3 && GraalVM Native Image

什么是 GraalVM?

GraalVM is a high-performance JDK designed to accelerate the execution of applications written in Java and other JVM languages while also providing runtimes for JavaScript, Python, and a number of other popular languages. GraalVM offers two ways to run Java applications: on the HotSpot JVM with Graal just-in-time (JIT) compiler or as an ahead-of-time (AOT) compiled native executable. GraalVM’s polyglot capabilities make it possible to mix multiple programming languages in a single application while eliminating foreign language call costs.

GraalVM是一个高性能的JDK,旨在加速用Java和其他JVM语言编写的应用程序的执行,同时也为JavaScript、Python和许多其他流行语言提供运行时。GraalVM提供了两种运行Java应用程序的方法:在HotSpot JVM上使用Graal实时(JIT)编译器或作为提前(AOT)编译的本地可执行文件。GraalVM的多语言功能使得在一个应用程序中混合多种编程语言成为可能,同时消除了外部调用成本。

GraalVM 架构

在这里插入图片描述

什么是 GraalVM Native Image?

GraalVM Native Images是独立的可执行文件,可以通过提前处理编译的Java应用程序来生成。本机映像通常比JVM映像占用的内存更少,启动速度更快。真的很快

Spring boot 3 & GraalVM Native Image

Windows 版本

Windows 10 22H2
下载需要的包,否则后面 java maven 编译会报错
通过微软WDK网址下载相关的软件

  • visual studio
    visual studio 2019 版本 (估计2022也可以)
    工作负荷-通用 Windows 平台开发

  • 安装 WDK
    适用于 Windows 10 版本 2004 的 WD

以上2个耗时较长。

  • 安装docker desktop

环境变量 一定要设置,否则使用mvn native:compile 编译时可能出现 cl.exe 找不到, 也会报 fatal error C1083: 无法打开包括文件: “stdio.h ”: No such file or directory]

MSVC_HOME=C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133
WK10_BIN_HOME=C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0
WK10_INCLUDE_HOME=C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0
WK10_LIB_HOME=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0
INCLUDE=%WK10_INCLUDE_HOME%\ucrt;%WK10_INCLUDE_HOME%\um;%WK10_INCLUDE_HOME%\shared;%MSVC_HOME%\include;
LIB=%WK10_LIB_HOME%\um\x64;%WK10_LIB_HOME%\ucrt\x64;%MSVC_HOME%\lib\x64;
#此处追加
Path=%MSVC_HOME%\bin\HostX64\x64;%WK10_BIN_HOME%\x64

创建一个springboot 工程

pom.xml

<?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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>pr.iceworld.fernando</groupId>
    <artifactId>spring-boot3-graavm-image</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- repository -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

    <profiles>
        <profile>
            <id>native</id>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>pr.iceworld.fernando.springboot3.image.MainApplication</mainClass>
                    <image>
                        <!-- 如果不指定,默认builder 是 tiny 版本,无法使用 docker run 运行 -->
                        <builder>paketobuildpacks/builder:base</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
@RestController
//@SpringBootApplication
//@SpringBootConfiguration
@EnableAutoConfiguration
//@ComponentScan
public class MainApplication {

    @RequestMapping("/")
    String home() {
        return "Hello It's me!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

编译工程并打包

mvn -Pnative native:compile 

在这里插入图片描述
spring-boot3-graavm-image.exe 可以直接执行,且加载很快,很快
···
2022-12-12T23:51:15.726+08:00 INFO 1224 — [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 69 ms
2022-12-12T23:51:15.750+08:00 INFO 1224 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’
2022-12-12T23:51:15.750+08:00 INFO 1224 — [ main] p.i.f.springboot3.image.MainApplication : Started MainApplication in 0.105 seconds (process running for 0.111)
···

C:\Users\ferna>curl localhost:8080
Hello It's me!

打包一个docker 镜像

mvn -Pnative spring-boot:build-image

REPOSITORY                  TAG        IMAGE ID       CREATED        SIZE
paketobuildpacks/run        base-cnb   3e3acccbaa17   4 days ago     88.8MB
paketobuildpacks/run        tiny-cnb   6cfe3de75423   4 days ago     17.3MB
spring-boot3-graavm-image   3.0.0      9b7b85e90f52   42 years ago   167MB
paketobuildpacks/builder    base       0593f8ae856c   42 years ago   1.35GB
paketobuildpacks/builder    <none>     215b9051b4b1   42 years ago   597MB
paketobuildpacks/builder    tiny       7841a1c811a3   42 years ago   590MB
docker run --rm -p 8080:8080 spring-boot3-graavm-image:3.0.0
C:\Users\ferna>curl localhost:8080
Hello It's me!

Linux 版本

TODO

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Spring Boot AOT (Ahead-Of-Time Compilation) 是指在Spring Boot应用程序启动之前将Java字节码编译成本地系统代码的过程,以提高应用程序的性能和响应速度。在Spring Boot 3.0或Spring Framework 6.0之后,我们可以直接使用Spring Framework 6.0内置的支持来处理AOT。我们可以通过自定义aot.factories文件配置来实现。未来,mica-auto也将支持使用注解来生成aot.factories文件。在Gradle构建工具中,我们可以在build.gradle文件中配置相关插件和依赖项,以支持Spring Boot AOT。例如,我们可以使用org.springframework.boot和io.spring.dependency-management插件,并在dependencies部分添加相应的依赖项。在应用程序代码中,我们需要使用@RestController和@SpringBootApplication注解来标记主类和控制器类。在主方法中,我们使用SpringApplication.run()方法启动应用程序。通过以上配置和代码,我们可以实现Spring Boot AOT。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Spring Boot 3.0 抢先了解:aot.factories 是个啥?](https://blog.csdn.net/j3T9Z7H/article/details/127437745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring Boot 3的AOT(GraalVM Native Image)应用开发](https://blog.csdn.net/haiyan_qi/article/details/128057967)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值