[JAVA] Einbetten des Konfigurationsservers in Spring Cloud Config

Spring Cloud Config wird grundsätzlich getrennt für Server und Client verwendet (glaube ich), kann aber auch im integrierten Modus gestartet werden.

Als Referenz Spring Cloud Config - Einbetten des Konfigurationsservers Um.

Quellcode

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

Als Abhängigkeit wird nur die Serverseite benötigt. Das Obige schließt auch das Web zum Überprüfen des Vorgangs ein.

/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

Schreiben Sie die Einstellungen in </ font> </ b> `` `bootstrap.yaml``` anstelle von appplication.yaml </ code> .

Das Folgende ist eine Steuerung zum Überprüfen des Betriebs.

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