LoginSignup
1
0

More than 5 years have passed since last update.

SpringCloudConfigをとりあえず動かしてみる

Last updated at Posted at 2018-11-16

目的

Spring Cloud Configのマニュアルを参考にしながら
シンプルにConfigServerとConfigClientを構築し、
ConfigServerに配置した設定ファイルがClientに反映されているか確認してみる。

Config Server

spring-cloud-config-serverを追加

build.gradle
dependencies {
    implementation('org.springframework.cloud:spring-cloud-config-server')
}

SpringBootの起動クラスに@EnableConfigServerを追加

ConfigServerApplication.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}

Configファイルが配置されているgitリポジトリをapplication.ymlに設定。
また、Springbootのデフォルトは8080ポートなので、
後述するクライアントと衝突しないよう、8880ポートにする。

application.yml
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を追加

build.gradle
dependencies {
  implementation('org.springframework.cloud:spring-cloud-starter-config')
}

bootstrap.ymlを新規作成する
SpringCloudConfigClientではapplication.ymlではなくbootstrap.ymlが読み込まれ、
そのspring.cloud.config.uriにアクセスがいく(未指定の場合はhttp://localhost:8888)

bootstrap.yml
spring:
  application:
    name: foo
  profiles:
    active: development
  cloud:
    config:
      # ConfigServerのURI
      uri: http://localhost:8880
      # ConfigServerに接続できなかった時にExceptionを発生させたいならtrueにする (defaultはfalse)
      fail-fast: true

起動クラスに設定値が確認できるエンドポイントを追加

ConfigClientApplication.java
@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"が表示された。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0