一、简介
为了方便统一管理配置文件管理,使用spring cloud config作为分布式配置中心,国产比较好的有百度的disconf,携程的apollo,这里我们介绍使用spring cloud config。它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git、SVN等仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
二、创建Config Server
创建一个spring-cloud项目,取名为config-server,支持maven和gradle,这里配置中心使用svn远程库上的配置:
1.maven的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>配置中心</description> <!--使用最新版的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> <!--依赖管理,用于管理spring-cloud的依赖,其中Edgware.SR1是版本号--> <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}" } }
新建入口类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: #使用svn这里必须指定为subversion,否则会报错,因为config默认配置的git active: subversion cloud: config: server: svn: # 配置svn仓库地址 uri: svn://192.168.1.1/config-repo # 配置svn访问账号 username: test # 配置svn访问密码 password: test #配置svn项目配置文件所在目录 default-label: profiles服务端完成,如果在svn上的config-repo的profiel目录下有一个application-dev.yml配置文件,则可以通过http://loalhost:8080/application-dev.yml访问获得配置信息,该项目没有指定端口,所以默认8080,
三、构建Config Client
重新创建一个springboot项目,取名为config-client
1.maven 的pom文件引入依赖:
<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}" } }
其配置文件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 #如果config-server配置了账号跟密码 username: test password: test #重试机制 fail-fast: true retry: initial-interval: 2000 max-interval: 10000 multiplier: 2 max-attempts: 10
程序的入口类,写一个API接口“/hello”,返回从如果配置中心的有foo变量的值,则可以获取,代码如下:
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}") // svn配置文件里的key String foo; @RequestMapping(value = "/hello") public String hi(){ return foo; } }