Spring Native:GraalVM native images入门

news/2024/7/10 3:03:54 标签: java, spring boot, spring, vue, mysql

Spring Native项目支持使用GraalVM将Spring应用程序打包为原生镜像。

与JVM可执行文件相比,原生镜像具有更快的启动时间( <100ms )和更低的内存消耗。但是,构建原生镜像比基于JVM的映像需要更多的时间。

该项目仍处于测试阶段,但已经支持大多数Spring组合模块,包括Spring Framework,Spring Boot, Spring Security, 和 Spring Cloud。

它的特性使它非常适合使用Spring Cloud功能构建无服务器 serverless 应用程序,并将其部署到Azure Functions、AWS Lambda或Knative等平台。

本文将指导您构建一个Spring Boot应用程序,该应用程序使用Spring native编译为本机可执行文件。您可以在GitHub上找到源代码: https://github.com/ThomasVitale/spring-tutorials/tree/master/spring-native-graalvm

使用Spring Native引导应用程序

当从spring initializer引导项目时( https://start.spring.io/ ),可以将Spring Native添加到应用程序中。

生成的项目将包含对SpringNative项目和Spring AOT插件的依赖,Spring AOT插件用于将应用程序源代码编译为本机可执行文件,同时提高兼容性和占用空间。

build.gradle 文件如下所示。

plugins {
	id 'org.springframework.boot' version '2.4.3'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
	id 'org.springframework.experimental.aot' version '0.9.0'
}

group = 'com.thomasvitale'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	maven { url 'https://repo.spring.io/release' }
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-webflux'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'io.projectreactor:reactor-test'
}

test {
	useJUnitPlatform()
}

bootBuildImage {
	builder = 'paketobuildpacks/builder:tiny'
	environment = ['BP_NATIVE_IMAGE': 'true']
}

您可以使用熟悉的Spring Boot插件(Gradle或Maven)及其嵌入式云本地Buildpacks支持,从应用程序构建本机映像。JVM和GraalVM映像之间的选择由bootBuildImage任务中的 BP_NATIVE_IMAGE 属性定义,该属性在使用Spring Initialzr时预先配置。

用Spring WebFlux定义REST端点

让我们用Spring WebFlux定义一个REST端点,以便测试应用程序。

在Initializer生成的 SpringNativeGraalvmApplication 类中,可以使用路由器函数或 @RestController 注释类添加REST端点。我们还是用前一种方法吧。

package com.thomasvitale.demo;

import reactor.core.publisher.Mono;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@SpringBootApplication
public class SpringNativeGraalvmApplication {

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

	@Bean
	RouterFunction<ServerResponse> routes() {
		return route()
				.GET("/", request -> ok().body(Mono.just("Spring Native and Beyond!"), String.class))
				.build();
	}
}

然后,在Initializer生成的 SpringNativeGraalVmaApplicationTests 类中,可以为REST端点添加集成测试。

package com.thomasvitale.demo;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
class SpringNativeGraalvmApplicationTests {

	@Autowired
	private WebTestClient webClient;

	@Test
	void whenGetBooksThenReturn() {
		webClient
				.get().uri("/")
				.exchange()
				.expectStatus().is2xxSuccessful()
				.expectBody(String.class).isEqualTo("Spring Native and Beyond!");
	}
}

将应用程序作为可执行JAR运行

项目中的Spring本机依赖项将优化应用程序启动时间和内存消耗,这要感谢Spring AOT插件,即使它作为可执行JAR运行。让我们试试看。

首先,打开一个终端窗口,导航到项目的根文件夹,然后运行以下命令。

$ ./gradlew bootRun

如果没有Spring AOT,应用程序的启动速度将比相应的版本更快。让我们尝试调用REST端点。

$ curl http://localhost:8080
Spring Native and Beyond!

将应用程序作为本机映像运行

现在,让我们尝试使用Spring native和GraalVM构建并运行一个本机映像。

使用Spring Boot插件构建本机映像非常简单。确保Docker引擎正在运行,然后执行以下命令。请注意,这将需要几分钟,很大程度上取决于你的笔记本电脑的CPU和内存特性。

$ ./gradlew bootBuildImage

结果将是一个 spring-native-graalvm:0.0.1-SNAPSHOT 的镜像你可以和Docker一起跑。

$ docker run --name spring-native-graalvm -p 8080:8080 spring-native-graalvm:0.0.1-SNAPSHOT

使用Spring Native的应用程序通常在不到100ms的时间内启动,这取决于您机器的可用资源。

再次调用REST端点,以确保应用程序作为本机映像运行时仍能正常工作。

$ curl http://localhost:8080
Spring Native and Beyond!

结论

在本文中,我介绍了如何快速引导Spring Boot应用程序,并使用Spring native和GraalVM将其编译为本机可执行文件。


http://www.niftyadmin.cn/n/1227457.html

相关文章

python安装模块失败_Python安装模块出错(ImportError: No module named setuptools)解决方法...

Python第三方模块中一般会自带setup.py文件&#xff0c;在Windows环境下&#xff0c;我们只需要使用命令cd c:\Temp\foopython setup.py install两个命令就可以完成第三方模块的安装了。第一个cd命令将当前目前切换到待安装的第三方模块的目录下(这里假设第三方模块解压后的目录…

一文教会你使用Knative运行Spring Boot项目

在本文中&#xff0c;我将解释什么是Knative以及如何将它与Spring Boot一起使用。尽管Knative是一个 serverless 无服务器平台&#xff0c;但我们可以在那里运行 任何类型的应用程序 &#xff08;不仅仅是函数&#xff09;。因此&#xff0c;我们将在那里运行一个标准的Spring …

java怎么把随机数放入数组_Java面试整理-基础篇8.集合1

1.Java中常见的集合及其关系&#xff1f;2.ArrayList、LinkedList、Vector的区别&#xff1f;LinkedList、ArrayList、Vector都是List接口的子类&#xff1b;LinkedList基于双向链表实现&#xff0c;ArrayList、Vector基于数组实现。ArrayList采用懒加载模式&#xff0c;在第一…

2018网易校招内推编程题全解

1.小易有一些彩色的砖块。每种颜色由一个大写字母表示。各个颜色砖块看起来都完全一样。现在有一个给定的字符串s,s中每个字符代表小易的某个砖块的颜色。小易想把他所有的砖块排成一行。如果最多存在一对不同颜色的相邻砖块,那么这行砖块就很漂亮的。请你帮助小易计算有多少种…

一文告诉你SOA和微服务的各自特点是什么?

什么是SOA&#xff1f; SOA是计算机软件设计中的一种体系结构模式。在这种类型的应用程序中&#xff0c;组件通过通信协议&#xff08;通常通过网络&#xff09;向其他组件提供服务。面向服务的原则独立于任何产品、供应商或技术。SOA的完整形式是面向服务的体系结构 SOA使各…

python itertools_python itertools 用法

1、介绍itertools 是python的迭代器模块&#xff0c;itertools提供的工具相当高效且节省内存。使用这些工具&#xff0c;你将能够创建自己定制的迭代器用于高效率的循环。- 无限迭代器itertools包自带了三个可以无限迭代的迭代器。这意味着&#xff0c;当你使用他们时&#xff…

python:基于wxpy微信聊天机器人

之前有听说过wxpy,但一直没有使用过&#xff0c;今天花了半个小时研究了一下&#xff0c;挺有意思。。。 首先&#xff0c;大家可以参考http://wxpy.readthedocs.io/zh/latest/&#xff0c;有中文的文档可以参考&#xff0c;pip安装wxpy&#xff0c;我是在linux环境&#xff0…

如何将现有的Spring框架应用程序迁移到SpringBoot应用程序

在本文中&#xff0c;我们将研究如何将现有的Spring框架应用程序迁移到SpringBoot应用程序。 Spring Boot并不是为了取代Spring&#xff0c;而是为了使使用它更快更容易。因此&#xff0c;迁移应用程序所需的大多数更改都与配置有关。在大多数情况下&#xff0c;我们的自定义控…