什么是Spring Cloud Config?html
建立并运行一个Spring Cloud Config Servergit
创建一个Repositorygithub
建立并运行一个Spring Cloud Config Clientweb
什么是配置信息?
一个Application中不仅是代码,还须要链接资源和其它应用,常常有不少须要外部设置的项去调整Application行为,如切换不一样的数据库,i18n国际化 等.应用中的会常常见到的xml,properties,yaml等就是配置信息.spring
常见的实现信息配置的方法:数据库
硬编码(缺点:须要修改代码,风险大)apache
放在xml等配置文件中,和应用一块儿打包(缺点:须要从新打包和重启)bootstrap
文件系统中(缺点:依赖操做系统等)app
环境变量(缺点:有大量的配置须要人工设置到环境变量中,不便于管理,且依赖平台)maven
云端存储(缺点:与其余应用耦合)
Spring Cloud Config 就是云端存储配置信息的,它具备中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
Spring Cloud Config的原理如图所示,真正的数据存在Git等repository中,Config Server去获取相应的信息,而后开发给Client Application,相互间的通讯基于HTTP,TCP,UDP等协议.
1.建立一个名为my-config-server的应用,并添加spring-cloud-starter-parent,spring-cloud-config-server依赖,pom信息具体以下
<?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> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <relativePath/> </parent> <groupId>org.mmb.cloud</groupId> <artifactId>mmb-config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
2.在Application主类上添加@EnableConfigServer注解,具体以下
package config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * Created by mmb on 2016/7/30. */ @EnableConfigServer @SpringBootApplication public class MMBConfigServerApplication { public static void main(String[] args) { SpringApplication.run(MMBConfigServerApplication.class, args); } }
3.去本身的GitHub上建立一个repository命名=MMBConfigData,并建立一个mmb-config-client.yml的配置文件,并添加一个key为luck-word,value mmb,或者其它任何值.具体以下
--- lucky-word: mmb
4.回本身的工程,设置应用application.yml,配置spring.cloud.config.server.git.uri为"https://github.com/"YOUR-GITHUB-ID"/"YOUR-REPOSITORY-NAME"",并设置端口server.port为8001
server: port: 8001 spring: cloud: config: server: git: uri: https://github.com/mumubin/MMBConfigData searchPaths: data
5.启动应用并打开地址http://localhost:8001/mmb-con...将会看到配置好的信息,个人以下
{ "name": "mmb-config-client", "profiles": [ "default" ], "label": "master", "version": "4d9240f45fecd34136f81683d44c2e144792af86", "propertySources": [ { "name": "https://github.com/mumubin/MMBConfigData/data/mmb-config-client.yml", "source": { "lucky-word": "mmb" } } ] }
1.建立一个名为my-config-client的应用,并添加spring-cloud-starter-parent,spring-cloud-starter-config,spring-boot-starter-web依赖,pom信息具体以下
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <!-- Allow for automatic restarts when classpath contents change. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <groupId>org.mmb.cloud</groupId> <artifactId>mmb-config-client</artifactId> <version>1.0-SNAPSHOT</version> </project>
2.建立bootstrap.yml在resource下,并设置spring.application.name,spring.cloud.config.uri,server.port信息,具体以下
spring: application: name: mmb-config-client cloud: config: uri: http://localhost:8001 --- server: port: 8002
注意这里是bootstrap.yml而不是appliction.yml,由于bootstrap.yml会在应用启动以前读取,而spring.cloud.config.uri会影响应用启动
3.建立一个Controller
@RestController public class LuckyWordController { @Value("${lucky-word}") String luckyWord; @RequestMapping("/lucky-word") public String showLuckyWord() { return "The lucky word is: " + luckyWord; } }
4.启动Application,打开http://localhost:8002/lucky-word
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
The lucky word is: mmb
特别感谢 kennyk65
Spring Cloud 中文用户组 31777218
Spring-Cloud-Config 官方文档-中文译本
Spring Cloud Netflix 官网文档-中文译本
本文实例github地址 Config Server Config Client