The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

Spring Cloud Config Serverからのプロパティの読み込み

このガイドでは、Quarkusアプリケーションが実行時に Spring Cloud Config Serverから設定プロパティを読み取る方法を説明します。

前提条件

このガイドを完成させるには、以下が必要です:

  • 約15分

  • IDE

  • JDK 17+がインストールされ、 JAVA_HOME が適切に設定されていること

  • Apache Maven 3.9.6

  • 使用したい場合は、 Quarkus CLI

  • ネイティブ実行可能ファイルをビルドしたい場合、MandrelまたはGraalVM(あるいはネイティブなコンテナビルドを使用する場合はDocker)をインストールし、 適切に設定していること

ソリューション

次項の説明に従って、一歩一歩アプリケーションを作成していくことをお勧めします。

Config Serverの起動

このガイドに必要なコンフィグサーバーを立ち上げるには、 こちらで説明している手順に従ってください。そのプロセスの最終結果は、サーバーにクエリを実行するアプリケーションが a-bootiful-client という名前の場合、 message という名前の設定プロパティに Hello world 値を提供する実行中の設定サーバーです。

Mavenプロジェクトの作成

まず、新しいプロジェクトが必要です。以下のコマンドで新規プロジェクトを作成します。

コマンドラインインタフェース
quarkus create app org.acme:spring-cloud-config-quickstart \
    --extension='rest,spring-cloud-config-client' \
    --no-code
cd spring-cloud-config-quickstart

Gradleプロジェクトを作成するには、 --gradle または --gradle-kotlin-dsl オプションを追加します。

Quarkus CLIのインストールと使用方法の詳細については、 Quarkus CLI ガイドを参照してください。

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

Gradleプロジェクトを作成するには、 -DbuildTool=gradle または -DbuildTool=gradle-kotlin-dsl オプションを追加します。

Windowsユーザーの場合:

  • cmdを使用する場合、(バックスラッシュ \ を使用せず、すべてを同じ行に書かないでください)。

  • Powershellを使用する場合は、 -D パラメータを二重引用符で囲んでください。例: "-DprojectArtifactId=spring-cloud-config-quickstart"

このコマンドは、spring-cloud-config-client エクステンションをインポートするプロジェクトを生成します。

すでにQuarkusプロジェクトが設定されている場合は、プロジェクトのベースディレクトリで以下のコマンドを実行することで、 spring-cloud-config-client エクステンションをプロジェクトに追加することができます。

コマンドラインインタフェース
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'

これにより、以下がビルドファイルに追加されます。

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

まず、 src/main/java/org/acme/spring/cloud/config/client/GreetingResource.java ファイルに、以下のようなシンプルな GreetingResource Jakarta REST リソースを作成します:

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";
    }
}

Config Serverから取得した設定プロパティーを使用したいので、 GreetingResource を更新して message プロパティーを注入します。更新したコードは以下のようになります。

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;
    }
}

アプリケーションの設定

Quarkusは、 quarkus.spring-cloud-config ルートで、さまざまな設定ノブを提供します。このガイドでは、Quarkusアプリケーションは、 application.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

アプリケーションをパッケージ化して実行する

アプリケーションを実行します。

コマンドラインインタフェース
quarkus dev
Maven
./mvnw quarkus:dev
Gradle
./gradlew --console=plain quarkusDev

ブラウザで http://localhost:8080/greeting を開きます。

結果は、Spring Cloud Configサーバから取得した値であるため、 Hello world となるはずです。

アプリケーションをネイティブ実行可能ファイルとして実行する

もちろん、 ネイティブ実行可能ファイルのビルドの手順でネイティブイメージを作成することもできます。

Spring Cloud Config Client Reference

ビルド時に固定される構成プロパティ - 他のすべての構成プロパティは実行時にオーバーライド可能

Configuration property

デフォルト

If enabled, will try to read the configuration from a Spring Cloud Config Server

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_ENABLED

Show more

boolean

false

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

Show more

boolean

false

The Base URI where the Spring Cloud Config Server is available

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_URL

Show more

string

http://localhost:8888

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

Show more

string

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

Show more

Duration

10S

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

Show more

Duration

60S

The username to be used if the Config Server has BASIC Auth enabled

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_USERNAME

Show more

string

The password to be used if the Config Server has BASIC Auth enabled

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_PASSWORD

Show more

string

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

Show more

path

Password of TrustStore to be used containing the SSL certificate used by the Config server

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_TRUST_STORE_PASSWORD

Show more

string

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

Show more

path

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

Show more

string

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

Show more

string

When using HTTPS and no keyStore has been specified, whether to trust all certificates

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_TRUST_CERTS

Show more

boolean

${quarkus.tls.trust-all:false}

The profiles to use for lookup

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_PROFILES

Show more

list of string

Custom headers to pass the Spring Cloud Config Server when performing the HTTP request

Environment variable: QUARKUS_SPRING_CLOUD_CONFIG_HEADERS

Show more

Map<String,String>

期間フォーマットについて

To write duration values, use the standard java.time.Duration format. See the Duration#parse() Java API documentation for more information.

数字で始まる簡略化した書式を使うこともできます:

  • 数値のみの場合は、秒単位の時間を表します。

  • 数値の後に ms が続く場合は、ミリ秒単位の時間を表します。

その他の場合は、簡略化されたフォーマットが解析のために java.time.Duration フォーマットに変換されます:

  • 数値の後に hms が続く場合は、その前に PT が付けられます。

  • 数値の後に d が続く場合は、その前に P が付けられます。

関連コンテンツ