1. 程式人生 > >0702-spring cloud config-git倉庫配置、用戶授權

0702-spring cloud config-git倉庫配置、用戶授權

.config ram authent set 簡單 Go 內容 -a 版本

一、概述

參看地址: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: ***

0702-spring cloud config-git倉庫配置、用戶授權