This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Configuration Loading Process

Quarkus can obtain the configuration of a project from several places. It reads the configuration priority into the following figure, and once it reads a configuration, it will not continue to read the configuration in the following configuration.

0x1 System Properties

System properties can be passed to applications during startup via the -d flag.

For example, to set the running port of the HTTP service, the modes of passing system parameters are as follows:

  • Quarkus devMode:mvn quarkus:dev -Dquarkus.http.port=8888
  • runjarPackage:java -Dquarkus.http.port=8888 -jar quarkus-run.jar
  • runnative-image:app-runner -Dquarkus.http.port=8888

0x2 Environment variables

The environment variable names follow the MicroProfile Config

Environment Variables Mapping Rules
Some operating systems allow only alphabetic characters or an underscore, _, in environment variables. Other characters such as ., /, etc may be disallowed. In order to set a value for a config property that has a name containing such disallowed characters from an environment variable, the following rules are used.

The ConfigSource for the environment variables searches three environment variables for a given property name (e.g. com.ACME.size):

1. Exact match (i.e. com.ACME.size)

2. Replace each character that is neither alphanumeric nor _ with _ (i.e. com_ACME_size)

3. Replace each character that is neither alphanumeric nor _ with _; then convert the name to upper case (i.e. COM_ACME_SIZE)

The first environment variable that is found is returned by this ConfigSource.
Copy the code

Environment variables are set in different ways for each system. You can check the way your system sets environment variables. General Unix system Settings environment variables are generally divided into command line Settings and environment variable file configuration

  • Command-line configuration:export QUARKUS_HTTP_PORT:8888
  • Configuration file configuration: Environment variable configuration files are divided into user variable configuration files and system variable configuration files. You can add these to the corresponding configuration files, but this is not recommended

0 x3. Env file

Note: Environment variables in the.env file cannot be obtained through the System.getenv(String) API like normal environment variables.

Env files are similar to environment variables, but have a smaller scope. They only apply to the current project, unlike environment variables, which can be applied to all projects.

It is set by configuring key-value pairs in the.env file. Key names follow the same MicroProfile Config specifications as environment variables

Usage:

  • For the dev pattern: Use it at the root of your project, but don’t package it with your code
  • forjarnative-imageRunning mode: Can be.envThe file is in the same directory as the JAR package or native image

0x4 Quarkus Application Configuration file

Like the Spring Boot project, Quarkus supports an application.properties configuration file. The application. Properties configuration file in the config folder in the same directory as the current JAR file and native image file is supported in the jar package and native image running mode. The configuration files in the Config folder take precedence over the configuration files in the project Resources folder

For dev running mode, the project can also use the config file in the config file, manually moving the config folder to the target folder, but using the MVN clean command will clean up this folder. When the time comes again want to manually re-create the config folder and configuration files, so in dev mode is not recommended to use the config/application. The properties

0x5 MicroProfile Configuration file

It’s in SRC /main/resources/ meta-INF /microprofile-config.properties

It works exactly the same as application.properties in the resources folder of your project, and you are advised to use the configuration file in the Resources folder

Use yML configuration files

In addition to the system properties, environment variables, and.env files, the configuration files can support yML configuration, but you need to add additional dependencies

Add the dependent

  1. XML file to add dependencies
<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-config-yaml</artifactId>
</dependency>
Copy the code
  1. Or you can add extended dependencies directly using the Maven command
./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-config-yaml"
Copy the code

Add the YML file

Removal of SRC/main/resources/application. The properties file, add the SRC/main/resources/application. The yaml file

If both files exist, Quarkus prefers to use the configuration from YML and then properties, so to avoid confusion, it is recommended to delete the Properties file.

Yml and YAML configuration file extensions are supported

The resources

  1. Quarkus. IO/guides/conf…
  2. Quarkus. Pro/guides/conf…
  3. Quarkus. IO/guides/conf…