Spring-boot-cloud-configuration-server
Spring Boot-クラウド構成サーバー
Spring Cloud Configuration Serverは、すべてのアプリケーション関連の構成プロパティを管理する集中化されたアプリケーションです。 この章では、Spring Cloud Configurationサーバーの作成方法について詳しく学習します。
Spring Cloud構成サーバーの作成
最初に、Spring InitializerページからSpring Bootプロジェクトをダウンロードし、Spring Cloud Config Server依存関係を選択します。 以下のスクリーンショットを確認してください-
次に、以下に説明するように、ビルド構成ファイルにSpring Cloud Configサーバーの依存関係を追加します-
Mavenユーザーは、以下の依存関係をpom.xmlファイルに追加できます。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Gradleユーザーは、build.gradleファイルに以下の依存関係を追加できます。
compile('org.springframework.cloud:spring-cloud-config-server')
次に、メインのSpring Bootアプリケーションクラスファイルに@EnableConfigServerアノテーションを追加します。 @EnableConfigServerアノテーションは、Spring Bootアプリケーションを構成サーバーとして機能させます。
メインのSpring Bootアプリケーションクラスファイルは以下のとおりです-
package com.finddevguides.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
次に、以下の構成をプロパティファイルに追加し、application.propertiesファイルをbootstrap.propertiesファイルに置き換えます。 以下のコードを確認してください-
server.port = 8888
spring.cloud.config.server.native.searchLocations=file:///C:/configprop/
SPRING_PROFILES_ACTIVE=native
構成サーバーはTomcatポート8888で実行され、アプリケーション構成プロパティはネイティブ検索場所からロードされます。
次に、 file:///C:/configprop/ に、クライアントアプリケーション-application.propertiesファイルを配置します。 たとえば、クライアントアプリケーションの名前は config-client で、application.propertiesファイルの名前を config-client.properties に変更し、プロパティファイルをパス file:///C:/configprop/ に配置します。
config-clientプロパティファイルのコードは次のとおりです-
welcome.message = Welcome to Spring cloud config server
完全なビルド構成ファイルは以下のとおりです-
Mavenユーザーは、以下に示す pom.xml を使用できます-
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.finddevguides</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>configserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Gradleユーザーは、以下に示すbuild.gradleファイルを使用できます-
<scope>import</scope>
</dependency>
</dependencies>
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.finddevguides'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
compile('org.springframework.cloud:spring-cloud-config-server')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
今、実行可能なJARファイルを作成し、次のMavenまたはGradleコマンドを使用してSpring Bootアプリケーションを実行します-
Mavenの場合、以下のコマンドを使用します-
mvn clean install
「BUILD SUCCESS」の後、ターゲットディレクトリの下にJARファイルがあります。
Gradleの場合、以下に示すコマンドを使用します-
gradle clean build
「BUILD SUCCESSFUL」の後、build/libsディレクトリの下にJARファイルがあります。
次のコマンドを使用してJARファイルを実行します-
java –jar <JARFILE>
さて、ここに示すように、アプリケーションはTomcatポート8888で開始されました-
ここで、WebブラウザでURL http://localhost:8888/config-client/default/master にアクセスすると、次のようにconfig-clientアプリケーションの構成プロパティが表示されます。