Spring Cloud Config

来源:互联网 发布:python retry 编辑:程序博客网 时间:2024/05/01 12:39

发博词

需要用过的人看。不是入门文档。

Environment 和 PropertySource

简单点说PropertySource和Environment 的区别是,假设配置文件中有Placeholder,那么你从PropertySource中获取的值是带Placeholder的,从Environment中获取的值是替换了Placeholder之后的值;
一般的实现是Environment会持有一个PropertySource的列表;
参考:
【Spring4揭秘 基础2】PropertySource和Enviroment
Spring3.1新属性管理API:PropertySource、Environment、Profile

架构

Spring Cloud Config的架构非常简单,一个Server,一个client。Server提供了多个基于HTTP协议的接口工Client调用获取信息。

理解几个概念

  1. spring cloud config label不是只指的branch,指定commitid、branch和tag的值都行,所以这些里面不要有重名的;
  2. 如果配置config-server也从远程拉取信息的话,需要将configserver应用的配置都放到bootstrap.yml
  3. 下载下来的库在/tmp/config-repo-,系统的临时文件夹/config-repo-随机id,basedir可以手动指定,最好手动指定一个,临时文件夹,有些操作系统会临时清理
  4. 因为网络的原因,所有想链接到config server的客户端都需要一个bootstrap.yml文件,spring.cloud.config.uri这个文件必须要有这个配置,而我们一般是用Discovery模式,只需要指定serviceid即可
  5. 我们已经知道application-*.yml文件的优先级了,那么本地远程都有配置文件的情况下,各自的优先级是远程优先级最高,其次是本地application系列文件,bootstrap优先级最低
  6. {application} 值是客户端的 “spring.application.name” 属性的值
  7. {profile} 的值是 客户端的”spring.profiles.active”的值,逗号分隔
  8. {label} 的值在configServer端设置,which is a server side feature labelling a “versioned” set of config files.
  9. git label (commit id, branch name or tag) “/”=》(_)
  10. Git URI可用变量:{application} and {profile} (and {label},客户端可控的变量是{application} and {profile},label是服务器端控制的,如何使用{application} and {profile}也是服务器端控制的

ConfigServer endpoint

name-profiles.properites,yml,json
label/name-profile.
name/profile/label
name/profiles
label/name-profile.pyj
name/profile/label
name/profile/label

这两个接口是一个,上面这个是下面的label=null的版本,也就是label的默认值版本
/{name}-{profiles}.yml /{name}-{profiles}.yaml
/{label}/{name}-{profiles}.yml /{label}/{name}-{profiles}.yaml

label applicationname profile path resolvePlaceholders
仓库,仓库下的目录(searchPaths)
字符串匹配

label 在前面
name-profile在前面
json,yml,ymal,proprieies结尾

  1. 其实是可以控制这五个部分
public Environment(Environment env) {       this(env.getName(), env.getProfiles(), env.getLabel(), env.getVersion(), env.getState());}

要使用ConfigServer注意点
1. spring.application.name git仓库的名字要有规律,能对应起来
2. git仓库名字和配置文件所在目录也要能对应起来

使用模式

通过在url和searchpaths上使用application、profile和label,产生了下面三种常用的方式:
1. all Instances in one repo
2. one repo per application
3. one repo per profile

整合Spring Cloud Bus

  1. 使用属性的地方记得加@RefreshCope注解
  2. configserver每次都刷新,不管想刷新谁
原创粉丝点击