第 11 章 添加 YAML 配置支持


红帽构建的 Quarkus 通过 Eclipse MicroProfile 配置的 SmallRye Config 实现支持 YAML 配置文件。您可以添加 Quarkus Config YAML 扩展,并使用 YAML over 属性进行配置。Quarkus 支持使用 application.yml 以及 application.yaml 作为 YAML 文件的名称。

YAML 配置文件优先于 application.properties 文件。推荐的方法是删除 application.properties 文件,仅使用一种配置文件来避免错误。

流程

  • 使用以下方法之一在项目中添加 YAML 扩展:

    • 打开 pom.xml 文件,并将 quarkus-config-yaml 扩展添加为依赖项:

      <dependency>
          <groupId>io.quarkus</groupId>
          <artifactId>quarkus-config-yaml</artifactId>
      </dependency>
      Copy to Clipboard Toggle word wrap
    • 要从命令行添加 quarkus-config-yaml 扩展,请在项目目录中输入以下命令:

      ./mvnw quarkus:add-extension -Dextensions="quarkus-config-yaml"
      Copy to Clipboard Toggle word wrap

11.1. 使用带有 YAML 的嵌套对象配置

您可以在已经存在的类中定义嵌套类。此流程演示了如何使用 YAML 格式的配置文件为 Quarkus 应用程序设置嵌套配置属性。

先决条件

  • 具有 Quarkus Maven 项目。
  • 具有 PostgreSQL 数据源。
  • 在项目的 pom.xml 文件中有以下扩展作为依赖项:

    • quarkus-rest-client,
    • quarkus-jdbc-postgresql
    • quarkus-config-yaml

流程

  1. 打开 src/main/resources/application.yaml 配置文件。
  2. application.yaml 文件中添加嵌套类配置属性,如下例所示:

    src/main/resources/application.yaml

    # Properties that configure the JDBC data source driver of your PostgreSQL data source
    quarkus:
      datasource:
        url: jdbc:postgresql://localhost:5432/some-database
        driver: org.postgresql.Driver
        username: quarkus
        password: quarkus
    
    # Property that configures the URL of the endpoint to which the rest client sends requests
    org:
      acme:
        restclient:
          CountriesService/mp-rest/url: https://restcountries.eu/rest
    
    # Property that configures the log message level for your application
    quarkus:
      log:
        category:
          # Do not use spaces in names of configuration properties that you place inside quotation marks
          "io.quarkus.category":
            level: INFO
    Copy to Clipboard Toggle word wrap

    请注意,您可以使用注释来描述配置属性,其方式与在 application.properties 中使用它们类似。

    注意

    始终使用空格在 YAML 配置文件中缩进属性。YAML 不允许使用标签页来缩进。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat