开发者

关于SpringBoot改动后0.03秒启动的问题

开发者 https://www.devze.com 2022-12-22 10:55 出处:网络 作者: 我是刘奇奇
目录SpringBoot改动后0.03秒启动一、概述二、环境准备三、创建SpringBoot项目四、打包镜像SpringBoot改动后0.03秒启动
目录
  • SpringBoot改动后0.03秒启动
  • 一、概述
  • 二、环境准备
  • 三、创建SpringBoot项目
  • 四、打包镜像

SpringBoot改动后0.03秒启动

一、概述

GraalVM 是一种高性能运行时,可显着提高应用程序性能和效率,非常适合微服务. 对于 Java 程序 GraalVM 负责将 Java 字节码编译成机器码,映像生成过程使用静态分析来查找可从主 Java 方法访问的任何代码,然后执行完全提前 (AOT) 编译。生成的本机二进制文件包含机器代码形式的整个程序,以便立即执行。

二、环境准备

下载 GraalVM : https://www.graalvm.org/downloads/,需要选择Java 17版本,下载完成后解压配置java环境变量

GRAALVM_HOME: D:\devops\graalvm-ce-java17-22.3.0
JAVA_HOME: %GRAALVM_HOME%
PATH: %GRAALVM_HOME%\bin

cmd(不要使用PowerShell,powershell可能回报错) 运行 gu install native-image

三、创建SpringBoot项目

SDK选择17

关于SpringBoot改动后0.03秒启动的问题

pom文件如下,都是自动生成的,没有修改

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot<开发者_JS开发;/groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo6</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo6</name>
    <description>demo6</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

创建IndexController,内容如下:

@RestController
@RequestMapping
public class IndexController {
    @RequestMapping("/")
    public String test(){
        return "spring-native";
    }
}

四、打包镜像

在项目目录里启动cmd,输入命令

 mvn clean -U -DskipTests -Pnative spring-boot:build-image

运行命令:

C:\Windows\system32>docker images
demo6                      0.0.1-SNAPSHOT   f025642103bf   43 years ago    96.8MB
C:\Windows\system32> docker run -di -p 8080:8080 demo6:0.0.1-SNAPSHOT

C:\Windows\system32>docker stats
CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           block I/O   PIDS
159c9bd5f0dc   jolly_hellman     0.01%     47.51MiB / 25.01GiB   0.19%     1.98kB / 538B     0B / 0B     18
PS D:\devops\java-project\demo6> docker logs -f 159c9bd5f0dc

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.0)

2022-12-21T07:19:43.970Z  INFO 1 --- [           main] com.example.demo.Demo6Application        : Starpythonting AOT-processed Demo6Application using Java 17.0.5 with PID 1 (/workspace/com.example.demo.Demo6Application started by cnb in /workspace)
2022-12-21T07:19:43.971Z  INFO 1 --- [           main] com.example.demo.Demo6Application        : No active profile set, falling back to 1 default profile: "default"
2022-12-21T07:19:43.979Z  INFO 1 --- js[           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-12-21T07:19:43.980Z  INFO 1 --- [           main] o.apache.catalina.core.StandardService 编程客栈  : Starting service [Tomcat]
2022-12-21T07:19:43.980Z  INFO 1 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-12-21T07:19:43.983Z  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-12-21T07:19:43.983Z  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 12 ms
2022-12-21T07:19:43.997Z  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) wit编程h context path ''
2022-12-21T07:19:43.997Z  INFO 1 --- [           main] com.example.demo.Demo6Application        : Started Demo6Application in 0.032 seconds (process running for 0.034)
2022-12-21T07:20:13.975Z  INFO 1 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-php12-21T07:20:13.975Z  INFO 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-12-21T07:20:13.976Z  INFO 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

可以看出使用native,打出的包为96M,运行内存47M,启动时间0.032S,占用资源和启动速度方面还是非常有优势的

到此这篇关于SpringBoot改动后0.03秒启动的文章就介绍到这了,更多相关SpringBoot改动后0.03秒启动内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消