Quarkus - Reading properties from Consul

    To complete this guide, you need:

    • less than 15 minutes

    • an IDE

    • JDK 1.8+ installed with configured appropriately

    • Apache Maven 3.6.2+

    Solution

    Introduction

    Consul is a versatile system which among other things, provides a distributed Key-Value store that is used in many architectures as a source of configuration for services. This Key-Value store is what the quarkus-consul-config extension interacts with in order to allow Quarkus applications to read runtime configuration properties from Consul.

    There are various ways to start Consul that vary in complexity, but for the purposes of this guide, we elect to start a single Consul server with no persistence via Docker, like so:

    Please consult the documentation to learn more about the various Consul installation options.

    Creating the Maven project

    First, we need a new project. Create a new project with the following command:

    1. -DprojectGroupId=org.acme \
    2. -DprojectArtifactId=consul-config-quickstart \
    3. -DclassName="org.acme.consul.config.GreetingResource" \
    4. -Dpath="/greeting" \
    5. -Dextensions="consul-config"
    6. cd consul-config-quickstart

    This command generates a Maven project with a REST endpoint and imports the consul-config extension.

    If you already have your Quarkus project configured, you can add the consul-config extension to your project by running the following command in your project base directory:

    This will add the following to your pom.xml:

    1. <dependency>
    2. <groupId>io.quarkus</groupId>
    3. <artifactId>quarkus-consul-config</artifactId>
    4. </dependency>

    GreetingController

    The Quarkus Maven plugin automatically generated a GreetingResource JAX-RS resource in the file that looks like:

    1. package org.acme.consul.config.client;
    2. import javax.ws.rs.GET;
    3. import javax.ws.rs.Path;
    4. import javax.ws.rs.Produces;
    5. import org.eclipse.microprofile.config.inject.ConfigProperty;
    6. @Path("/hello")
    7. public class GreetingResource {
    8. @ConfigProperty(name = "message", defaultValue="Hello from default")
    9. String message;
    10. @GET
    11. @Produces(MediaType.TEXT_PLAIN)
    12. public String hello() {
    13. return message;
    14. }
    15. }

    Quarkus provides various configuration knobs under the quarkus.consul-config root. For the purposes of this guide, our Quarkus application is going to be configured in application.properties as follows:

    Add Configuration to Consul

    For the previous application configuration to work, we need to add a config/consul-test key under Consul’s Key Value store. The value of this key will essentially be a properties “file” containing the application configuration. In this case we want to add the following data to the config/consul-test key:

      When adding this configuration from the UI, Consul will automatically convert the data into the necessary base64 encoding. If you instead add the configuration via the Consul’s REST API, make sure to first encode the previous data into base64.

      Package and run the application

      Run the application with: ./mvnw compile quarkus:dev. Open your browser to http://localhost:8080/greeting.

      The result should be: Hello from Consul as it is the value obtained from the Consul Key Value store.

      You can of course create a native image using the instructions of the .

      Configuration Reference