Spring-boot-cloud-configuration-client

提供:Dev Guides
移動先:案内検索

Spring Boot-クラウド構成クライアント

アプリケーションによっては、変更が必要な構成プロパティが必要な場合があり、開発者はそれらを削除するか、これを実行するためにアプリケーションを再起動する必要があります。 ただし、これにより、実稼働環境でのダウンタイムやアプリケーションの再起動が必要になる場合があります。 開発者は、Spring Cloud Configuration Serverを使用して、アプリケーションを再起動したりダウンタイムしたりすることなく、新しい構成プロパティをロードできます。

Spring Cloud Configuration Serverの使用

まず、https://start.spring.io/からSpring Bootプロジェクトをダウンロードし、Spring Cloud Config Client依存関係を選択します。 次に、ビルド構成ファイルにSpring Cloud Starter Config依存関係を追加します。

Mavenユーザーは、pom.xmlファイルに次の依存関係を追加できます。

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Gradleユーザーは、次の依存関係を build.gradle ファイルに追加できます。

compile('org.springframework.cloud:spring-cloud-starter-config')

次に、@ RefreshScopeアノテーションをメインのSpring Bootアプリケーションに追加する必要があります。 @RefreshScopeアノテーションは、構成サーバーから構成プロパティー値をロードするために使用されます。

package com.example.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;

@SpringBootApplication
@RefreshScope
public class ConfigclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(ConfigclientApplication.class, args);
   }
}

ここで、application.propertiesファイルに構成サーバーのURLを追加し、アプリケーション名を指定します。

注意-設定クライアントアプリケーションを起動する前に、http://localhost:8888設定サーバーを実行する必要があります。

spring.application.name = config-client
spring.cloud.config.uri = http://localhost:8888

構成サーバーからウェルカムメッセージを読み取るための単純なRESTエンドポイントを記述するためのコードを以下に示します-

package com.example.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RefreshScope
@RestController
public class ConfigclientApplication {
   @Value("${welcome.message}")
   String welcomeText;

   public static void main(String[] args) {
      SpringApplication.run(ConfigclientApplication.class, args);
   }
   @RequestMapping(value = "/")
   public String welcomeText() {
      return welcomeText;
   }
}

次のMavenまたはGradleコマンドを使用して、実行可能なJARファイルを作成し、Spring Bootアプリケーションを実行できます-

Mavenの場合、以下に示すコマンドを使用できます-

mvn clean install

「BUILD SUCCESS」の後、ターゲットディレクトリの下にJARファイルがあります。

Gradleの場合、以下に示すコマンドを使用できます-

gradle clean build

「BUILD SUCCESSFUL」の後、build/libsディレクトリの下にJARファイルがあります。

次に、次のコマンドを使用してJARファイルを実行します。

java –jar <JARFILE>

さて、アプリケーションはここに示すようにTomcatポート8080で開始されました-

Tomcatポート8080で開始されたアプリケーション

コンソールウィンドウでログを確認できます。 config-clientアプリケーションは、 [[1]] から構成を取得しています

2017-12-08 12:41:57.682  INFO 1104 --- [
   main] c.c.c.ConfigServicePropertySourceLocator :
   Fetching config from server at: http://localhost:8888

ここでURLにアクセスすると、 http://localhost:8080/ ウェルカムメッセージが構成サーバーから読み込まれます。

Spring Cloud Config Server

次に、構成サーバーでプロパティ値を変更し、アクチュエーターのエンドポイントPOST URL http://localhost:8080/refresh にアクセスして、URL http://localhost:8080/ の新しい構成プロパティ値を確認します。