spring cloud配置模式匹配配置文件

时间:2016-04-07 21:02:43

标签: spring spring-boot cloud config spring-cloud

我正在尝试基于应用程序的不同配置文件实现spring cloud config的模式匹配功能。根据{{​​3}}中的文档,可以根据配置文件匹配存储库。下面是我的配置服务器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,我的配置服务器应匹配配置文件模式并获取适当的属性。 例如:http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_environment_repository 我应该从ssh获得demo-development.properties://git@xxxgithub.com/development.git

但在我的应用程序中,默认的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>

1 个答案:

答案 0 :(得分:1)

在对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

OR

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 yml文件 - 期望的字母或数字字符,但找到但找到/.

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

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

我观察到的另一个问题是,我在yml文件上遇到编译错误,如果我不得不提到模式数组为'- */development' - 在连字符后面注意空格(这意味着它可以保存多个值为数组)并以'* / development'开头,带有错误:预期的字母或数字字符,但找到但找到/

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