kagamihogeの日記

kagamihogeの日記です。

Spring Cloud Configをためす

Spring Cloud Config を試す。

gitリポジトリの準備

適当なローカルディレクトリとか、GitHubとか、http://kagamihoge.hatenablog.com/entry/2020/02/13/095145 とか、なんでも良いが適当なgitリポジトリを準備する。

プロパティファイルとして/sample.propertiesというファイルをpushしておく。中身はごく普通のプロパティファイル。

hoge.message=sample hello config.

Spring Cloud Config Server

httpでプロパティファイルを公開するserverを準備する。Spring Initializr でDependenciesにConfig Serverを追加する。それで生成されるbuild.gradleは下記のような感じ。

plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'kagamihoge.sample.springcloudconfig'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "Hoxton.SR1")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

起動用のエントリポイントを作る。@EnableConfigServerが必要。

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.SERVLET).run(args);
    }
}

serverのプロパティファイル。gitのuri, username, passwordを指定する。後述のclientも一緒に動かすのでポートも変更している。

server.port=8888
spring.cloud.config.server.git.uri=ssh://root@localhost:32768/root/config.git
spring.cloud.config.server.git.username=root
spring.cloud.config.server.git.password=a

Config Client

serverからプロパティを取得するclientをつくる。プロジェクト作成は同様にSpring Initializrを使用し、Dependenciesにweb, Config Client, actuatorを追加する。actuatorはプロパティのリフレッシュに使用するので、必要無ければ無しでも良い。

以下は生成されたbuild.gradleをそのままコピペしたもの。

plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'kagamihoge.sample.springcloudconfig'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "Hoxton.SR1")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

プロパティ取得は今まで通りの@Valueが使える。@RefreshScopeは後述。

@SpringBootApplication
@RestController
@RefreshScope
public class Application {
    @Value("${hoge.message}")
    private String message;
    
    @RequestMapping("/")
    public String home() {
        return "Hello World!" + message;
    }
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.SERVLET).run(args);
    }
}

clientのプロパティにいくつかの設定をする。まずspring.cloud.config.uriにserverのurlを指定する。

spring.application.nameはアプリケーション名で、プロパティファイル名の解決にこれが使われる。ルールは https://cloud.spring.io/spring-cloud-config/reference/html/#_quick_start にあるとおり。今回はclientでspring.application.name=sampleと指定しているので{application}sampleとなり、{profile}は未指定なので空、よって/{application}-{profile}.propertiesのルールで/sample.propertiesを参照しにいく事になる。

spring.cloud.config.uri=http://localhost:8888
spring.application.name=sample
management.endpoints.web.exposure.include=refresh

これを起動して http://localhost:8080/ にアクセスするとserverからプロパティを取得して表示が行われる。

プロパティのリフレッシュ

プロパティファイル変更のリフレッシュにはそれ用のエンドポイントをたたく。

このためにはactuatorが必要で、build.gradleでactuatorの依存性とmanagement.endpoints.web.exposure.include=refreshでrefreshエンポイントを使用可能にしておく。

動作検証としてgitのsample.propertiesを適当に変更してpush後、http://localhost:8080/actuator/refreshにPOSTする。なお、@Valueのプロパティは@RefreshScopeを付与する必要がある。これで再度 http://localhost:8080/ アクセスすると変更後のプロパティが取得できる。

参考