# 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.

## Prerequisites

To complete this guide, you need:

- Roughly 15 minutes

- An IDE

- JDK 17+ installed with `JAVA_HOME` configured appropriately

- Apache Maven 3.9.9

- Optionally the Quarkus CLI if you want to use it

- Optionally Mandrel or GraalVM installed and configured appropriately if you want to build a native executable (or Docker if you use a native container build)

## 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:

CLI

    quarkus create app org.acme:spring-cloud-config-quickstart \
        --extension='rest,spring-cloud-config-client' \
        --no-code
    cd spring-cloud-config-quickstart

To create a Gradle project, add the `--gradle` or `--gradle-kotlin-dsl` option.

For more information about how to install and use the Quarkus CLI, see the Quarkus CLI guide.

Maven

    mvn io.quarkus.platform:quarkus-maven-plugin:3.26.0.CR1:create \
        -DprojectGroupId=org.acme \
        -DprojectArtifactId=spring-cloud-config-quickstart \
        -Dextensions='rest,spring-cloud-config-client' \
        -DnoCode
    cd spring-cloud-config-quickstart

To create a Gradle project, add the `-DbuildTool=gradle` or `-DbuildTool=gradle-kotlin-dsl` option.

For Windows users:

- If using cmd, (don’t use backward slash `\` and put everything on the same line)

- If using Powershell, wrap `-D` parameters in double quotes e.g. `"-DprojectArtifactId=spring-cloud-config-quickstart"`

This command generates a project which 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:

CLI

    quarkus extension add spring-cloud-config-client

Maven

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

Gradle

    ./gradlew addExtension --extensions='spring-cloud-config-client'

This will add the following to your build file:

pom.xml

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-spring-cloud-config-client</artifactId>
    </dependency>

build.gradle

    implementation("io.quarkus:quarkus-spring-cloud-config-client")

## GreetingController

First, create a simple `GreetingResource` Jakarta REST resource in the `src/main/java/org/acme/spring/cloud/config/client/GreetingResource.java` file that looks like:

    package org.acme.spring.spring.cloud.config.client;

    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.Produces;
    import jakarta.ws.rs.core.MediaType;

    @Path("/hello")
    public class GreetingResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
            return "hello";
        }
    }

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:

    package org.acme.spring.spring.cloud.config.client;

    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.Produces;
    import jakarta.ws.rs.core.MediaType;

    import org.eclipse.microprofile.config.inject.ConfigProperty;

    @Path("/hello")
    public class GreetingResource {

        @ConfigProperty(name = "message", defaultValue="hello default")
        String message;

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
            return message;
        }
    }

## 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:

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

| If you are using Gradle, the Gradle setting `rootProject.name` has precedence over `quarkus.application.name` so be sure to set the Gradle property to the application name you want the Spring Cloud Config server to see.

## Package and run the application

Run the application with:

CLI

    quarkus dev

Maven

    ./mvnw quarkus:dev

Gradle

    ./gradlew --console=plain quarkusDev

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:

- Quarkus - Extension for Spring DI

- Quarkus - Extension for Spring Web

- Quarkus - Extension for Spring Data JPA

- Quarkus - Extension for Spring Data REST

- Quarkus - Extension for Spring Security

- Quarkus - Extension for Spring Boot properties

- Quarkus - Extension for Spring Cache

- Quarkus - Extension for Spring Scheduled

## Spring Cloud Config Client Reference

Configuration property fixed at build time - All other configuration properties are overridable at runtime

Configuration property | Type | Default
--- | --- | ---
`quarkus.spring-cloud-config.enabled` If enabled, will try to read the configuration from a Spring Cloud Config Server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_ENABLED` | boolean | `false`
`quarkus.spring-cloud-config.fail-fast` If set to true, the application will not stand up if it cannot obtain configuration from the Config Server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_FAIL_FAST` | boolean | `false`
`quarkus.spring-cloud-config.url` The Base URI where the Spring Cloud Config Server is available Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_URL` | string | `http://localhost:8888`
`quarkus.spring-cloud-config.name` Name of the application on Spring Cloud Config server. Could be a list of names to load multiple files (value separated by a comma) Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_NAME` | string | `${quarkus.application.name:}`
`quarkus.spring-cloud-config.label` The label to be used to pull remote configuration properties. The default is set on the Spring Cloud Config Server (generally "master" when the server uses a Git backend). Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_LABEL` | string |
`quarkus.spring-cloud-config.connection-timeout` The amount of time to wait when initially establishing a connection before giving up and timing out. Specify `0` to wait indefinitely. Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_CONNECTION_TIMEOUT` | Duration | `10S`
`quarkus.spring-cloud-config.read-timeout` The amount of time to wait for a read on a socket before an exception is thrown. Specify `0` to wait indefinitely. Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_READ_TIMEOUT` | Duration | `60S`
`quarkus.spring-cloud-config.username` The username to be used if the Config Server has BASIC Auth enabled Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_USERNAME` | string |
`quarkus.spring-cloud-config.password` The password to be used if the Config Server has BASIC Auth enabled Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_PASSWORD` | string |
`quarkus.spring-cloud-config.trust-store` TrustStore to be used containing the SSL certificate used by the Config server Can be either a classpath resource or a file system path Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_TRUST_STORE` | path |
`quarkus.spring-cloud-config.trust-store-password` Password of TrustStore to be used containing the SSL certificate used by the Config server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_TRUST_STORE_PASSWORD` | string |
`quarkus.spring-cloud-config.key-store` KeyStore to be used containing the SSL certificate for authentication with the Config server Can be either a classpath resource or a file system path Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_KEY_STORE` | path |
`quarkus.spring-cloud-config.key-store-password` Password of KeyStore to be used containing the SSL certificate for authentication with the Config server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_KEY_STORE_PASSWORD` | string |
`quarkus.spring-cloud-config.key-password` Password to recover key from KeyStore for SSL client authentication with the Config server If no value is provided, the key-store-password will be used Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_KEY_PASSWORD` | string |
`quarkus.spring-cloud-config.trust-certs` When using HTTPS and no keyStore has been specified, whether to trust all certificates Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_TRUST_CERTS` | boolean | `${quarkus.tls.trust-all:false}`
`quarkus.spring-cloud-config.headers."header-name"` Custom headers to pass the Spring Cloud Config Server when performing the HTTP request Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_HEADERS__HEADER_NAME_` | Map<String,String> |
`quarkus.spring-cloud-config.profiles` The profiles to use for lookup Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_PROFILES` | list of string |
`quarkus.spring-cloud-config.ordinal` Microprofile Config ordinal. Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_ORDINAL` | int | `450`
`quarkus.spring-cloud-config.discovery.enabled` Enable discovery of the Spring Cloud Config Server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_DISCOVERY_ENABLED` | boolean | `false`
`quarkus.spring-cloud-config.discovery.service-id` The service ID to use when discovering the Spring Cloud Config Server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_DISCOVERY_SERVICE_ID` | string |
`quarkus.spring-cloud-config.discovery.eureka-config.service-url."service-url"` The service URL to use to specify Eureka server Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_DISCOVERY_EUREKA_CONFIG_SERVICE_URL__SERVICE_URL_` | Map<String,String> |
`quarkus.spring-cloud-config.discovery.eureka-config.registry-fetch-interval-seconds` Indicates how often(in seconds) to fetch the registry information from the eureka server. Environment variable: `QUARKUS_SPRING_CLOUD_CONFIG_DISCOVERY_EUREKA_CONFIG_REGISTRY_FETCH_INTERVAL_SECONDS` | Duration | `30S`

| About the Duration format To write duration values, use the standard `java.time.Duration` format. See the Duration#parse() Java API documentation for more information. You can also use a simplified format, starting with a number: If the value is only a number, it represents time in seconds. If the value is a number followed by `ms`, it represents time in milliseconds. In other cases, the simplified format is translated to the `java.time.Duration` format for parsing: If the value is a number followed by `h`, `m`, or `s`, it is prefixed with `PT`. If the value is a number followed by `d`, it is prefixed with `P`.