因为我的操作系统是macOS,所以下面的步骤在mac上安装

安装GraalVM

  1. 下载macOS上的GraalVM安装包
    https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-21.1.0
    选择mac版本,我选择的是jdk11的的版本
    下载完成后 本地会有
    graalvm-ce-java11-darwin-amd64-21.1.0.tar.gz
  2. 安装
    参考 https://www.graalvm.org/docs/getting-started/macos/
  3. 安装 native-imag
    在GraalVM虚拟机目录/bin目录下,
    /gu install native-image
    注意,可能需要fq
  4. 使用https://start.springboot.io/ 生成spring-native源代码
  5. mvn -Pnative -DskipTests packag
  1. 启动target目前下的native文件
2021-06-15 19:47:17.766  INFO 72802 --- [           main] o.s.nativex.NativeListener               : This application is bootstrapped with code generated with Spring AOT

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

2021-06-15 19:47:17.770  INFO 72802 --- [           main] c.z.s.SpringnativeApplication            : Starting SpringnativeApplication v0.0.1-SNAPSHOT using Java 11.0.11 on zhkdeMacBook-Pro.local with PID 72802 (/Users/zhk/workspaces/geekbang-lessons/springnative/target/springnative started by zhk in /Users/zhk/workspaces/geekbang-lessons/springnative/target)
2021-06-15 19:47:17.770  INFO 72802 --- [           main] c.z.s.SpringnativeApplication            : No active profile set, falling back to default profiles: default
2021-06-15 19:47:17.813  INFO 72802 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
Jun 15, 2021 7:47:17 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jun 15, 2021 7:47:17 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jun 15, 2021 7:47:17 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.46]
Jun 15, 2021 7:47:17 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
2021-06-15 19:47:17.816  INFO 72802 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 45 ms
Jun 15, 2021 7:47:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
2021-06-15 19:47:17.836  INFO 72802 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-15 19:47:17.837  INFO 72802 --- [           main] c.z.s.SpringnativeApplication            : Started SpringnativeApplication in 0.31 seconds (JVM running for 0.313)
Jun 15, 2021 7:47:43 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-15 19:47:43.768  INFO 72802 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-06-15 19:47:43.769  INFO 72802 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

使用Spring Boot Buildpacks support产生docker镜像

  1. 本机安装docker环境
  2. 使用上面的工程,进入工程目录
  3. mvn spring-boot:build-image mac环境注意给docker分配的内存要大,建议8G
  4. 启动docker docker run –rm -p 8080:8080 springnative:0.0.1-SNAPSHOT
  5. 镜像已经上传到dockerhub
  • docker login
  • docker tag springnative:0.0.1-SNAPSHOT
  • thurstonzk2008/springnative:0.0.1-SNAPSHOT
  • docker push thurstonzk2008/springnative:0.0.1-SNAPSHOT