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/ アクセスすると変更後のプロパティが取得できる。