2024年3月27日

Quarkus介绍

作者 高劲松

官方站点

https://quarkus.io/

Quarkus 是另一个采用与上述 Spring Boot 类似方法的框架,但还有一个额外的优点,即以更快的启动时间、更好的资源利用率和效率交付更小的工件(Supersonic、Subatomic)。

它针对云、无服务器和容器化环境进行了优化。尽管侧重点略有不同, Quarkus 也能与最流行的 Java 框架很好地集成。

Quarkus

一套适用于GraalVM和HotSpot的开源技术,用于编写Java应用程序。它提供(承诺)超快的启动时间和更低的内存占用。这使其成为容器和无服务器工作负载的理想选择。它使用 Eclipse Microprofile(JAX-RS、CDI、JSON-P)来构建微服务,这是 Java EE 的一个子集。

GraalVM 是一个通用的多语言虚拟机(JavaScript、Python、Ruby、R、Java、Scala、Kotlin)。 GraalVM(特别是 Substrate VM)使提前 (AOT) 编译成为可能,将字节码转换为本机机器码,从而生成可以在本机执行的二进制文件。

请记住,并非每个功能在本机执行中都可用,AOT 编译有其局限性。注意这句话(引用GraalVM团队的话):

我们运行一个积极的静态分析,它需要一个封闭世界的假设,这意味着在运行时可以访问的所有类和所有字节码都必须在构建时是已知的。
因此,例如,反射和 Java 本机接口 (JNI) 将不起作用,至少是开箱即用的(需要一些额外的工作)。您可以在此处找到限制列表本机映像 Java 限制文档。

Spring Boot

不用多介绍了,用一句话说(随意跳过它):Spring Boot 建立在 Spring Framework 之上,是一个开源框架,它提供了一种更简单的方法来构建、配置和运行基于 Java Web 的应用程序。使其成为微服务的良好候选者。

战斗准备 — 创建 Docker 镜像

Quarkus非常适合 Kubernetes 项目!这是redhat为 Kubernetes 提供 Quarkus Java 框架,专门设计用于与 OpenJDK HotSpot 和 GraavalVM 配合使用。该框架提供了命令式和反应式编程模型来解决与微服务架构相关的挑战。

 

Kubernative 原生 Quarkus 框架的创建是为了最大限度地减少内存消耗并最大限度地缩短开发的启动时间(数十毫秒)。快速的启动时间使微服务可以在容器和 Kubernetes 中轻松扩展。

特征:

  • 它提供了各种各样的技术、库和 API,使其易于学习和使用。
  • 使用此平台,您可以提前优化 JVM 和本机代码的代码,以提高应用程序性能。
  • 该框架拥有比其他框架更快的启动时间。

快递开始


Install via Command Line Interface

Open your favorite terminal and use JBang to install the Quarkus CLI. You do not need to have Java installed first.

For Linux, macOS, and Windows (using WSL or bash compatible shell like Cygwin or MinGW)

curl -Ls https://sh.jbang.dev | bash -s - trust add https://repo1.maven.org/maven2/io/quarkus/quarkus-cli/ curl -Ls https://sh.jbang.dev | bash -s - app install --fresh --force quarkus@quarkusio

For Windows using Powershell

iex "& { $(iwr https://ps.jbang.dev) } trust add https://repo1.maven.org/maven2/io/quarkus/quarkus-cli/" iex "& { $(iwr https://ps.jbang.dev) } app install --fresh --force quarkus@quarkusio"

If it’s your first time to install, you’ll need to restart your shell.

Or, you can also install the CLI with SDKMAN!

sdk install quarkus

For more options, such as Homebrew or Chocolatey, see the Quarkus CLI guide.

Step 2

Create the Getting Started Application

Run this script in your CLI:quarkus create && cd code-with-quarkus

Step 3

Run the Getting Started Application

Run this script in your CLI:quarkus dev

Boom! Your Quarkus app is now running at localhost:8080

Step 4

Live Coding with Quarkus

Quarkus makes it easy to change your code on the fly. Let’s modify the RESTful endpoint

Open src/main/java/org/acme/GreetingResource.java in a text editor or your favorite IDE and change “Hello from RESTEasy Reactive” to “Hola from RESTEasy Reactive”. Then refresh the browser and see the changes. @Path("/hello") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello RESTEasy"; } }