目的
Spring Cloud Configのマニュアルを参考にしながら
シンプルにConfigServerとConfigClientを構築し、
ConfigServerに配置した設定ファイルがClientに反映されているか確認してみる。
Config Server
spring-cloud-config-server
を追加
dependencies {
implementation('org.springframework.cloud:spring-cloud-config-server')
}
SpringBootの起動クラスに@EnableConfigServer
を追加
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
Configファイルが配置されているgitリポジトリをapplication.ymlに設定。
また、Springbootのデフォルトは8080ポートなので、
後述するクライアントと衝突しないよう、8880ポートにする。
server:
port: 8880
spring:
cloud:
config:
server:
git:
# 設定ファイルが存在するgitリポジトリURI
uri: https://github.com/spring-cloud-samples/config-repo.git
ここまで設定したら起動する。
起動すると以下のエンドポイントでアクセスできるようになる。
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
ConfigClientはConfigServerから{}をClientの値で置き換えた結果の設定ファイルを取得する。
{application}:spring.application.name
の値 (デフォルトは"application")
{profile}:spring.profiles.active
の値
{label}:gitのブランチ、タグ名、またはcommitID(デフォルトは"master")
試しにhttp://localhost:8880/foo/development/
にリクエストを投げると
以下のレスポンスが返ってきた
{
"name": "foo",
"profiles": [
"development"
],
"label": null,
"version": "a611374438e75aa1b9808908c57833480944e1a8",
"state": null,
"propertySources": [
{
"name": "https://github.com/spring-cloud-samples/config-repo.git/foo-development.properties",
"source": {
"bar": "spam",
"foo": "from foo development"
}
},
{
"name": "https://github.com/spring-cloud-samples/config-repo.git/foo.properties",
"source": {
"foo": "from foo props",
"democonfigclient.message": "hello spring io"
}
},
{
"name": "https://github.com/spring-cloud-samples/config-repo.git/application.yml (document #0)",
"source": {
"info.description": "Spring Cloud Samples",
"info.url": "https://github.com/spring-cloud-samples",
"eureka.client.serviceUrl.defaultZone": "http://localhost:8761/eureka/",
"foo": "baz"
}
}
]
}
gitリポジトリに認証がある場合はこの辺を参考にapplication.ymlを設定すればよさそう
Authentication
Config Client
spring-cloud-starter-config
を追加
dependencies {
implementation('org.springframework.cloud:spring-cloud-starter-config')
}
bootstrap.ymlを新規作成する
SpringCloudConfigClientではapplication.ymlではなくbootstrap.ymlが読み込まれ、
そのspring.cloud.config.uri
にアクセスがいく(未指定の場合はhttp://localhost:8888)
spring:
application:
name: foo
profiles:
active: development
cloud:
config:
# ConfigServerのURI
uri: http://localhost:8880
# ConfigServerに接続できなかった時にExceptionを発生させたいならtrueにする (defaultはfalse)
fail-fast: true
起動クラスに設定値が確認できるエンドポイントを追加
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@GetMapping("/bar")
public String bar(@Value("${bar: notDefined}") String bar) {
return bar;
}
@GetMapping("/foo")
public String foo(@Value("${foo: notDefined}") String foo) {
return foo;
}
}
結果確認
Clientのapplication名はfoo、profileはdevelopmentなので以下の設定ファイルが期待値になる。
https://github.com/spring-cloud-samples/config-repo/blob/master/foo-development.properties
bar: spam
foo: from foo development
Clientを起動し、localhost:8080/barにアクセスすると"spam"
localhost:8080/fooにアクセスすると"from foo development"が表示された。