[JAVA] Spring Cloud Config Embedding the Config Server

Spring Cloud Config is basically used separately for server and client (I think), but it can also be started in embedded mode.

For reference, Spring Cloud Config --Embedding the Config Server Around.

Source code

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

Only the server side is required as the dependency. The above also includes the web for checking the operation.

/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

Write the settings in </ font> </ b> `` `bootstrap.yaml``` instead of appplication.yaml </ code> .

The following is a controller for checking the operation.

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