How dev mode differs from a production application

此文档解释了 Quarkus 中的 dev 模式与生产应用程序有何不同。

This document explains how the dev mode in Quarkus differs from a production application.

Intro

Quarkus 提供了一个 dev 模式(在 herehere 中更详细地解释),在开发期间极大地帮助了开发,但它 NEVER 不应用于生产。

Quarkus provides a dev mode (explained in more detail here and here) which greatly aids during development but should NEVER be used in production.

Architectural differences

撇开功能集,在 dev 模式下运行的 Quarkus 应用程序在架构上不同于生产应用程序(即使用 java -jar …​ 运行的应用程序)。

Feature sets aside, the Quarkus application that is run under dev mode differs architecturally from the production application (i.e. the one that is run using java -jar …​).

在 dev 模式下,Quarkus 使用类加载器层次结构(在 here 中详细解释),该层次结构允许开发者无需重建和重新启动应用程序即可实时重新加载用户代码。

In dev mode, Quarkus uses a ClassLoader hierarchy (explained in detail here) that enables the live reload of user code without requiring a rebuild and restart of the application.

在生产应用程序中,上述类加载基础设施完全不存在——只有一个专门构建的类加载器加载(几乎)所有类和依赖项。

In a production application, the aforementioned class loading infrastructure is entirely absent - there is a single, purpose built ClassLoader that loads (almost) all classes and dependencies.

Dev-mode features

为了秉持提供开发者乐趣的理念,Quarkus 在启用 dev 模式时提供了许多功能。最重要的功能有:

In keeping with the mantra of providing developer joy, Quarkus provides a host of features when dev mode is enabled. The most important features are:

Live reload

这个极其重要的功能无需介绍,已在 architectural differences 段落中提及。

This mightily important feature needs no introduction and has already been mentioned in the architectural-differences section.

Dev UI

Quarkus 提供了一个非常实用的 UI,可通过浏览器访问 /q/dev-ui。此 UI 允许开发者查看应用程序的状态,还可以访问各种可更改该状态的操作(取决于存在的扩展)。此类操作的示例包括:

Quarkus provides a very useful UI accessible from the browser at /q/dev-ui. This UI allows a developer to see the state of the application, but also provides access to various actions that can change that state (depending on the extensions that are present). Examples of such operations are:

  • Changing configuration values

  • Running Database migration scripts

  • Clearing of caches

  • Running scheduled operations

  • Building a container

Quarkus 3.x 中已实现了一个新的 Dev UI。目前尚未提供所有功能。您仍然可以使用以下方法访问 Dev UI 的旧版本:[role="bare"][role="bare"]http://localhost:8080/q/dev-ui/.

A new Dev UI has been implemented in Quarkus 3.x. Not all the features are available yet. You can still access the previous version of the Dev UI using: [role="bare"]http://localhost:8080/q/dev-ui/.

Error pages

为了使开发错误变得非常容易诊断,Quarkus 在 dev 模式下运行时提供了各种详细的错误页面。

In an effort to make development errors very easy to diagnose, Quarkus provides various detailed error pages when running in dev mode.

Database import scripts

当 Quarkus 在 dev 模式下运行时,quarkus-hibernate-orm 扩展将在 src/main/resources 中运行 import.sql 脚本。可以在 here 中找到更多详细信息。

The quarkus-hibernate-orm extension will run the import.sql script in src/main/resources when Quarkus is running in dev mode. More details can be found here.

Dev Services

在 dev 模式下测试或运行时,Quarkus 甚至可以立即为您提供零配置数据库,这是我们称为开发人员服务的功能。可以在 here 中找到更多信息。

When testing or running in dev mode Quarkus can even provide you with a zero config database out of the box, a feature we refer to as Dev Services. More information can be found here.

Swagger UI

当 Quarkus 在 dev 模式下运行时,quarkus-smallrye-openapi 扩展将公开 Swagger UI。可以在 here 中找到更多信息。

The quarkus-smallrye-openapi extension will expose the Swagger UI when Quarkus is running in dev mode. Additional information can be found here.

GraphQL UI

当 Quarkus 在 dev 模式下运行时,quarkus-smallrye-graphql 扩展将公开 GraphQL UI。可以在 here 中找到更多详细信息。

The quarkus-smallrye-graphql extension will expose the GraphQL UI when Quarkus is running in dev mode. More details can be found here.

Health UI

使用 Gemini 在 dev 模式下运行 Quarkus 时,quarkus-smallrye-health 扩展将展示运行状况 UI。This 部分提供了其他信息。

The quarkus-smallrye-health extension will expose the Health UI when Quarkus is running in dev mode. This section provides additional information.

Mock mailer

在 dev 模式下运行 Quarkus 时,quarkus-mailer 扩展将启用内存模拟邮件服务器。有关详细信息,请参阅 this

The quarkus-mailer extension will enable an in-memory mock mail server when Quarkus is running in dev mode. See this for more details.

gRPC

  • The gRPC Reflection Service is enabled in dev mode by default. That lets you use tools such as grpcurl. In production mode, the reflection service is disabled. You can enable it explicitly using quarkus.grpc-server.enable-reflection-service=true.

  • In dev mode, quarkus.grpc.server.instances has no effect.

Others

可能有其他一些配置属性(取决于添加到应用程序中的扩展)在 dev 模式下无效。

There might be other configuration properties (depending on the extensions added to the application) that have no effect in dev mode.

Performance implications

在 dev 模式下,最大程度减少应用程序的运行时占用空间并非主要目标(尽管 Quarkus 仍然启动速度很快且占用内存少),主要目标是增强开发人员的喜悦感。因此,每次进行实时重新加载时,将加载更多类,并进行构建时操作。

In dev mode, minimizing the runtime footprint of the application is not the primary objective (although Quarkus still starts plenty fast and consumes little memory) - the primary objective is enabling developer joy. Therefore, many more classes are loaded and build time operations also take place every time a live-reload is performed.

相比之下,在生产应用程序中,Quarkus 的主要目标是用最少量的内存并尽可能快地启动。因此,在运行生产应用程序时,不会执行生成时操作(根据定义),并且生成时所需的各种基础结构类在运行时根本不存在。此外,fast-jar 包类型附带的专用 ClassLoader 可确保以尽可能快的速度进行类查找,同时还将内存中的 JAR 文件数保持在最少。

In contrast, in a production application the main objective for Quarkus is to consume the least amount of memory and startup in the smallest amount of time. Thus, when running the production application, build time operations are not performed (by definition) and various infrastructure classes needed at build time are not present at all at runtime. Furthermore, the purpose built ClassLoader that comes with the fast-jar package type ensures that class lookup is done as fast as possible while also keeping the minimum amount of jars in memory.

由于优化性能并不是 dev 模式的目标,为了加快启动速度,在 dev 模式下禁用 JVM 的 C2 编译器。

Since optimal performance is never an objective of dev mode, in the interest of improving startup time, the JVM’s C2 compiler is disabled in dev mode.

Security implications

dev 模式应用程序不应该在生产中运行最重要的原因可能是,dev 模式允许读取机密信息(通过 Dev-UI),同时允许访问可能具有破坏性的操作(通过公开生产应用程序中不应该提供的端点或通过 Dev-UI)。

Perhaps the most important reason why dev mode applications should not be run in production is that the dev mode allows reading information that could be confidential (via the Dev-UI) while also giving access to operations that could be destructive (either by exposing endpoints that should not be available in production application or via the Dev-UI).

Native executable

当创建本机可执行文件时(在 here 中详细说明),它是 always 从生产应用程序构建的。

When a native executable is created (explained in detail here), it is always built from a production application.