spring cloud config学习二:配置仓库

git配置仓库

spring cloud config的服务端,对于配置仓库的默认实现采用了git。git非常适用于存储配置内容,它可以非常方便的使用各种第三方工具来对内容进行管理更新和版本化,同时git仓库的hook功能还可以帮助我们实时的监控配置内容的修改。其中,git自身的版本控制功能正是其他一些配置中心所欠缺的,通过git进行存储意味着,一个应用的不同部署实例可以从spring cloud config的服务端获取不同的版本配置,从而支持一些特殊的应用场景。

由于spring cloud config中默认使用git,所以我们只需要在config server中的application.properties中设置spring.cloud.config.server.git.uri属性,比如下面的配置:

spring:
  application:
    name: config-server-git
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/zhihaomiao/config-repo-demo
          username: 
          password: 
          search-paths: config-repo

如果我们将该值通过file://前缀来设置为一个文件地址(在window系统中,要使用file:///来定位文件内容),那么它将以本地仓库的方式运行,这样我们就可以脱离git服务端来快速进行调试与开发,比如:因为在快速入门的时候我们知道配置git仓库的时候读取配置会从git远程仓库中git clone到本地,我的控制台日志告诉我下载到本地的/var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/文件夹下(当然自己也可以git clone远程的配置信息到本地),所以如下配置(spring.cloud.config.server.git.uri=file://${user.home}/config-repo)

spring:
  application:
    name: config-server-git
  cloud:
    config:
      server:
        git:
          uri: /var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/config-repo

其中,${user.home}代表当前用户的所属目录,file://配置的本地文件系统方式虽然对于本地开发调试时使用非常方便,但是该方式也仅用于开发与测试,在生产环境中务必搭建自己的git仓库来存储配置资源。

With VCS based backends (git, svn) files are checked out or cloned to the local filesystem. By default they are put in the system temporary directory with a prefix of config-repo-. On linux, for example it could be /tmp/config-repo-<randomid>. Some operating systems routinely clean out temporary directories. This can lead to unexpected behaviour such as missing properties. To avoid this problem, change the directory Config Server uses, by setting
spring.cloud.config.server.git.basedir or spring.cloud.config.server.svn.basedir to a directory that does not reside in the system temp structure.

使用基于VCS的后端(git,svn)文件被检出或克隆到本地文件系统。 默认情况下,它们放在系统临时目录中,前缀为config-repo-。 在linux上,例如可以是/tmp/config-repo- <randomid>。 一些操作系统会定期清除临时目录。 这可能会导致意外的行为,例如缺少属性。 为避免此问题,请通过将spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir设置为不驻留在系统临时结构中的目录来更改Config Server所使用的目录。

占位符配置url

{application},{profile},{label}这些占位符除了用于标识配置文件的规则之外,还可以用于config Server中对git仓库地址的url配置。比如我们可以通过{application}占位符实现一个应用对应一个git仓库目录的配置效果,具体配置实现:

spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/{application}
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

其中,{application}代表了应用名,所以当客户端应用向config server发起获取配置的请求时,Config server会根据客户端的spring.application.name信息来填充{application}占位符以定位配置资源的存储位置,从而实现根据微服务应用的属性动态获取不同的配置。另外,在这些占位符中,{label}参数较为特别,如果git的分支和标签名包含"/",那么{label}参数在http的url中应该使用"(_)"来代替,以避免改变了url含义,指向到其他的url资源。

当我们使用git作为配置中心来存储各个微服务应用配置文件的时候,该功能会变得非常有用,通过在url中使用占位符可以帮助我们规划和实现通用的仓库配置,比如,下面的规划:

  • 代码库:使用服务名作为git仓库名称,比如用户服务的代码库http://git.oschina.net/zhihaomiao/user-service
  • 配置库:使用服务名加上-config后缀作为git仓库名称,比如用户服务的配置库地址位置是http://git.oschina.net/zhihaomiao/user-service-config

这时,我们就可以使用spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/{application}-config配置,来同时匹配多个不同服务的配置仓库。

demo
首先在git上建立一个仓库,仓库名为 user-service-config

具体的配置文件如下:


为不同的环境配置不同的配置信息等等:

spring:
  datasource:
    username: user-dev

check:
  uri: default-1.0

定义config server中的配置文件为:

spring:
  application:
    name: config-server-git
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/zhihaomiao/{application}-config
          username: zhihao.miao
          password: 13579qwertyu
server:
  port: 9090

然后定义一个user-service服务,配置文件bootstrap.yml文件如下:

spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://localhost:9090
      profile: pro
      label: master
server:
  port: 7070

user-service中定义UserController

@RestController
@RequestMapping("/user")
public class UserController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${check.uri}")
    private String checkurl;

    @GetMapping("/index")
    public String index(){
        log.info("username="+username+",check.uri=="+username);
        return "username="+username+",check.uri==="+checkurl;
    }
}

访问:

http://localhost:7070/user/index

说明
这种配置方式我认为是最好的,每个服务(user-service)对应一个git上的仓库(命名为user-service-config),对应的微服务项目代码可以在git上建立(user-service)仓库,将代码和配置很好的分离开来,当然还可以在config server中这样配置(http://git.oschina.net/zhihaomiao/{application}-config-{profile}),一个环境对应git上的一个仓库,不过太细粒度了,不建议这么使用。

我们发现在快速入门中的配置是在通过一个git仓库中定义不同的{application}-{profile}.yml,这样也是一种多项目配置文件的管理方式,关于哪种配置更加适合,当然还是具体问题具体分析,没有最好的方式,只有最合适的方式。

参考资料
Git Backend-Placeholders in Git URI

配置多个仓库

config server除了可以通过applicationprofile模式来匹配配置仓库之外,还支持通过带有通配符的表达式来匹配,以实现更为复杂的配置要求。并且当我们有多个匹配规则的时候,还可以通过逗号来分割多个{application}/{profile}配置规则,比如:

spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/config-repo

spring.cloud.config.server.git.repos.dev.pattern=dev/*
spring.cloud.config.server.git.repos.dev.uri=file://home/git/config-repo

spring.cloud.config.server.git.repos.test=http://git.oschina.net/test/config-repo

spring.cloud.config.server.git.repos.prod.pattern=prod/pp*,online/oo*
spring.cloud.config.server.git.repos.prod.uri=http://git.oschina.net/prod/config-repo

上述配置内容通过配置内容spring.cloud.config.server.git.uri属性,指定了一个默认的仓库位置,当使用{application}/{profile}模式未能匹配到合适的仓库时,就将在该默认仓库位置下获取配置信息。除此之外,还配置了三个仓库,分别是devtestprod。其中,dev仓库匹配dev/*的模式,所以无论profile是什么,它都能匹配application名称为dev的应用。并且我们可以注意到,它存储的配置文件位置还采用了config server的本地文件系统中的内容。对于此位置,我们可以通过访问http://localhost:9090/dev/profile的请求来验证到该仓库的配置内容,其中profile可以是任意值。而testprod仓库均使用git仓库的存储,并且test仓库未配置匹配规则,所以它只匹配application名为test的应用;prod仓库则需要匹配applicationprod并且profilepp开头,或者applicationonline并且profileoo开头的应用和环境。

当配置多个仓库的时候,config server在启动的时会直接克隆第一个仓库的配置库,其他的配置只有在请求时才会克隆到本地,所以对于仓库的排列可以根据配置内容的重要程度有所区分。另外,如果表达式是以通配符开始的,那么需要使用引号将配置内容引起来。

注意

  • 这种方式的配置肯定是不好的,造成管理的混乱,spring.cloud.config.server.git.uri配置是默认的回退配置,如果其他条件都不符合或者是配置的属性在其匹配的模式中并没有得到,那么就会去spring.cloud.config.server.git.uri去匹配。
  • 本列中的repos后面的dev,test,prod都是对应着application应用名称。
  • 上面的pattern配置如果在yml中配置的话,那么还有另外一种写法,可以看官网,使用-这边就不展示了。

参考资料
Pattern Matching and Multiple Repositories

cloneOnStart的用法

By default the server clones remote repositories when configuration is first requested. The server can be configured to clone the repositories at startup. For example at the top level:

默认情况下,首次请求配置时,服务器克隆远程存储库。 服务器可以配置为在启动时克隆存储库。 例如在顶层:

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

In this example the server clones team-a’s config-repo on startup before it accepts any requests. All other repositories will not be cloned until configuration from the repository is requested.

在此示例中,服务器在启动之前克隆了team-a的config-repo,然后它接受任何请求。 所有其他存储库将不被克隆,直到请求时才发起克隆的操作。

cloneOnStart也可以配置在repos下,这个时候就会将下面的所有应用的配置文件在服务启动的时候克隆到本地。

好处

Setting a repository to be cloned when the Config Server starts up can help to identify a misconfigured configuration source (e.g., an invalid repository URI) quickly, while the Config Server is starting up. With cloneOnStart not enabled for a configuration source, the Config Server may start successfully with a misconfigured or invalid configuration source and not detect an error until an application requests configuration from that configuration source.

在配置服务器启动时设置要克隆的存储库可以帮助在配置服务器启动时快速识别配置错误的配置源(例如,无效的存储库URI)。 使用cloneOnStart未启用配置源时,配置服务器可能启动成功配置错误或无效的配置源,并且不会检测到错误,直到应用程序从该配置源请求配置时才发现配置错误。

参考资料
cloneOnStart
Placeholders in Git Search Paths

子目录存储

除了支持占位符配置,多仓库配置之外,config server还可以将配置文件定位到git仓库的子目录中。我们在快速入门中的我们除了配置spring.cloud.config.server.git.uri之外海配置了另外一个参数:spring.cloud.config.server.git.search-paths,通过这个参数可以实现在http://git.oschina.net/zhihaomiao/config-repo-demo仓库的config-repo子目录下实现配置的存储。

spring:
  application:
    name: config-server-git
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/zhihaomiao/config-repo-demo
          username: 
          password: 
          search-paths: config-repo
server:
  port: 9090

通过上面的配置,我们可以实现http://git.oschina.net/zhihaomiao/config-repo-demo仓库下,一个应用一个目录的效果。

对于spring.cloud.config.server.git.search-paths参数的配置也支持使用{application},{profile}{label}占位符,比如:

spring:
  application:
    name: config-server-git
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/zhihaomiao/config-repo-demo
          username: 
          password: 
          search-paths: {application}
server:
  port: 9090

这种方式也可以一个服务一个目录,这样就可以在一个仓库中管理多个服务的配置,这种方式也比较好。

参考资料
Pattern Matching and Multiple Repositories
Placeholders in Git Search Paths

访问权限

config server在访问git仓库的时候,若采用http的方式进行认证,那么我们需要增加usernamepassword属性来配置账户,比如快速入门的demo

 spring:
  application:
    name: config-server-git
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/zhihaomiao/config-repo-demo
          username: 
          password: 
          search-paths: config-repo
server:
  port: 9090

若不采用http的认证方式,我们也可以采用ssh的方式,通过生成key并在git仓库中进行配置匹配以实现访问。

svn配置仓库

config server除了支持git仓库之外,也能使用svn仓库,只需要如下配置。

  • 在pom.xml中引入svn的依赖配置,让config server拥有读取svn内容的能力:
  • application.properties中使用svn的配置属性来指定svn服务器的位置,以及访问的账户名与密码:
spring.cloud.config.server.svn.uri=svn://localhost:443/didispace/config-repo
spring.cloud.config.server.svn.username=username
spring.cloud.config.server.svn.password=password

通过上面的配置修改,config server就可以使用svn作为仓库来存储配置文件了,对于客户端来说,这个过程是透明的,所以不需要做任何变动。

本地仓库

在使用了gitsvn仓库之后,文件都会在config server的本地文件系统中存储一份,这些文件默认会被存储以config-repo为前缀的临时目录中,比如/tmp/config-repo-<随机数>的目录。由于其随机性以及临时目录的特性,可能会有一些不可预知的后果,为了避免将来可能会出现的问题,最好的方法就是指定一个固定的位置来存储这些重要信息。我们只需要通过spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir来配置一个我们准备好的目录即可。

本地文件系统

spring cloud config也提供了一种不适用git仓库或svn仓库的存储方式,而是使用本地文件系统的存储方式来保存配置信息。实现方式也简单,只需要配置属性spring.profile.active=native,config server会默认从应用的src/main/resource目录下搜索配置文件。如果需要指定搜索配置文件的路径,我们可以通过spring.cloud.config.server.native.searchLocations属性来指定具体的配置文件位置。

虽然spring cloud config提供了这样的功能,但是为了支持更好的内容管理和版本控制等强大功能,还是推荐使用git仓库的方式

本博客代码
代码地址
配置仓库
user服务配置仓库
order服务配置仓库

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容