购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

3.5 其他仓库的实现配置

1.配置Git

在应用配置文件与特定配置文件中可以通过正则表达式来支持更为复杂的情况。在{application}/{profile}中可以使用通配符进行匹配,如果有多个值可以使用逗号分隔,配置文件示例如下:


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配置的默认URI。

上面例子中pattern属性是一个YAML数组,也可以使用YAML数组格式来定义。这样可以设置成多个配置文件,示例如下:


spring:
    cloud:
        config:
            server:
                git:
                    uri: https://github.com/spring-cloud-samples/config-repo
                    repos:
                        development:
                        pattern:
                            - */development
                            - */staging
                        uri: https://github.com/development/config-repo
                    staging:
                        pattern:
                            - */qa
                            - */production
                        uri: https://github.com/staging/config-repo

每个资源库有一个可选的配置,用来指定扫描路径,示例如下:


spring:
    cloud:
        config:
            server:
                git:
                    uri: https://github.com/spring-cloud-samples/config-repo
                    searchPaths: foo,bar*

这样系统就会自动搜索foo的子目录,以及以bar开头的文件夹中的子目录。

默认情况下,当第一次请求配置时,系统复制远程资源库。系统也可以配置成一启动就复制远程资源库,示例如下:


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的资源库会在启动时就从远程资源库进行复制,其他的则等到第一次请求时才从远程资源库复制。

2.配置权限与HTTPS

如果远程资源库设置了权限认证,则可以如下配置:


spring:
    cloud:
        config:
            server:
                git:
                    uri: https://github.com/spring-cloud-samples/config-repo
                    username: trolley
                    password: strongpassword

如果不使用HTTPS和用户认证,可以使用SSH URI的格式。例如,git@github.com:configuration/cloud-configuration,这就需要先有SSH的key。这种方式系统会使用JGit库进行访问,可以去查看相关文档。可以在~/.git/config中设置HTTPS代理配置,也可以通过JVM参数-Dhttps.proxyHost、-Dhttps.proxyPort来配置代理。

提示 用户不知道自己的~/.git目录时,可以使用git config--global来指定。例如:git config--global http.sslVerify false。

3.配置SVN

如果希望使用SVN充当配置仓库来替换Git,配置也与Git类似,同样支持账户、密码、搜索路径等配置,这里不再赘述,SVN配置示例如下:


spring:
    cloud:
        config:
            server:
                svn:
                    uri: https://subversion.assembla.com/svn/spring-cloud-config-repo/
                #git:
                #  uri: https://github.com/pcf-guides/configuration-server-config-repo
                default-label: trunk
profiles:
    active: subversion

4.配置本地仓库

如果希望配置仓库从本地classpath或者文件系统加载配置文件,可以通过spring.profiles.active=native开启。默认从classpath中加载,如果使用“file:”前缀加载文件系统,则从本地路径中加载。当然也可以使用${}样式的环境占位符,例如:file:///${user.home}/config-repo。 MMSWcL85r5Kw2EC9woQVxCHPVArs1sTbWuWwDAxumNSIeJfRGE0LCFRd5tsthuAx

点击中间区域
呼出菜单
上一章
目录
下一章
×