Quarkus - Reading properties from Spring Cloud Config Server

This guide explains how your Quarkus application can read configuration properties at runtime from the Spring Cloud Config Server.

This technology is considered preview.

In preview, backward compatibility and presence in the ecosystem is not guaranteed. Specific improvements might require to change configuration or APIs and plans to become stable are under way. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible extension statuses, check our FAQ entry.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.6.2+

Solution

We recommend that you follow the instructions in the next sections and create the application step by step.

Stand up a Config Server

To stand up the Config Server required for this guide, please follow the instructions outlined here. The end result of that process is a running Config Server that will provide the Hello world value for a configuration property named message when the application querying the server is named a-bootiful-client.

Creating the Maven project

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

  1. mvn io.quarkus:quarkus-maven-plugin:1.7.6.Final:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=spring-cloud-config-quickstart \
  4. -DclassName="org.acme.spring.cloud.config.client.GreetingResource" \
  5. -Dpath="/greeting" \
  6. -Dextensions="spring-cloud-config-client"
  7. cd spring-cloud-config-quickstart

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

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

  1. ./mvnw quarkus:add-extension -Dextensions="spring-cloud-config-client"

This will add the following to your pom.xml:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-spring-cloud-config-client</artifactId>
  4. </dependency>

GreetingController

The Quarkus Maven plugin automatically generated a GreetingResource JAX-RS resource in the src/main/java/org/acme/spring/cloud/config/client/GreetingResource.java file that looks like:

  1. package org.acme.spring.spring.cloud.config.client;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6. @Path("/hello")
  7. public class GreetingResource {
  8. @GET
  9. @Produces(MediaType.TEXT_PLAIN)
  10. public String hello() {
  11. return "hello";
  12. }
  13. }

As we want to use configuration properties obtained from the Config Server, we will update the GreetingResource to inject the message property. The updated code will look like this:

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

Configuring the application

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

  1. # use the same name as the application name that was configured when standing up the Config Server
  2. quarkus.application.name=a-bootiful-client
  3. # enable retrieval of configuration from the Config Server - this is off by default
  4. quarkus.spring-cloud-config.enabled=true
  5. # configure the URL where the Config Server listens to HTTP requests - this could have been left out since http://localhost:8888 is the default
  6. quarkus.spring-cloud-config.url=http://localhost:8888

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 world as it is the value obtained from the Spring Cloud Config server.

Run the application as a native executable

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

More Spring guides

Quarkus has more Spring compatibility features. See the following guides for more details:

Spring Cloud Config Client Reference

About the Duration format

The format for durations uses the standard java.time.Duration format. You can learn more about it in the Duration#parse() javadoc.

You can also provide duration values starting with a number. In this case, if the value consists only of a number, the converter treats the value as seconds. Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.