0702-spring cloud config-git仓库配置、用户授权

一、概述

参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_environment_repository

例如:git、svn、基于git本地存储、 本地存储、Vault

二、git

  EnvironmentRepository的默认实现使用Git后端。要更改存储库的位置,可以在配置服务器中设置“spring.cloud.config.server.git.uri”配置属性(例如,在application.yml中)。

优点:版本审计、分布式简单

准备工作

1、新建两个git:

  sample:https://github.com/bjlhx15/spring-cloud-config-test-sample.git

  special:https://github.com/bjlhx15/spring-cloud-config-test-special.git

2、克隆到本地

  在每个仓库中增加application.yml,内容如下:

  sample中的配置:

profile: sample

  special中的配置:

profile: special

  然后提交git。

2.1、Git URI中的占位符

2.1.1、开发

  代码:https://github.com/bjlhx15/spring-cloud/tree/master/config-part/microservice-config-server

  修改配置文件:【注意uri中的通配符{application}】

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bjlhx15/{application}

2.1.1、访问

  http://localhost:8080/spring-cloud-config-test-sample-default.yml

  http://localhost:8080/spring-cloud-config-test-special-default.yml

  可以发现,不同微服务对应不同git配置服务,配置隔离。

2.2、模式匹配和多个存储库

  还有对应用程序和配置文件名称进行模式匹配的更复杂要求的支持。模式格式是带有通配符的{application} / {profile}名称的逗号分隔列表(其中可能需要引用以通配符开头的模式)。例:

在准备工作的special中添加开发【spring-cloud-config-test-special-dev.yml】和测试环境【spring-cloud-config-test-special-test.yml】

内容分别如下:

  spring-cloud-config-test-special-dev.yml

profile: special-dev

  spring-cloud-config-test-special-test.yml

profile: special-test

2.2.1、开发

  代码:https://github.com/bjlhx15/spring-cloud/tree/master/config-part/microservice-config-server

  修改配置文件: 

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bjlhx15/spring-cloud-config-test-repo  #公用的
          repos:
            simple: https://github.com/bjlhx15/spring-cloud-config-test-sample
            special:
              pattern: special*/dev*,*special*/test*
              uri: https://github.com/bjlhx15/spring-cloud-config-test-special

   实际测试出错,正在排查中

Binding to target org.springframework.cloud.config.server.ssh.SshUriProperties(uri=https://github.com/bjlhx15/spring-cloud-config-test-repo hostKeyAlgorithm=null, hostKey=null, privateKey=null, ignoreLocalSshSettings=false, knownHostsFile=null, preferredAuthentications=null, strictHostKeyChecking=true,){repos={special=org.springframework.cloud.config.server.ssh.SshUriProperties(uri=https://github.com/bjlhx15/spring-cloud-config-test-special hostKeyAlgorithm=null, hostKey=null, privateKey=null, ignoreLocalSshSettings=false, knownHostsFile=null, preferredAuthentications=null, strictHostKeyChecking=true,)}} failed:

    Property: spring.cloud.config.server.git.repos[sample]
    Value: https://github.com/bjlhx15/spring-cloud-config-test-sample
    Reason: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.cloud.config.server.ssh.SshUriProperties$SshUriNestedRepoProperties' for property 'repos[sample]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.cloud.config.server.ssh.SshUriProperties$SshUriNestedRepoProperties' for property 'repos[sample]': no matching editors or conversion strategy found

可以查看:org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository,中repos属性

进入org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository.PatternMatchingJGitEnvironmentRepository:查看到pattern和URI

 repo中的pattern属性实际上是一个数组,因此您可以在属性文件中使用YAML数组(或[0],[1]等后缀)绑定到多个模式。如果您要使用多个配置文件运行应用程序,则可能需要执行此操作。

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

2.3、searchPaths

  每个存储库还可以选择将配置文件存储在子目录中,并且可以将搜索这些目录的模式指定为searchPaths。例如在顶层:

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bjlhx15/spring-cloud-config-test-repo
          searchPaths: 
            - foo # foo 路徑
            - bar # bar 路徑        

  访问:

    http://localhost:8080/foo-dev.yml 

    http://localhost:8080/bar-dev.yml

    http://localhost:8080/de-dev.yml

   服务器搜索顶层 “/”和foo子目录中的配置文件,以及名称以“bar”开头的任何子目录。使管理清晰一些

其实也可以使用通配符

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: '{application}'

2.4、cloneOnStart

查看以上代码可知,都是在调用使用时候加载配置

Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-6862549358101599484/application.yml 

增加参数

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          cloneOnStart: true  #全部启动时候加载
          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

三、用户授权

配置用户名密码等

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bjlhx15/spring-cloud-config-test-repo
          username: ***
          password: ***

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值