为什么用spring cloud config 写道
一个Application中不只是代码,还需要连接资源和其它应用,经常有很多需要外部设置的项去调整Application行为,如切换不同的数据库,i18n国际化 等.应用中的会经常见到的xml,properties,yaml等就是配置信息.
常见的实现信息配置的方法:
● 硬编码(缺点:需要修改代码,风险大)
● 放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)
● 文件系统中(缺点:依赖操作系统等)
● 环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)
● 云端存储(缺点:与其他应用耦合)Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
常见的实现信息配置的方法:
● 硬编码(缺点:需要修改代码,风险大)
● 放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)
● 文件系统中(缺点:依赖操作系统等)
● 环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)
● 云端存储(缺点:与其他应用耦合)Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
真正的数据存在Git等repository中,Config Server去获取相应的信息,然后开发给Client Application,相互间的通信基于HTTP,TCP,UDP等协议.微服务框架落地云平台,如果配置文本地化,将来多实例化后,极难维护。
// =================配置仓库(git/svn/native)=================
属性文件属名
格式:name-profile.properties/yml,例如:
vs-dev.properties
vs-test.properties
vs-prod.properties
仓库创建
略
// =================
MAVEN(项目构建)
=================
服务端
<?xml version="1.0" encoding="UTF-8"?> <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> <artifactId>config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <java.version>1.7</java.version> <project.encoding>UTF-8</project.encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.5.RELEASE</spring.version> </properties> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <relativePath/> </parent> <dependencies> <!--spring cloud config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- SVN Repos --> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <optimize>true</optimize> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!--mvn spring-boot:run 命令--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>客户端:
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- <parent> <artifactId>spring-cloud-in-action</artifactId> <groupId>com.nobodyiam</groupId> <version>1.0-SNAPSHOT</version> </parent> --> <modelVersion>4.0.0</modelVersion> <artifactId>config-client</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <java.version>1.7</java.version> <project.encoding>UTF-8</project.encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.5.RELEASE</spring.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.3.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> --> </dependencies> </dependencyManagement> <dependencies> <!--spring cloud config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <optimize>true</optimize> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!--mvn spring-boot:run 命令--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>// ================= 编码 ======================
服务端
package com.spring.cloud.config.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.context.annotation.Configuration; /** * Created by Jason on 2/24/16. */ @Configuration @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }配置文件可以采用yml或properties格式,远程仓库可以采用git/svn/native,远程仓库只支持Http协议,所以svn必须以HTTP协议开放,否则连接不上,客户无法读取配置文件进行初始化工作。
management.context_path= /admin server.port= 8086 #server.contextPath= /ConfigServer #Git or SVN(subversion) or native , by default git(value config default) spring.profiles.active= default #1.Git repos configuration spring.cloud.config.server.git.uri= https://github.com/yangyangmyself/config-repo #spring.cloud.config.server.git.username=oyyl #spring.cloud.config.server.git.password=oyyl #spring.cloud.config.server.git.cloneOnStart=true #spring.cloud.config.server.git.basedir=target/class/repos #2.SVN repos configuration #spring.cloud.config.server.svn.uri= https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.username=oyyl #spring.cloud.config.server.svn.password=oyyl #spring.cloud.config.server.svn.default-label=branches #spring.cloud.config.server.svn.searchPaths=configs/test #spring.cloud.config.server.svn.cloneOnStart=true #2.1Multi sub repos configuration(Maven project is must add svnkit jar file) #Math name(or applicaion) #spring.cloud.config.server.svn.repos.r1.pattern=vs1* #spring.cloud.config.server.svn.repos.r1.uri=https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.repos.r1.username=oyyl #spring.cloud.config.server.svn.repos.r1.password=oyyl #spring.cloud.config.server.svn.repos.r1.default-label=branches #spring.cloud.config.server.svn.repos.r1.searchPaths=configs/test #spring.cloud.config.server.svn.repos.r2.pattern=vs2* #spring.cloud.config.server.svn.repos.r2.uri=https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.repos.r2.username=oyyl #spring.cloud.config.server.svn.repos.r2.password=oyyl #spring.cloud.config.server.svn.repos.r2.default-label=branches #spring.cloud.config.server.svn.repos.r2.searchPaths=configs/test #Math profile #spring.cloud.config.server.svn.repos.r3.pattern=vs2/* #spring.cloud.config.server.svn.repos.r3.uri=https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.repos.r3.username=oyyl #spring.cloud.config.server.svn.repos.r3.password=oyyl #spring.cloud.config.server.svn.repos.r3.default-label=branches #spring.cloud.config.server.svn.repos.r3.searchPaths=configs/test #3.Read Native Config #3.1Load local system file #spring.profiles.active=native #spring.cloud.config.server.native.searchLocations=file://D:/etc/cas/config #3.2Load application classpath #spring.profiles.active=native #spring.cloud.config.server.native.searchLocations=classpath\:/repos
验证
http://localhost:8086/name/profiles/label
http://localhost:8086/name-profiles.properties/
http://localhost:8086/name-profiles.yml,如下:
http://localhost:8086/vs-dev.yml
客户端
package com.spring.cloud.config.client; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Configuration @EnableAutoConfiguration @ComponentScan @RefreshScope @RestController public class ConfigClientApplication { @Value("${redis.server}") private String paraName; @RequestMapping(value = "/cf/redis") public String showRedis() { return "Reids IP: " + paraName; } private String getRequestStr(HttpServletRequest request){ String servletPath = request.getServletPath(); String requestURL = request.getRequestURL().toString(); return requestURL.substring(0, requestURL.indexOf(servletPath)); } @RequestMapping(value = "/admin/config/refresh") public String refresh(HttpServletRequest request) { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(getRequestStr(request) + "/admin/refresh"); try { client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "refresh"; } @RequestMapping(value = "/admin/config/restart") public String restart(HttpServletRequest request) { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(getRequestStr(request) + "/admin/restart"); try { client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "restart"; } public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }bootstrap.properties配置文件, bootstrap优先application加载,配置连接spring cloud config server端及读取属性文件
如:读取环境文件有:
vs.properties
vs-dev.properties
vs-test.properties
配置说明:
spring.application.name 为属性文件name片段
spring.cloud.config.profile 为属性文件profile片段
spring.cloud.config.uri 为config server端
如读取开发环境配置文件
vs-dev.properties
,则按如下配置:
spring.application.name=vs
spring.cloud.config.profile=dev
如果
profile=default,则读取
vs.properties属性文件
management.context_path= /admin server.contextPath= /vs server.port= 8087 #Propertis name(name of name-profile-lable.yml) spring.application.name= vs #1.Read Remote Config spring.cloud.config.uri= http://localhost:8086 #Profile(configed value to default or dev or test or prod and so on) # is come from propertis name(profile of name-profile-lable.yml) spring.cloud.config.profile: default// ============== 高可用多库配置说明(服务端) ==================
info: component: Config Server management: context_path: /admin server: port: 8888 spring: cloud: config: server: svn: password: oyyl uri: https://192.168.110.44/svn/config-repos // 只支持HTTP、HTTPS其它协议测试未通过 username: oyyl default-label: branches // 默认使用分支 searchPaths: configs/test //多级目录表示 cloneOnStart: true // 启动里复制到本地服务器 repos:... //多库配置 profiles: active: subversion //表示使用SVN, 默认使用git 【注意】uri 指定repos根目录即可,否则无法找到文件 SVN: default-label: trunk/branches GIT: default-label: master/branchesYML
info: component: Config Server management: context_path: /admin server: port: 8888 spring: cloud: config: server: svn: password: oyyl uri: https://192.168.110.44/svn/config-repos // 默认配置库 username: oyyl default-label: branches searchPaths: configs repos: vs1: pattern: vs1* // 此配置只匹配application, 如果为 vs1/* ,则匹配profiles uri: https://192.168.110.44/svn/config-repos // 配置库1 password: oyyl username: oyyl default-label: branches searchPaths: configs vs2: pattern: vs2* uri: https://192.168.110.44/svn/config-repos // 配置库2 password: oyyl username: oyyl default-label: branches searchPaths: configs profiles: active: subversion
目前实现远程仓库配置文件修改后,需要调用客户的Endpoint接口,进行手动刷新属性值,下面就是启动
Endpoint管理接口
// 启动管理接口,如refresh 等,需要spring-boot-starter-actuatorr <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
客户端通过@Value({"key"})实现读取属性,只支持实例属性,不支持类静态属性赋值