参考资料:

  https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.0.RELEASE/single/spring-cloud-config.html

  http://cloud.spring.io/spring-cloud-static/Camden.SR7/#_spring_cloud_config

Spring Cloud Config为分布式系统中的外部配置提供服务端和客户端的支持。使用Config Server,你可以在所有环境中管理应用程序的外部属性。

客户端和服务端上的概念和与Spring Environment和PropertySource相同,因此非常适合Spring应用程序,也可以与任何语言运行的应用程序一起使用。

当应用程序从开发到测试转移到部署管道时,你可以管理这些环境之间的配置,以确保应用程序拥有迁移时所需的所有内容。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,并且可以通过各种工具来访问内容。

特点:

Spring Cloud Config Server features:

  对于外部配置项(如name-value对或相同功能的YAML内容),提供了基于资源的HTTP接口;

  加密和解密属性值(对称或不对称);

  使用@EnableConfigServer注解可以很容易将此服务器嵌入到Spring Boot应用程序中。

Config Client features(for Spring  applications):

  绑定到配置服务器,并使用远程属性资源来初始化Spring环境;

  加密和解密属性值(对称和非对称)。

快速入门:

  启动服务:

$ cd spring-cloud-config-server
$ ../mvnw spring-boot:run

  服务器是一个Spring Boot应用程序,你可以从你的IDE运行它(main类是ConfigServerApplication).然后试着启动一个客户端:

$ curl localhost:/foo/development
{"name":"foo","label":"master","propertySources":[
{"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}},
{"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}
]}

  查找一个属性资源的默认策略是克隆一个git仓库(at spring.cloud.config.server.git.uri ) ,使用它去初始化一个 mini SpringApplication. 这个mini-application的Environment用来枚举属性资源,并通过JSON节点发布它们。

  HTTP服务具有以下形式的资源:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

  “applications”是SpringApplicationspring.config.name(一般来说application是一个常规的Spring Boot应用),"profile"是一个active的profile(或者是逗号分隔的属性列表),label是一个可选的git标签(默认为"master").

  Spring Cloud Config Server从git仓库(必须提供)为远程客户端提取配置:

spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo

  要在应用程序中使用这些功能,只需将其构建为一个依赖于 spring-cloud-config-client的Spring Boot应用程序。添加依赖最方便的方法是通过Spring Boot启动器org.springframework.cloud:spring-cloud-starter-config. 对于Maven用户还有一个父pom和BOM(spring-cloud-starter-parent),对于Gradle和Spring CLI用户有一个Spring IO版本管理文件。例如Maven的配置:

<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-dependencies</artifactId>
<version>Brixton.RELEASE</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <!-- repositories also needed for snapshots and milestones -->

  然后创建一个标准的Spring Boot应用程序,就像这个简单的HTTP服务器:

@SpringBootApplication
@RestController
public class Application { @RequestMapping("/")
public String home() {
return "Hello World!";
} public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

  运行时,它将从端口8888上的默认本地配置服务器(如果它正在运行)中选取外部配置。要修改启动行为,可以使用bootstrap.properties(类似于application.properties,但是是一个应用上下文启动的配置文件)更改配置服务器的位置,例如:

spring.cloud.config.uri: http://myconfigserver.com

  引用属性将作为高优先级属性源显示在 /env 端点中,例如:

$ curl localhost:/env
{
"profiles":[],
"configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
"servletContextInitParams":{},
"systemProperties":{...},
...
}

  (名为“ConfigService:<远程存储库的URL>/<文件名>”的属性源包含了具有值"bar"的属性"foo"并且是最高优先级的)

note:属性源名称中的URL是git仓库地址而不是配置服务器的URL。

  

最新文章

  1. 一个百万数量级的mysql实例
  2. network config
  3. windows下安装mingw
  4. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
  5. opsview
  6. Apache ab并发负载压力测试
  7. Web上传文件
  8. Xcode常用快捷键及代码格式刷(缩进)方法-b
  9. hdu 2516 取石子游戏 博弈论
  10. Android-自己定义图像资源的使用(1)
  11. 用Meta 取消流量器缓存实现每次访问都刷新页面方便调试
  12. Ajax及select级联
  13. 理解java值传递与引用传递
  14. 前端JS插件整理
  15. pointer-net
  16. Apache Solr 初级教程(介绍、安装部署、Java接口、中文分词)
  17. android 中的一些资源注解,让编译器帮你检查代码
  18. 全球最大的3D数据集公开了!标记好的10800张全景图
  19. CountDownLatch与join的区别和联系
  20. 添加wifi adb

热门文章

  1. php钩子原理和实现
  2. 浅谈Vue.use
  3. xcode代码提示没了
  4. C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
  5. Docker入门5------生产力工具docker-compose
  6. 关于 spring MVC 配置自动扫描中 use-default-filters 属性
  7. java之jedis使用
  8. python进阶之 网络编程
  9. [js]浏览器同源策略(same-origin policy)
  10. 八、UIViewController们之间的协作——Segue