栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

dmikusa/graalvm-tiny

2020.0.2

spring-release

Spring release

https://repo.spring.io/release

false

spring-milestone

Spring milestone

https://repo.spring.io/milestone

false

spring-snapshot

Spring Snapshots

https://repo.spring.io/snapshot

false

spring-release

Spring release

https://repo.spring.io/release

false

spring-milestone

Spring milestone

https://repo.spring.io/milestone

false

spring-snapshot

Spring Snapshots

https://repo.spring.io/snapshot

false

org.springframework.experimental

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}

true

${native.build.args}

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,一个负责构建镜像:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号