이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Configuring your Quarkus applications by using a YAML file
Abstract
Chapter 1. Configuring your Quarkus applications by using a YAML file 링크 복사링크가 클립보드에 복사되었습니다!
As an application developer, you can use Red Hat build of Quarkus to create microservices-based applications written in Java that run on OpenShift Container Platform and serverless environments. Applications compiled to native executables have small memory footprints and fast startup times.
Apply structured configuration by updating the application.yaml file to configure your Quarkus application.
Alternatively, you can configure your Quarkus application by setting properties in the application.properties file. For more information, see Setting configuration properties.
The procedures include configuration examples that are created by using the Quarkus config-quickstart exercise.
Prerequisites
You have installed OpenJDK 11 or 17 and set the
JAVA_HOMEenvironment variable to specify the location of the Java SDK.- To download Red Hat build of OpenJDK, log in to the Red Hat Customer Portal and go to Software Downloads.
You installed Apache Maven 3.8.x, where x is 6 or later.
- Download Maven from the Apache Maven Project website.
You have configured Apache Maven to use artifacts from the Quarkus Maven repository.
- To learn how to configure Apache Maven settings, see Getting started with Quarkus.
1.1. Red Hat configuration options 링크 복사링크가 클립보드에 복사되었습니다!
Configuration options enable you to change the settings of your application in a single configuration file. Red Hat build of Quarkus supports configuration profiles that you can use to group related properties and switch between profiles as required.
By default, Quarkus reads properties from the application.properties file located in the src/main/resources directory. You can also configure Quarkus to read properties from a YAML file instead.
When you add the quarkus-config-yaml dependency to your project pom.xml file, you can configure and manage your application properties in the application.yaml file. For more information, see Adding YAML configuration support.
Red Hat build of Quarkus also supports MicroProfile Config, which enables you to load the configuration of your application from other sources.
You can use the MicroProfile Config specification from the Eclipse MicroProfile project to inject configuration properties into your application and access them using a method defined in your code.
Quarkus can also read application properties from different origins, including the following sources:
- The file system
- A database
-
A Kubernetes or OpenShift Container Platform
ConfigMapor Secret object - Any source that can be loaded by a Java application
1.2. Adding YAML configuration support 링크 복사링크가 클립보드에 복사되었습니다!
Red Hat build of Quarkus supports YAML configuration files through the SmallRye Config implementation of Eclipse MicroProfile Config. You can add the Quarkus Config YAML extension and use the YAML configuration file over the properties file for configuration. Quarkus supports the use of application.yml and application.yaml as the name of the YAML file.
The YAML configuration file takes precedence over the application.properties file. To avoid errors, you can delete the application.properties file and use only one type of configuration file.
Procedure
Use one of the following methods to add the YAML extension in your project:
Open the
pom.xmlfile and add thequarkus-config-yamlextension as a dependency:Example
pom.xmlfile<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-config-yaml</artifactId> </dependency><dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-config-yaml</artifactId> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow To add the
quarkus-config-yamlextension from the command line, enter the following command from your project directory:Add
quarkus-config-yamlextension./mvnw quarkus:add-extension -Dextensions="quarkus-config-yaml"
./mvnw quarkus:add-extension -Dextensions="quarkus-config-yaml"Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.2.1. Using nested object configuration with YAML 링크 복사링크가 클립보드에 복사되었습니다!
You can define a nested class inside an already existing class. This procedure shows how you can set nested configuration properties for your Quarkus application by using a configuration file in YAML format.
Prerequisites
- You have a Quarkus Maven project.
- You have a PostgreSQL data source.
You have the following extensions as dependencies in the
pom.xmlfile of your project:-
quarkus-rest-client -
quarkus-jdbc-postgresql -
quarkus-config-yaml
-
Procedure
-
Open the
src/main/resources/application.yamlconfiguration file. Add the nested class configuration properties to your
application.yamlfile as shown in the following example:Example
application.yamlfileCopy to Clipboard Copied! Toggle word wrap Toggle overflow In a similar way to using the comments in the
application.propertiesfile, you can use comments to describe your configuration properties in YAML format.NoteAlways use spaces to indent the properties in your YAML configuration file. YAML does not allow you to use tabs for indentation.
1.2.2. Setting custom configuration profiles with YAML 링크 복사링크가 클립보드에 복사되었습니다!
With Quarkus, you can set configuration properties and values that are specific to different configuration profiles of your application. You can start your application with a specific profile to access a particular configuration. This procedure shows how you can provide a configuration for a specific profile in YAML format.
Prerequisites
- You have a Quarkus Maven project that is configured to use a PostgreSQL data source with a JDBC data source driver
-
You have the
quarkus-jdbc-postgresqlandquarkus-config-yamlextensions as dependencies in thepom.xmlfile of your project
Procedure
-
Open the
src/main/resources/application.yamlconfiguration file. To set a profile dependent configuration, add the profile name before defining the key-value pairs by using the
"%<profile_name>"syntax. Ensure that you place the profile name inside quotation marks.TipIn YAML, all strings that begin with a special character must be placed inside quotation marks.
In the following example, the PostgreSQL database is configured to be available at the
jdbc:postgresql://localhost:5432/some-databaseURL when you start your Quarkus application in development mode:src/main/resources/application.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.3. Property expressions 링크 복사링크가 클립보드에 복사되었습니다!
Property expressions are combinations of property references and plain text strings that you can use to substitute values of properties in your configuration.
Much like a variable, you can use a property expression in Quarkus to substitute a value of a configuration property instead of hardcoding it. A property expression is resolved when java.util.Properties reads the value of the property from a configuration source in your application.
This means that if a configuration property is read from your configuration at compile time, the property expression is also resolved at compile time. If the configuration property is overriden at runtime, its value is resolved at runtime.
You can resolve property expressions by using more than one configuration source. This means that you can use a value of a property that is defined in one configuration source to expand a property expression that you use in another configuration source.
If the value of a property in an expression cannot be resolved, and you do not set a default value for the expression, your application encounters a NoSuchElementException.
1.3.1. Example usage of property expressions using a YAML file 링크 복사링크가 클립보드에 복사되었습니다!
This section shows examples of how you can use property expressions to achieve flexibility when configuring your Quarkus application.
You can reference nested properties by using the . (dot) separator as in {x.factor}.
Example application.yaml file
1.4. External application.yaml file for configuring properties at runtime 링크 복사링크가 클립보드에 복사되었습니다!
To configure your application properties at runtime, add your application.yaml file to the config directory.
The values in the config/application.yaml file, if one exists, override any values from the regular application.yaml file.
Ensure that the config/application.yaml file is in the root of the working directory relative to the Quarkus application runner, as outlined in the following example:
├── config │ └── application.yaml ├── my-app-runner
├── config
│ └── application.yaml
├── my-app-runner
1.5. Managing configuration key conflicts 링크 복사링크가 클립보드에 복사되었습니다!
Structured formats such as YAML only support a subset of the possible configuration namespace. The following procedure shows how to resolve a conflict between two configuration properties, quarkus.http.cors and quarkus.http.cors.methods, where one property is the prefix of another.
Prerequisites
- You have a Quarkus project that is configured to read YAML configuration files.
Procedure
- Open your YAML configuration file.
To define a YAML property as a prefix of another property, add a tilde (
~) in the scope of the property as shown in the following example:Example of defininf a YAML property as a prefix
quarkus: http: cors: ~: true methods: GET,PUT,POSTquarkus: http: cors: ~: true methods: GET,PUT,POSTCopy to Clipboard Copied! Toggle word wrap Toggle overflow To compile your Quarkus application in development mode, enter the following command from the project directory:
Compile your Quarkus application
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow [NOTE
You can use YAML keys for conflicting configuration keys at any level because they are not included in the assembly of configuration property name.