参考资料:

  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。

  

spring-cloud-config——Quick Start的更多相关文章

  1. Spring Cloud Config

    Spring Cloud Config provides server and client-side support for externalized configuration in a dist ...

  2. spring cloud连载第二篇之Spring Cloud Config

    Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持.通过Spring Cloud Config你可以有一个统一的地方来管理所有 ...

  3. spring cloud config 入门

    简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...

  4. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  5. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  6. SpringCloud的配置管理:Spring Cloud Config

    演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...

  7. 搭建spring cloud config

    很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...

  8. Spring Cloud Config - RSA简介以及使用RSA加密配置文件

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

  9. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  10. 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效

    本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...

随机推荐

  1. juqery 点击张三触发李四的方法 trigger(); 和 被选元素前插入指定的内容的方法 brfore();

    $('.zc_fabu_img_1').on('click',function(){ $("#upImg img").trigger("click"); }) ...

  2. maven 配置篇 之 settings.xml

    maven2 比起maven1 来说,需要配置的文件少多了,主要集中在pom.xml和settings.xml中.    先来说说settings.xml,settings.xml对于maven来说相 ...

  3. CentOS使用systemctl daemon-reload报错Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: Timeout was reached (g-io-error-quark, 24)解决办法

    CentOS修改了系统启动文件后需要重载报错 systemctl daemon-reload Error getting authority: Error initializing authority ...

  4. 解决 .net core 中 nuget 包版本冲突问题

    今天在一个 asp.net core 项目中遇到了 nuget 包版本冲突的问题,错误信息如下: Version conflict detected for Microsoft.AspNet.WebA ...

  5. TZOJ :2731: 存钱计划(二)

    描述 在TZC,WY存了钱,现在他要去买东西了.店很多,标记为1,2,3,4,5,6....但有的店之间有大路相连,而有的没有路.现在要由一个店到另一个店买东西,中途最少要经过多少个其它的店铺呢? 如 ...

  6. Object.keys()的理解

    Object.keys(obj) 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致. 例子如下: ①.简单的数组( ...

  7. PyQt5的安装及基本配置

    安装PyQt5 注:Pyqt5只支持python3.5以上版本 Mac安装 Mac上使用编译安装的话,后面使用pyinstaller打包可能会出问题,推荐使用命令行一键安装,Linux如果包管理没有的 ...

  8. python框架之Django(12)-认证系统之auth模块

    我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Django作为一个完美主义者的终极框 ...

  9. linux affinity

    现在的CPU几乎都是多核,所以,分配给予进程相同数量的线程是合理的需求 但是,这些线程不一定就均匀跑在这些内核上 所以,我们要指派,“一个线程就运行在一个固定的CPU内核上” //test.c #de ...

  10. Cocos2d-js3.3 模态对话框的实现

    首先,先了解一下什么是模态对话框,百度百科的给出了下面一个定义: 模态对话框(Modal Dialogue Box,又叫做模式对话框),是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话 ...