1. Introduction
In order to facilitate unified management of configuration file management, spring cloud config is used as a distributed configuration center. Baidu's disconf and Ctrip's apollo are better domestically produced. Here we introduce the use of spring cloud config. It supports placing the configuration service in the memory of the configuration service (ie local), and also supports placing it in remote Git, SVN and other repositories. In the spring cloud config component, there are two roles, one is config server and the other is config client.
2. Create Config Server
Create a spring-cloud project named config-server, which supports maven and gradle. The configuration center here uses the configuration on the svn remote library:
1. Introduce dependencies in maven's pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>config-server</groupId> <artifactId>config-server</artifactId> <packaging>jar</packaging> <name>dmw-config</name> <description>Configuration Center</description> <!--Use the latest version of spring-boot -> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <!--Dependency management, used to manage the dependencies of spring-cloud, where Edgware.SR1 is the version number--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>dmw-config</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.gradle build.gradle依赖:
buildscript { ext { springBootVersion = '1.5.10.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'org.springframework.boot' group = 'com.config' version = '' sourceCompatibility = 1.8 repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() } configurations { providedRuntime } ext { springCloudVersion = 'Edgware.SR1' if(!project.hasProperty("profile")){ profile='test' } } sourceSets { main { resources { srcDir "env/${profile}" } } } dependencies { compile('org.springframework.cloud:spring-cloud-config-server'){ exclude module: 'spring-boot-starter-tomcat' } compile('org.springframework.cloud:spring-cloud-starter-eureka') runtime('org.springframework.boot:spring-boot-starter-undertow') testCompile('org.springframework.boot:spring-boot-starter-test') compile('org.tmatesoft.svnkit:svnkit') compile('org.springframework.boot:spring-boot-starter-actuator') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
Create a new entry class BootApplication:
mport org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication .class, args); } }application.yml :
spring: profiles: #Use svn here must be specified as subversion, otherwise an error will be reported, because the git configured by default in config active: subversion cloud: config: server: svn: # Configure the svn repository address uri: svn://192.168.1.1/config-repo # Configure svn access account username: test # Configure svn access password password: test #Configure the directory where the svn project configuration file is located default-label: profilesThe server is completed. If there is an application-dev.yml configuration file in the profiel directory of config-repo on svn, you can access the configuration information through http://loalhost:8080/application-dev.yml. This project does not have Specify the port, so the default is 8080,
3. Build Config Client
Recreate a springboot project named config-client
1. The pom file of maven introduces dependencies:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2. gradle gruadle.build
buildscript { ext { springBootVersion = '1.5.10.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'org.springframework.boot' apply plugin: 'war' group = 'com.config' version = '' sourceCompatibility = 1.8 repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() } configurations { providedRuntime } ext{ springCloudVersion = 'Edgware.SR1' if(!project.hasProperty("profile")){ profile='demo' } } sourceSets { main { resources { srcDir "profiles/${profile}" } } } dependencies { compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-config-client') providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
Its configuration file bootstrap.yml :
spring: profiles: active: dev application: name: config-client cloud: config: enabled: true profile: ${spring.profiles.active} uri: http://${spring.cloud.client.ipAddress}:8080 #If config-server is configured with account and password username: test password: test #retry mechanism fail-fast: true retry: initial-interval: 2000 max-interval: 10000 multiplier: 2 max-attempts: 10
The entry class of the program, write an API interface "/hello", and return the value from the foo variable in the configuration center, which can be obtained. The code is as follows:
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication .class, args); } @Value("${foo}") // key in svn configuration file String foo; @RequestMapping(value = "/hello") public String hi(){ return foo; } }