1
1

More than 3 years have passed since last update.

Spring Cloud ConfigのEmbedding the Config Server

Posted at

Spring Cloud Configは基本的にはserverとclientに分かれて使うもの(と思う)が、組み込みモードで起動することも出来る。

リファレンス的にはSpring Cloud Config - Embedding the Config Serverのあたり。

ソースコード

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

sourceCompatibility = '11'
repositories {
  mavenCentral()
}
ext {
  set('springCloudVersion', "Hoxton.SR3")
}
dependencies {
  implementation 'org.springframework.cloud:spring-cloud-config-server'
  implementation 'org.springframework.boot:spring-boot-starter-web'

  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()
}

依存性としてはserver側だけで良い。上は動作確認用にwebも入れている。

/src/main/resources/bootstrap.yaml
spring:
  application:
    name: sample
  profiles:
    active: composite
  cloud:
    config:
      server:
        composite:
          - type: native
            search-locations: file:///C:/configtest/
        bootstrap: true
      prefix: config

appplication.yamlではなくbootstrap.yamlに設定を記述する。

以下は動作確認用のcontroller。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableConfigServer
@SpringBootApplication
@RestController
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);
    }
}
1
1
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
1