Spring Native实战(畅快体验79毫秒启动springboot应用)

spring-native

0.10.0-SNAPSHOT

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

${classifier}

${builder}

<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>

<BP_NATIVE_IMAGE_BUILD_ARGUMENTS>${native.build.args}</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>

IF_NOT_PRESENT

org.springframework.experimental

spring-aot-maven-plugin

0.10.0-SNAPSHOT

test-generate

test-generate

generate

generate

  • 上述pom.xml有以下几处需要注意:
  1. 插件仓库、依赖库仓库、依赖库版本的配置都集中在这里;

  2. 配置好spring-aot-maven-plugin和spring-boot-maven-plugin这两个插件,子工程会用到;

  3. 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”>

spring-native-tutorials

com.bolingcavalry

1.0-SNAPSHOT

4.0.0

webmvc

org.springframework.experimental

spring-native

org.springframework.boot

spring-boot-starter-web

org.apache.tomcat.embed

tomcat-embed-core

org.apache.tomcat.embed

tomcat-embed-websocket

org.apache.tomcat.experimental

tomcat-embed-programmatic

${tomcat.version}

org.springframework.boot

spring-boot-starter-test

test

org.springframework.experimental

spring-aot-maven-plugin

true

org.springframework.boot

spring-boot-maven-plugin

  • 代码很简单,一个普通的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

41 years ago 452kB

41 years ago 2.51MB

41 years ago 57.2MB

41 years ago 1.4MB

41 years ago 268B

41 years ago 17.3MB

  • 镜像构建成功,可以验证基本功能了;

验证

  • 执行以下命令,创建一个临时容器(控制台结束后容器会被清理掉):

docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT

  • 控制台输出如下,79毫秒启动完成,真是一眨间的功夫:

(base) zhaoqindeMBP:~ zhaoqin$ docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT

2021-05-22 09:34:57.578 INFO 1 — [ main] o.s.nativex.NativeListener : This application is bootstrapped with code generated with Spring AOT

. ____ _ __ _ _

/\ / _ __ _ () __ __ _ \ \ \ \

( ( )___ | '_ | '| | ’ / _` | \ \ \ \

\/ _)| |)| | | | | || (| | ) ) ) )

’ || .__|| ||| |__, | / / / /

=|_|======|/=////

:: Spring Boot :: (v2.5.0-SNAPSHOT)

2021-05-22 09:34:57.586 INFO 1 — [ main] c.b.webmvc.WebmvcApplication : Starting WebmvcApplication using Java 1.8.0_292 on 3529ec458896 with PID 1 (/workspace/com.bolingcavalry.webmvc.WebmvcApplication started by cnb in /workspace)

2021-05-22 09:34:57.586 INFO 1 — [ main] c.b.webmvc.WebmvcApplication : No active profile set, falling back to default profiles: default

2021-05-22 09:34:57.661 INFO 1 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)

May 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol init

INFO: Initializing ProtocolHandler [“http-nio-8080”]

May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardService startInternal

INFO: Starting service [Tomcat]

May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardEngine startInternal

INFO: Starting Servlet engine: [Apache Tomcat/9.0.46]

May 22, 2021 9:34:57 AM org.apache.catalina.core.ApplicationContext log

INFO: Initializing Spring embedded WebApplicationContext

2021-05-22 09:34:57.669 INFO 1 — [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 79 ms

May 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler [“http-nio-8080”]

2021-05-22 09:34:57.713 INFO 1 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’

2021-05-22 09:34:57.713 INFO 1 — [ main] c.b.webmvc.WebmvcApplication : Started WebmvcApplication in 0.178 seconds (JVM running for 0.19)

2021-05-22 09:34:57.713 INFO 1 — [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT

2021-05-22 09:34:57.714 INFO 1 — [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC

  • 浏览器访问本机8080端口,如下图,应用基本功能正常:

在这里插入图片描述

  • 再看看资源使用情况,命令是docker stats,如下可见,内存仅用了30M:

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS

6ce6c66fb4de jovial_hertz 0.11% 30.69MiB / 3.844GiB 0.78% 1.49kB / 158B 4.31MB / 0B 18

  • 我曾经在hub.docker.com上放了一个传统springboot应用制作的镜像bolingcavalry/hellojib:0.0.1-SNAPSHOT,现在拿来和Spring Native镜像对比一下,启动信息如下,耗时2036毫秒:

(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker run --rm -P docker.io/bolingcavalry/hellojib:0.0.1-SNAPSHOT

. ____ _ __ _ _

/\ / _ __ _ () __ __ _ \ \ \ \

( ( )___ | '_ | '| | ’ / _` | \ \ \ \

\/ _)| |)| | | | | || (| | ) ) ) )

’ || .__|| ||| |__, | / / / /

=|_|======|/=////

:: Spring Boot :: (v2.1.6.RELEASE)

2021-05-22 11:13:28.121 INFO 1 — [ main] c.b.hellojib.HellojibApplication : Starting HellojibApplication on ffb32e5b68b9 with PID 1 (/app/classes started by root in /)

2021-05-22 11:13:28.128 INFO 1 — [ main] c.b.hellojib.HellojibApplication : No active profile set, falling back to default profiles: default

2021-05-22 11:13:30.000 INFO 1 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)

2021-05-22 11:13:30.054 INFO 1 — [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

2021-05-22 11:13:30.054 INFO 1 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]

2021-05-22 11:13:30.241 INFO 1 — [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2021-05-22 11:13:30.241 INFO 1 — [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2036 ms

2021-05-22 11:13:30.715 INFO 1 — [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor’

2021-05-22 11:13:31.103 INFO 1 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’

2021-05-22 11:13:31.110 INFO 1 — [ main] c.b.hellojib.HellojibApplication : Started HellojibApplication in 3.618 seconds (JVM running for 4.297)

《MySql面试专题》

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

《MySql性能优化的21个最佳实践》

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

《MySQL高级知识笔记》

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

文中展示的资料包括:**《MySql思维导图》《MySql核心笔记》《MySql调优笔记》《MySql面试专题》《MySql性能优化的21个最佳实践》《MySq高级知识笔记》**如下图

全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好

关注我,点赞本文给更多有需要的人

636)]

《MySQL高级知识笔记》

[外链图片转存中…(img-OZfeRKWv-1714148222636)]

[外链图片转存中…(img-A4H7jNJE-1714148222636)]

[外链图片转存中…(img-VXgA8Ka1-1714148222637)]

[外链图片转存中…(img-D3k6l5re-1714148222637)]

[外链图片转存中…(img-KpwVWuul-1714148222637)]

[外链图片转存中…(img-XP0sdc7r-1714148222638)]

[外链图片转存中…(img-prZ54qDy-1714148222638)]

[外链图片转存中…(img-eTlueUvD-1714148222638)]

[外链图片转存中…(img-kCelog9J-1714148222639)]

[外链图片转存中…(img-VQYq9pMA-1714148222639)]

文中展示的资料包括:**《MySql思维导图》《MySql核心笔记》《MySql调优笔记》《MySql面试专题》《MySql性能优化的21个最佳实践》《MySq高级知识笔记》**如下图

[外链图片转存中…(img-dsyDcngH-1714148222639)]

关注我,点赞本文给更多有需要的人

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值