////
This guide is maintained in the main Quarkus repository
and pull requests should be submitted there:
https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc
////
= Reading properties from Spring Cloud Config Server

include::./attributes.adoc[]

This guide explains how your Quarkus application can read configuration properties at runtime from the https://cloud.spring.io/spring-cloud-config[Spring Cloud Config Server].

== Prerequisites

include::includes/devtools/prerequisites.adoc[]

== 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 https://github.com/spring-guides/gs-centralized-configuration#stand-up-a-config-server[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:

:create-app-artifact-id: spring-cloud-config-quickstart
:create-app-extensions: spring-cloud-config-client
include::includes/devtools/create-app.adoc[]

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:

:add-extension-extensions: spring-cloud-config-client
include::includes/devtools/extension-add.adoc[]

This will add the following to your build file:

[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"]
.pom.xml
----
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-spring-cloud-config-client</artifactId>
</dependency>
----

[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"]
.build.gradle
----
implementation("io.quarkus:quarkus-spring-cloud-config-client")
----

== GreetingController

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

[source,java]
----
package org.acme.spring.spring.cloud.config.client;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.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:

[source,java]
----
package org.acme.spring.spring.cloud.config.client;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.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:

[source,properties]
----
# 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
----

== Package and run the application

Run the application with:

include::includes/devtools/dev.adoc[]

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 xref:building-native-image.adoc[Building a native executable guide].

== More Spring guides

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

* xref:spring-di.adoc[Quarkus - Extension for Spring DI]
* xref:spring-web.adoc[Quarkus - Extension for Spring Web]
* xref:spring-data-jpa.adoc[Quarkus - Extension for Spring Data JPA]
* xref:spring-data-rest.adoc[Quarkus - Extension for Spring Data REST]
* xref:spring-security.adoc[Quarkus - Extension for Spring Security]
* xref:spring-boot-properties.adoc[Quarkus - Extension for Spring Boot properties]
* xref:spring-cache.adoc[Quarkus - Extension for Spring Cache]
* xref:spring-scheduled.adoc[Quarkus - Extension for Spring Scheduled]

[[spring-cloud-config-client-configuration-reference]]
== Spring Cloud Config Client Reference

include::{generated-dir}/config/quarkus-spring-cloud-config-client.adoc[leveloffset=+1, opts=optional]

