Skip to content
SimplyMe
Go back

Quarkus Configuration: Leveraging Environment Variables

Edit page

Quarkus, known for its supersonic subatomic Java, provides a flexible configuration system that allows you to manage application settings from various sources. One powerful and often overlooked method is using environment variables. Let’s dive into how you can harness the power of environment variables to configure your Quarkus applications.

Quarkus, known for its supersonic subatomic Java, provides a flexible configuration system that allows you to manage application settings from various sources. One powerful and often overlooked method is using environment variables. Let’s dive into how you can harness the power of environment variables to configure your Quarkus applications.

Why Environment Variables?

Environment variables offer several advantages:

How Quarkus Handles Environment Variables

Quarkus prioritizes configuration sources, and environment variables hold a high precedence. This means that if a configuration property is defined both in your application.yaml (or application.properties) and as an environment variable, the environment variable’s value will take effect.

Naming Conventions

Quarkus uses a simple naming convention to map environment variables to configuration properties:

For example:

Practical Example

Consider the following application.yaml:

quarkus:
  http:
    port: 8080
  datasource:
    url: jdbc:h2:mem:testdb

To override the HTTP port and database URL using environment variables, you would set: export QUARKUS_HTTP_PORT=9000 export QUARKUS_DATASOURCE_URL=jdbc:postgresql://localhost:5432/mydb

Now, when your Quarkus application starts, it will use port 9000 and the PostgreSQL database URL. Accessing Configuration You can access these configuration properties within your Quarkus application using the @ConfigProperty annotation:

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/config")
public class ConfigResource {
    @Inject
    @ConfigProperty(name = "quarkus.http.port")
    String httpPort;
    @Inject
    @ConfigProperty(name = "quarkus.datasource.url")
    String datasourceUrl;
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getConfig() {
        return "HTTP Port: " + httpPort + "\nDatasource URL: " + datasourceUrl;
    }
}

Important Considerations


Edit page
Share this post on:

Previous Post
Kubernetes ConfigMaps: Centralized Configuration for Your Applications
Next Post
Visualizing Your API with PlantUML: A Diagrammatic Approach