spring 配置文件的云配置模式匹配

spring cloud config pattern match for profile

我正在尝试根据应用程序的不同配置文件实现 spring 云配置的模式匹配功能。根据 http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_environment_repository 中的文档,可以根据配置文件匹配存储库。下面是我的配置服务器 application.yml

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: ssh://xxxx@github/sample/cloud-config-properties.git
          repos:
            development:
             pattern:
               -*/development    
               -*/staging 
             uri: ssh://git@xxxgithub.com/development.git
            test:
             pattern: 
               -*/test
               -*/perf
             uri: ${HOME}/Documents/cloud-config-sample-test

我有一个配置客户端应用程序 "user" 并且有 user.properties、user-development.properties、user-test.properties

根据文档 - 无论应用程序名称如何,如果模式匹配 */development i,e localhost:8888/user/development 或 localhost:8888/demo/development my配置服务器应该匹配配置文件模式并获取适当的属性。 例如:http://localhost:8888/demo/development 我应该从 ssh://git@xxxgithub.com/development.git

得到 demo-development.properties

但在我的应用程序中,默认 uri 用于所有配置文件,即我的 属性 文件 demo.properties 是从 uri: ssh://xxxx@github/sample/cloud-config-properties.git

对此有任何指示吗?

编辑: pom.xml

<parent>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-parent</artifactId>
      <version>Brixton.M5</version>
      <relativePath /> 
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
<repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots-continuous</id>
            <name>Spring Snapshots Continuous</name>
            <url>http://repo.spring.io/libs-snapshot-continuous-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

在对 PatternMatching 源代码进行一些调试后,我解决了这个问题:您可以选择两种方式之一。

application.yml
server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: ssh://xxxx@github/sample/cloud-config-properties.git
          repos:
           development:
            pattern: '*/development' ## give in quotes
            uri: ssh://git@xxxgithub.com/development.git

development:
  pattern: xx*/development,*/development ##since it is not allowed to have a value starting with a wildcard( '*' )after pattern I first gave a generic matching but the second value is */development. Since pattern takes multiple values, the second pattern will match with the profile
  uri: ssh://git@xxxgithub.com/development.git

pattern: */development.Error on yml file- expected alphabetic or numeric character, but found but found /.

配置文件模式 git repo 未被识别的原因是:虽然 spring 允许在 yml 文件中以 '-' 开头的模式有多个数组值,但模式匹配器正在使用'-' 作为要匹配的字符串。即它正在寻找模式 '-*/development' 而不是 '*/development'

   repos:
    development:
     pattern:
      -*/development    
      -*/staging 

我观察到的另一个问题是,如果我必须将模式数组提及为 '- */development',我会在 yml 文件上遇到编译错误 - 请注意连字符后的 space(这是为了表明它可以将多个值作为数组保存)并以 '*/development' 开头并出现错误:预期的字母或数字字符,但找到但找到 /

 repos:
        development:
         pattern:
          - */development    
          - */staging