Centralize the configuration files for each application in a system for easy management.
Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; Import org.springframework.cloud.config.server.EnableConfigServer, @SpringBootApplication @enableconfigserver Public class configapplication { publicstaticvoid main (string[] args) { Springapplication.run (configapplication. class , args);} }
As above, adding @enableconfigserver to a Spring boot application can turn it into a centralized configuration service.
Build.gradle file:
Buildscript {ext {springbootversion= ' 1.4.2. RELEASE '} repositories {mavencentral () Jcenter ()} dependencies {classpath ("Org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}") }}Apply the Java-library plugin to add support forjava libraryapply plugin: ' java-Library ' Apply plugin: ' java ' Apply plugin: ' Org.springframework.boot ' version= ' 0.0.1-SNAPSHOT ' sourcecompatibility= 1.8//inchThis section, where to find the dependencies of your projectrepositories {declareUse Jcenter forResolving your dependencies. Can declare any maven/ivy/file repository here. Jcenter () mavencentral () Maven {URL"Http://dl.bintray.com/oembedler/maven"} maven {URL"Https://repo.spring.io/libs-release"} maven {URL"http://10.100.122.249:8881/nexus/content/groups/public/"}}dependencymanagement {imports {Mavenbom ' org.springframework.cloud:spring-cloud-netflix:1.2.0. M1 ' Mavenbom ' org.springframework.cloud:spring-cloud-dependencies:Camden.SR2 '}} dependencies {//Use JUnit test framework testimplementation ' Junit:junit:4.12'https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-Server Compile ("ORG.SPRINGFRAMEWORK.CLOUD:SPRING-CLOUD-STARTER-PARENT:CAMDEN.SR2") Compile ("org.springframework.boot:spring-boot-starter-security") Compile ("Org.springframework.cloud:spring-cloud-config-server") Compile ("Org.springframework.boot:spring-boot-starter-web") Compile ("Org.springframework.boot:spring-boot-starter-actuator")}
Config server's own configuration file is as follows:
Spring: Cloud: config: server: Native: searchlocations:classpath:/Shared Profiles: active:nativeserver: 8888Security: User: password:root
The native representative places the configuration files for each access application in the local directory of the config server, which is placed in the
Start config server and verify Http://localhost:8888/demo/application.property (this assumes that the configuration file for the demo app is placed in the shared directory demo.property)
Take a look at how the Access application uses this config server
In the demo application:
Add compile("ORG.SPRINGFRAMEWORK.CLOUD:SPRING-CLOUD-STARTER-PARENT:CAMDEN.SR2") to dependencies Compile("org.springframework.cloud:spring-cloud-starter-config")//in Build. under Gradle root, add the following dependency dependencymanagement {imports {mavenbom ' org. Springframework.cloud: Spring- Cloud-dependencies: Camden. SR2 '}}
* Copy the Application.properties file under Project resource to the shared folder under Server-config resource
and rename it to the project name. Properties, such as the demo project, named Demo.properties
* Empty application.properties Content
3. Create a new Bootstrap.yml file under Resource (Demo project example)
Spring: application: name:demo Profiles: active:default Cloud: config: URI: http://localhost:8888/ name:demo fail-fast:true password:root username:user
The centralized configuration of Spring cloud practice Spring-config