首页 > 其他分享 >Spring Native打包本地镜像,无需通过Graal的maven插件buildtools

Spring Native打包本地镜像,无需通过Graal的maven插件buildtools

时间:2023-02-01 12:25:15浏览次数:58  
标签:02 INFO 插件 buildtools 48 Spring pkslow 2022

简介

在文章《GraalVM和Spring Native尝鲜,一步步让Springboot启动飞起来,66ms完成启动》中,我们介绍了如何使用Spring Native和buildtools插件,打包出本地镜像,也打包成Docker镜像。本文探索一下,如果不通过这个插件来生成镜像。这样我们可以控制更多、了解更多。

创建Spring Boot项目

创建Spring Boot项目,引入Spring Native,我们主要使用的是Spring Native提供的AOT功能。如下:

<dependencies>
  <dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>spring-native</artifactId>
    <version>${spring-native.version}</version>
  </dependency>

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

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.experimental</groupId>
      <artifactId>spring-aot-maven-plugin</artifactId>
      <version>${spring-native.version}</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>
</build>

Build image的时候,我们需要引用依赖库,所以加上下面的插件:

<plugin>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

接着先编译出Java class,命令如下:

$ mvn clean package

打包本地镜像

前面的命令打包完,在target目录下会有一个lib目录,我们需要用上。通过native-image命令来生成镜像,如下:

$ native-image -cp ./target/classes/:target/lib/* com.pkslow.springboot.SpringbootGraalVMNativeMain Pkslow.SpringbootGraalVMNativeMain

这样就会生成一个可执行文件:Pkslow.SpringbootGraalVMNativeMain

执行如下:

$ ./Pkslow.SpringbootGraalVMNativeMain

使用了93ms来启动,还是很快的。

访问服务是正常的:

$ curl -i http://localhost:8080/hi-graalvm
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 67
Date: Wed, 02 Nov 2022 15:08:44 GMT

This message is from Spring Boot built/run by GraalVM/Spring Native

创建Docker镜像

先拉取基础镜像:

$ docker pull springci/graalvm-ce:java11-0.12.x

进入容器看一下里面的状况:

$ docker run -it --rm --entrypoint bash springci/graalvm-ce:java11-0.12.x
root@d9b54bdce70a:/# pwd
/
root@d9b54bdce70a:/# java --version
openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment GraalVM 22.1.1.0-dev (build 11.0.15+10-jvmci-22.1-b06)
OpenJDK 64-Bit Server VM GraalVM 22.1.1.0-dev (build 11.0.15+10-jvmci-22.1-b06, mixed mode, sharing)
root@d9b54bdce70a:/# which java
/opt/java/bin/java
root@d9b54bdce70a:/# which native-image
/opt/java/bin/native-image

准备一下Dockerfile如下:

FROM springci/graalvm-ce:java11-0.12.x
VOLUME /tmp

ARG PORT=8080
ARG TIME_ZONE=Asia/Shanghai

ENV TZ=${TIME_ZONE}
EXPOSE ${PORT}
RUN pwd

RUN mkdir /pkslow-application
WORKDIR /pkslow-application/
RUN cd /pkslow-application
COPY target/classes/ /pkslow-application/classes/
COPY target/lib/ /pkslow-application/lib/

RUN native-image -cp /pkslow-application/classes/:/pkslow-application/lib/* \
    com.pkslow.springboot.SpringbootGraalVMNativeMain \
    Pkslow.SpringbootGraalVMNativeMain


ENTRYPOINT ["/pkslow-application/Pkslow.SpringbootGraalVMNativeMain"]

打包Docker镜像:

$ docker build . -t pkslow/spring-boot-native-without-buildtools:1.0-SNAPSHOT -f src/main/dker/Dockerfile

完成后通过下面命令启动,57ms完成启动:

$ docker run -it -p 8080:8080 --rm pkslow/spring-boot-native-without-buildtools:0-SNAPSHOT
2022-11-02 23:48:40.918  INFO 1 --- [           main] o.s.nativex.NativeListener               : AOT mode enabled

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

2022-11-02 23:48:40.920  INFO 1 --- [           main] c.p.s.SpringbootGraalVMNativeMain        : Starting SpringbootGraalVMNativeMain using Java 11.0.15 on 12528bd45fd7 with PID 1 (/pkslow-application/Pkslow.SpringbootGraalVMNativeMain started by root in /pkslow-application)
2022-11-02 23:48:40.920  INFO 1 --- [           main] c.p.s.SpringbootGraalVMNativeMain        : No active profile set, falling back to default profiles: default
2022-11-02 23:48:40.934  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-11-02 23:48:40.935  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-02 23:48:40.935  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-02 23:48:40.940  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-02 23:48:40.940  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 19 ms
2022-11-02 23:48:40.963  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-02 23:48:40.964  INFO 1 --- [           main] c.p.s.SpringbootGraalVMNativeMain        : Started SpringbootGraalVMNativeMain in 0.057 seconds (JVM running for 0.061)
2022-11-02 23:48:57.098  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-11-02 23:48:57.098  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-11-02 23:48:57.098  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

测试正常启动:

$ curl -i http://127.0.0.1:8080/hi-graalvm
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 67
Date: Wed, 02 Nov 2022 15:49:05 GMT

This message is from Spring Boot built/run by GraalVM/Spring Native

为了集成打包镜像到CI/CD中,在pom添加以下插件:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <phase>install</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <skip>${docker.skip}</skip>
        <executable>docker</executable>
        <arguments>
          <argument>build</argument>
          <argument>.</argument>
          <argument>-t</argument>
          <argument>pkslow/${project.artifactId}:${project.version}</argument>
          <argument>-f</argument>
          <argument>src/main/docker/Dockerfile</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

通过以下命令即可生成Docker镜像:

$ mvn clean install -Ddocker.skip=false

代码

代码请看GitHub: https://github.com/LarryDpk/pkslow-samples

标签:02,INFO,插件,buildtools,48,Spring,pkslow,2022
From: https://www.cnblogs.com/larrydpk/p/17082128.html

相关文章

  • spring 事务不生效
    1.方法自身(this)调用问题,导致事务失效 非事务方法seckillVoucher()中调用的自身类的事务方法createVoucherOrder()。解决办法:ps:要加aspj依赖,同时在Application中开启@......
  • BdTab全新一代的浏览器新标签页插件
    BdTab新标签页是最受欢迎的浏览器扩展程序之一,它不但能够美化你的主页,同时能有效提高生产力,非常轻松高效的管理常用网站,快速添加常用网站图标、颜色、大小、布局均可自由......
  • Spring Security、Oauth2与JWT
    SpringSecurity、Oauth2与JWT安全框架基础认证、会话与授权用户身份认证:用户身份认证即用户去访问系统资源时系统要求验证用户的身份信息,身份合法方可继续访问。常见的......
  • vim 一些插件的学习
    1.neoformat:Neoformat,输入命令之后按空格+tab键进行选择格式化的程序,项目地址:https://github.com/sbdchd/neoformat2.vim-multiple-cursors选中任意文本后,使用Ct......
  • pluginManagement插件管理
    Maven中提供了dependencyManagement来解决继承的问题,同样也提供了解决插件继承问题的pluginManagement元素,在父pom中可以在这个元素中声明插件的配置信息,但是子pom.xml中不......
  • Spring-Boot/Spring-Cloud继承问题
    继承使用dependencyManagement进行依赖管理,但是只有使用继承的时候,dependencyManagement中声明的依赖才可能被子pom.xml用到,如果我的项目本来就继承了一个父pom.xml,但是现......
  • SpringBoot 前端大文件上传
    ​ 最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现。在某些业务中,大文件上传是一个比较重要的......
  • springboot Config类@Import导入Config类中bean覆盖方法
    spring-cloud-openfeign-core第三方包里面有FeignClientsConfiguration.class里面是包括了feign需要的bean,但里面已经定义了feignEncoder、feignDecoder,如果项目中想要定制......
  • SpringMVC前后端分离关键
    SpringMVC前后端分离关键前后端一般通过json数据的相互转换来实现分离一、JSONJSON(JavaScriptObjectNotation,JS对象标记)是一种轻量级的数据交换格式,目前使用特别广......
  • @vue/cli 插件开发之自动根据目录列表生成别名配置
    1.相关文档@vue/cli官方文档@vue/cli@vue/cli插件开发指南@vue/cli2.插件命名规范为了让一个CLI插件在VueCLI项目中被正常使用,它必须遵循vue-cli-plugin-或......