Spring Native实战(畅快体验79毫秒启动springboot应用)
Posted 程序员欣宸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Native实战(畅快体验79毫秒启动springboot应用)相关的知识,希望对你有一定的参考价值。
欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
关于Spring Native
- Spring官方博客于2021年03月11日宣布Spring Native的beta版本发布,借助Spring Native可以将spring应用与GraalVM集成到native image中;
- native image是GraalVM的一项技术,会将java应用的字节码编译成可执行文件,还会与JDK的本地库做静态链接,运行应用时无需Java虚拟机,自身已集成了内存管理,线程调度等能力,更多信息请参考:https://www.graalvm.org/reference-manual/native-image/
- 本文以实战为主,因此不会用太多篇幅介绍Spring Native的理论和优势,这里简单小结几个重要特性:
- 应用启动速度不超过100毫秒;
- 启动即达到性能峰值(C1、C2等手段已经用不上了)
- 运行时更低的内存消耗;
- docker镜像不含JDK(所需文件已经抽取出来放入镜像),官方展示的含有Spring Boot, Spring MVC, Jackson, Tomcat的镜像大小是50M;
- 为了达到前面的效果,代价是构建时间更长;
Spring Native到底是什么
个人的理解:Spring Native是Spring提供的、制作native image的技术方案,涉及到以下关键技术:
- Spring ahead-of-time (AOT) 插件,对spring应用做AOT处理,使得传统虚拟机的class lazy loading在不复存在;
- spring-boot-maven-plugin插件在构建docker镜像的时候,使用了名为dmikusa/graalvm-tiny的镜像作为构建工具,这个工具负责将当前工程的构建结果和GraalVM集成在一起,最终制作成native image;
本篇概览
作为实战风格的文章,本篇主要内容是开发springboot应用再构建为native image,然后验证其功能和效果,本文由以下内容构成:
- 环境信息
- 新建名为spring-native-tutorials的maven父工程,对实战用到的依赖库、插件等做统一配置;
- 新建名为webmvc的maven子工程,这是个springboot应用;
- 将webmvc构建为native image,这是个docker镜像;
- 在docker中启动镜像,验证是否可用,并检查相关相关指标;
环境信息
本次实战相关的环境信息如下:
- 电脑:MacBook pro 13寸 2018
- 操作系统:macOS Big Sur 11.2.3
- IDE:IntelliJ IDEA 2018.3.5 (Ultimate Edition)
- docker:20.10.5
- JDK:1.8.0_211
- maven:3.6.0
- springboot:2.5.0-SNAPSHOT
- spring-aot-maven-plugin:0.10.0-SNAPSHOT
源码下载
- 本篇实战中的完整源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
- 这个git项目中有多个文件夹,本次实战的源码在spring-native-tutorials文件夹下,如下图红框所示:
新建名为spring-native-tutorials的maven父工程
- 对Spring Native的学习不是写出helloworld就完事,因此这里先创建一个父工程,为今后所有的应用提供统一的依赖库、插件管理;
- 新建名为spring-native-tutorials的maven父工程,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">
<modelVersion>4.0.0</modelVersion>
<modules>
<module>webmvc</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>com.bolingcavalry</groupId>
<artifactId>spring-native-tutorials</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<!-- springboot生成jar文件的文件名后缀,用来避免Spring Boot repackaging和native-image-maven-plugin插件之间可能存在的冲突 -->
<classifier/>
<!-- 构建镜像时的定制参数 -->
<native.build.args/>
<!-- 指定使用dmikusa/graalvm-tiny这个镜像作为构建工具,来构建镜像 -->
<builder>dmikusa/graalvm-tiny</builder>
<!-- spring cloud版本 -->
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<!-- 插件管理 -->
<pluginRepositories>
<pluginRepository>
<id>spring-release</id>
<name>Spring release</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestone</id>
<name>Spring milestone</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshot</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
<!--仓库管理-->
<repositories>
<repository>
<id>spring-release</id>
<name>Spring release</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring milestone</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshot</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<!--依赖包版本管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>0.10.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--插件配置-->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>${classifier}</classifier>
<image>
<builder>${builder}</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
<BP_NATIVE_IMAGE_BUILD_ARGUMENTS>${native.build.args}</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
</env>
<!--执行构建任务的镜像,如果在当前环境不存在才会远程下载-->
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
</image>
</configuration>
</plugin>
<!-- aot插件,ahead-of-time transformations -->
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>0.10.0-SNAPSHOT</version>
<executions>
<execution>
<id>test-generate</id>
<goals>
<goal>test-generate</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
- 上述pom.xml有以下几处需要注意:
- 插件仓库、依赖库仓库、依赖库版本的配置都集中在这里;
- 配置好spring-aot-maven-plugin和spring-boot-maven-plugin这两个插件,子工程会用到;
- spring-boot-maven-plugin插件制作docker镜像的时候,又会用到dmikusa/graalvm-tiny镜像,这才是真正构建native image的工具;
新建springboot类型的maven子工程
- 新建名为webmvc的子工程,pom.xml内容如下,可见内容很简单,就是常规依赖库和父工程配置的两个插件,一个负责执行AOT,一个负责构建镜像:
<?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>
<artifactId>spring-native-tutorials</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>webmvc</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat.experimental</groupId>
<artifactId>tomcat-embed-programmatic</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<configuration>
<removeSpelSupport>true</removeSpelSupport>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 代码很简单,一个普通的springboot应用,带http接口:
package com.bolingcavalry.webmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@SpringBootApplication
@RestController
public class WebmvcApplication {
public static void main(String[] args) {
SpringApplication.run(WebmvcApplication.class, args);
}
@ResponseStatus(HttpStatus.ACCEPTED)
@GetMapping("/status")
public String status() {
return "status";
}
@GetMapping("/")
public String hello() {
return "1. Hello from Spring MVC and Tomcat, " + LocalDateTime.now();
}
}
- 现在编码已完成,来构建docker镜像吧,进入父工程的pom.xml所在目录,执行以下命令:
mvn clean -U -DskipTests spring-boot:build-image
- 构建成功后输出信息如下(篇幅所限仅截取最后一小段),耗时4分25秒,期间笔记本风扇狂转:
...
[INFO] Successfully built image 'docker.io/library/webmvc:1.0-SNAPSHOT'
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for spring-native-tutorials 1.0-SNAPSHOT:
[INFO]
[INFO] spring-native-tutorials ............................ SUCCESS [ 1.786 s]
[INFO] webmvc ............................................. SUCCESS [04:19 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:25 min
[INFO] Finished at: 2021-05-22T16:36:44+08:00
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "nexus" could not be activated because it does not exist.
- 执行docker images命令,如下图,可见镜像已经生成:
- 查看镜像构成,可见每个layer都不大,共计七十多M:
(base) zhaoqindeMBP:~ zhaoqin$ docker history webmvc:1.0-SNAPSHOT
IMAGE CREATED CREATED BY SIZE COMMENT
b8ff54813ae0 41 years ago 69B
<missing> 41 years ago 452kB
<missing> 41 years ago 2.51MB
<missing> 41 years ago 57.2MB
<missing> 41 years ago 1.4MB
<missing>以上是关于Spring Native实战(畅快体验79毫秒启动springboot应用)的主要内容,如果未能解决你的问题,请参考以下文章