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:
- Flexibility: Easily adjust settings for different environments (dev, test, prod) without modifying code or configuration files.
- Security: Store sensitive data (passwords, API keys) outside your codebase, reducing security risks.
- Containerization: Seamlessly integrate with container orchestration platforms like Kubernetes, where environment variables are a standard configuration mechanism.
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:
- Convert the property name to uppercase.
- Replace dots (
.) with underscores (_).
For example:
quarkus.http.portbecomesQUARKUS_HTTP_PORTmyapp.database.urlbecomesMYAPP_DATABASE_URL
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
- .env Files: While Quarkus supports .env files, remember that environment variables defined in .env are not accessible via System.getenv(String).
- Security: Avoid committing .env files containing sensitive information to version control.
- Deployment: Environment variables are ideal for containerized deployments, enabling dynamic configuration adjustments without rebuilding images. Conclusion Environment variables provide a robust and flexible way to configure your Quarkus applications. By understanding the naming conventions and priority rules, you can effectively manage application settings across different environments, enhancing security and simplifying deployment.