赞
踩
之前的文章《Graalvm 安装和静态编译(https://blog.csdn.net/penngo/article/details/128006244)》介绍了graalvm的安装和环境配置,普通java应用、swing应用、javafx应用编译为原生的方法。
本文介绍springboot应用编译为原生的方法,graalvm的安装配置参考前文。
GraalVM Native Support版本要求Spring Boot >= 3.0.0-M1。
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image
native-build-toolsMaven插件:https://graalvm.github.io/native-build-tools/latest/index.html
可以在https://start.spring.io/快速生成项目:
pom.xml
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.penngo.prometheus</groupId>
<version>1.0.0</version>
<artifactId>springboot_graalvm_test</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</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>
<configuration>
<mainClass>com.penngo.prometheus.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public/</url>
</repository>
</repositories>
</project>
创建一个简单的springboot例子应用。
Main.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
IndexController.java
import com.penngo.prometheus.component.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
@Controller
public class IndexController {
private static Logger log = LoggerFactory.getLogger(IndexController.class);
@RequestMapping("/index")
@ResponseBody
public Result index() {
return Result.create("测试");
}
}
application.yml
server:
port: 8080
servlet:
encoding:
force: true
charset: UTF-8
enabled: true
tomcat:
uri-encoding: UTF-8
logging:
file:
name: logs/app.log
logback:
rollingpolicy:
max-file-size: 10MB
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} %L - %msg%xEx%n'
file: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} %L - %msg%xEx%n'
GraalVM Native Support支持两种方式编译:编译为docker镜像或原生应用文件。
需要安装docker服务,可以参考文章《docker、docker-compose安装和常用命令https://blog.csdn.net/penngo/article/details/128164507》。
编译为镜像文命令:
mvn -Pnative spring-boot:build-image
启动服务:
docker run --rm -p 8080:8080 docker.io/library/springboot_graalvm_test:1.0.0
需要在本地系统中配置graalvm,可以参考《Graalvm 安装和静态编译(https://blog.csdn.net/penngo/article/details/128006244)》。本文是在win10上编译。
编译命令:
mvn -Pnative clean native:compile
启动服务:
双击springboot_graalvm_test.exe运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。