Spring Cloud Config(配置中心)
每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code
一、简介
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。
Spring Cloud Config服务端特性
- HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
- 属性值的加密和解密(对称加密和非对称加密)
- 通过使用@EnableConfigServer在Spring boot应用中非常简单的嵌入。
Config客户端的特性(特指Spring应用)
- 绑定Config服务端,并使用远程的属性源初始化Spring环境。
- 属性值的加密和解密(对称加密和非对称加密)
入门示例:
只要classpath下有Spring Boot Actuator和Spring Config Client,Spring Boot应用就会尝试连接配置服务http://localhost:8888,这个地址是spring.cloud.config.uri的默认地址。如果你想修改这个地址,你可以在bootstrap.[yml或properties]中设置spring.cloud.config.uri或者通过系统属性或者通过环境变量。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Configuration @EnableAutoConfiguration @RestController public class Application { @Value ( "${config.name}" ) String name = "World" ; @RequestMapping ( "/" ) public String home() { return "Hello " + name; } public static void main(String[] args) { SpringApplication.run(Application. class , args); } } |
上面例子中的config.name可以来自本地的配置文件,也可以来自远程的配置服务。默认情况下,远程的配置服务将优先使用。
为了运行你自己的配置服务中心,你可以使用spring-cloud-config-server依赖,和@EnableConfigServer注解。如果你设置了spring.config.name=configserver,应用将会运行在8888端口,并且从一个样本仓库提供数据。你需要设置spring.cloud.config.server.git.uri来指定你自己的配置数据。默认的,它是一个git仓库,也可以配置成本地的文件系统。
二、Spring Cloud Config服务端
服务器为外部配置(键称值对或等效的YAML内容)提供了基于资源的HTTP。它可以在Spring Boot应用中使用@EnableConfigServer内嵌。例子如下:
1
2
3
4
5
6
7
8
|
@SpringBootApplication @EnableConfigServer public class SpringCloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServerApplication. class , args); } } |
像所有的Spring Boot应用一样,它默认运行在8080端口,你可以通过多种方式将其切换到8888端口。最简单的可以设置spring.config.name=configserver(在Config Server的jar包有一个configserver.yml),它设置了一个默认的配置仓库。另外一种方式是使用你自己的application.properties,这也是小编推荐的方式:
1
2
|
server.port: 8888 spring.cloud.config.server.git.uri: git地址 |
git地址中是你的YAML或者properties文件。
环境仓库
你想在哪里存储配置数据?支持这种行为的策略是EnvironmentRepository,它服务于Environment实例。这个Environment是Spring Environment的一个浅副本。Environment通过3个变量被参数化。
- {application}映射客户端的"spring.application.name"
- {profile}映射客户端的"spring.profiles.active"(逗号分隔列表)
- {label}它是服务端的特性,标记版本的一组配置文件
仓库的实现通常表现的像Spring boot加载配置文件一样,"spring.config.name"等于{application}参数, "spring.profiles.active" 等于{profile}参数。profiles的优先规则和正常的规则是一样的,活动的profiles优于默认的。如果有多个profiles,则最后一个胜出。
客户端的配置实例:
1
2
3
4
5
|
spring: application: name: foo profiles: active: dev,mysql |
在Spring Boot应用中,这些参数也可以通过环境变量或者命令行参数设置。
git后端
EnvironmentRepository的默认实现是使用git后端,它对管理更新、物理环境和审核更改非常的方便。要改变仓库的地址,你可以在配置服务端设置"spring.cloud.config.server.git.uri"属性(在application.properties文件中)。如果你用file:开头设置它,它将从本地仓库运行,这样可以在没有服务端的情况下非常快速和简单的启动。这种情况,服务端将直接在本地仓库中运行。为了扩展配置服务并使它高可用,你需要把服务的所有实例指向同一个仓库,因此只有共享文件系统可以工作。即使在这种情况下,最好使用共享文件系统存储库的ssh:协议,以便服务器可以将其克隆并使用本地工作副本作为缓存。
该仓库的实现将HTTP资源中的{label}参数映射到git的标签(提交id、分支名称或者tag)。如果git分支或者tag名称中包含“/”,则HTTP URL中的label要使用特殊字符“(_)”代替。例如:如果分支的名称是foo/bar,则HTTP中的label的格式为foo(_)bar。这个特殊字符也可以用到{application}参数中。
git URI中的占位符
Spring Cloud Config Server支持在git URL中使用占位符,使用{application} 和 {profile}(如果使用{label},请记住它是使用在git标签中的)。因此你可以轻松的支持“一个应用一个仓库”的原则。如下:
1
2
3
4
5
6
|
spring: cloud: config: server: git: uri: https: //github.com/myorg/{application} |
或者一个环境一个仓库的原则,使用{profile}代替{application}。另外在{application}参数中使用特殊字符"(_)"可以支持多组织。
1
2
3
4
5
6
|
spring: cloud: config: server: git: uri: https: //github.com/{application} |
{application}参数的格式为"organization(_)application"。
模式匹配和多仓库
在{application}和{profile}参数中使用模式匹配可以支持更多复杂的需求。模式的格式是一组逗号分隔的{application}/{profile},其中的参数可以使用通配符。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
spring: cloud: config: server: git: uri: https: //github.com/spring-cloud-samples/config-repo repos: simple: https: //github.com/simple/config-repo special: pattern: special*/dev*,*special*/dev* uri: https: //github.com/special/config-repo local: pattern: local* uri: file:/home/configsvc/config-repo |
如果{application}/{profile}没有匹配到任何模式,它将使用默认的仓库地址:spring.cloud.config.server.git.uri。上面的例子中,"simple"仓库匹配的是“simple/*”(它仅仅匹配一个仓库simple,在所有的环境下)。"local"仓库将匹配所有{application}的名字以“local”开头的,并且也是在所有的环境下。“/*”前缀自动添加到所有没有设置{profile}的模式中。
每一个仓库也可以在子目录下存储配置文件,模式匹配也可以用于搜索这些目录,需要制定searchPaths,如下:
1
2
3
4
5
6
7
|
spring: cloud: config: server: git: uri: https: //github.com/spring-cloud-samples/config-repo searchPaths: foo,bar* |
上面的例子中,将在foo和以bar开头的目录中,搜索配置文件。
默认地,服务器在第一次请求配置文件时克隆远程的仓库,服务器也可以配置在启动的时候克隆仓库,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
spring: cloud: config: server: git: uri: https: //git/common/config-repo.git repos: team-a: pattern: team-a-* cloneOnStart: true uri: http: //git/team-a/config-repo.git team-b: pattern: team-b-* cloneOnStart: false uri: http: //git/team-b/config-repo.git team-c: pattern: team-c-* uri: http: //git/team-a/config-repo.git |
在上面的例子team-a的仓库将在服务端启动时进行克隆,其他的仓库将在第一次请求时克隆。
认证
如果远程的git仓库需要用户名和密码,可以参照下面的例子
1
2
3
4
5
6
7
8
|
spring: cloud: config: server: git: uri: https: //github.com/spring-cloud-samples/config-repo username: trolley password: strongpassword |
到此,Spring Cloud Config服务端就介绍到这里,还有一些不常用的功能在这里就不介绍了,大家可以参照spring cloud官网。Spring Cloud Config服务端的代码示例可以参照我的GitHub地址:https://github.com/bigbugliu/spring-cloud-config-server。
三、Spring Cloud Config 客户端
Spring Boot应用可以立即使用Spring Config Server。只要在classpath中有Spring Cloud Config Client的jar包,这个应用就会请求配置的服务端。他将使用绑定的配置服务器(spring.cloud.config.uri中配置的)的属性初始化spring环境。
在某些情况下,如果服务无法连接到配置服务器,则可能希望启动服务失败。如果这是所需的行为,请设置引导配置属性spring.cloud.config.failFast=true
,客户端将以异常停止。
如果您希望配置服务器在您的应用程序启动时可能偶尔不可用,您可以要求它在发生故障后继续尝试。首先,您需要设置spring.cloud.config.failFast=true,然后您需要将spring-retry和spring-boot-starter-aop添加到您的类路径中。默认行为是重试6次,初始退避间隔为1000ms,指数乘数为1.1,用于后续退避。您可以使用spring.cloud.config.retry.*配置属性配置这些属性(和其他)。
每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code
Spring Cloud Config(配置中心)的更多相关文章
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- 微服务SpringCloud之Spring Cloud Config配置中心Git
微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...
- 微服务SpringCloud之Spring Cloud Config配置中心服务化
在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...
- spring cloud --- config 配置中心 [本地、git获取配置文件]
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...
- Spring Cloud Config 配置中心高可用
详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...
- Spring Cloud Config 配置中心
请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...
- 微服务SpringCloud之Spring Cloud Config配置中心SVN
在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...
- SpringCloud学习笔记(7):使用Spring Cloud Config配置中心
简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...
- Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...
- Spring Cloud Config 配置中心 自动加解密功能 jasypt方式
使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用 jasypt-spring-boot- ...
随机推荐
- Hibernate(十四)抓取策略
抓取策略: 抓取策略是当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.Hibernate的抓取策略是Hibernate提升性能的一 ...
- 华硕笔记本的U盘启动
开机以后有两种方式: 1:按住ESC键,在弹出的见面直接选择USB启动进入. 2:按F2进BLOS进入,在boot里面原则第一个,找到USB作为第一启动项,再按F10保存一下即可.
- input file图片上传
<div class="div-title"> <h5>图片上传</h5> <div class="photo-box" ...
- 洛谷P1516 青蛙的约会
题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清 ...
- html之input标签(11)
1.输入框 type=“text” 就是一个简单的输入框 <body> <input type="text"> </body> 2.密码输入框 ...
- linux连接数过多,导致ping包丢包的问题解析
1.首先要明确,无论是tcp, udp, raw等这些都要占用socket, 那么就涉及到连接数的问题. 所以,linux连接数的问题,不仅仅是tcp连接数. 2.查看当前系统中所有的socket 连 ...
- Scrollview嵌套Recyclerview嵌套滑动冲突,导致滑动时会出现卡顿的现象
recyclerView.setLayoutManager(new GridLayoutManager(mContext,2){ @Override public boolean canScrollV ...
- wap2app(九)-- 使用mui.previewImage之后,页面a链接不能跳转
使用Hbuilder的长按保存图片的预览图片之后,页面所有的a链接都不能跳转. 解决办法: 可以使用下面绑定tap利用js跳转,亲测有效. mui('body').on( 'tap' , 'a' , ...
- centos开发环境安装的备忘
#Centos visudo运行普通用户$(whomai)执行sudo操作 http://www.cnblogs.com/xianyunhe/archive ...
- python之模块使用
1.入口 """ 模块测试入口 """ import show_message as sm # 导入方式一 sm.show(sm.__nam ...