当前位置:   article > 正文

springboot学习(八十六) springboot使用graalvm编译native程序

springboot学习(八十六) springboot使用graalvm编译native程序

一、windows环境下

1.下载graalvm的jdk

https://injdk.cn/
下载windows版本

配置java环境变量,配置过程略

2.下载visual Studio Build Tools

下载地址:https://aka.ms/vs/17/release/vs_BuildTools.exe

安装后选择组件:
在这里插入图片描述
其中windows SDK根据实际情况选择

3.编写springboot代码

3.1 maven代码

pox.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/>
<!--  lookup parent from repository  -->
</parent>
<groupId>org.example</groupId>
<artifactId>native-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>native-demo</name>
<description>native-demo</description>
<properties>
<java.version>21</java.version>
</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>
<repositories>

<repository>
<id>aliyun</id>
<name>aliyun Repository</name>
<url>https://maven.aliyun.com/repository/public/</url>
</repository>
<repository>
<id>central-repos</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<name>aliyun Repository</name>
<url>https://maven.aliyun.com/repository/public/</url>
</pluginRepository>
<pluginRepository>
<id>central-repos</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
<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>
</plugin>
</plugins>
</build>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

启动类:

package org.example.nativedemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class NativeDemoApplication {

    @GetMapping
    public String test() {
        return "success";
    }

    public static void main(String[] args) {
        SpringApplication.run(NativeDemoApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.2 gradle代码

gradle.build:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.3'
    id 'io.spring.dependency-management' version '1.1.4'
    id 'org.graalvm.buildtools.native' version '0.9.28'
}

group = 'org.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '21'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

启动类

package org.example.demonativegradle;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoNativeGradleApplication {

    @GetMapping
    public String test() {
        return "success";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoNativeGradleApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.打包

4.1 maven打包

配置maven环境变量(略)

打包:
使用x64 native tools打开命令行
在这里插入图片描述

进入项目目录在,执行命令

mvn -Pnative native:compile
  • 1

打包的二进制包在target下

4.2 gradle打包

使用x64 native tools打开命令行
在这里插入图片描述

进入项目目录在,执行命令

gradle nativeCompile
  • 1

打包的二进制包在build/native/nativeComplie下

二、linux环境下

1.下载graalvm的jdk

https://injdk.cn/
下载linux以及对应架构的版本

配置java环境变量,配置过程略

2.安装依赖库

yum install -y gcc glibc-devel zlib-devel
  • 1

3.编写springboot代码

与windows下的测试代码一致

4.打包

4.1 maven打包

配置maven环境变量

进入项目目录在,执行命令

mvn -Pnative native:compile
  • 1

打包的二进制包在target下

4.2 gradle打包

进入项目目录在,执行命令

gradle nativeCompile
  • 1

打包的二进制包在build/native/nativeComplie下

三、反射、序列化等问题处理

工程中如果有反射、序列化等,打的二进制包运行时会出问题,需要进行处理。

首先将springboot项目打jar包(gradle中bootJar,maven中repackage或install等其他方式)

配置graalvm 的jdk环境变量运行jar包

java -Dspring.aot.enabled=true -agentlib:native-image-agent=config-output-dir=./config -jar target/<jar包名>
  • 1

jar包启动后,尽量在页面或其他测试手段测试所有可能得功能,control+c停止执行后会在jar包同级目录的config重生成一些配置文件,将这些配置文件拷贝到项目的resource/META-INF/native-image下

再执行native打包即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/239522
推荐阅读
相关标签
  

闽ICP备14008679号