小编典典

Spring云配置模式匹配配置文件

spring-boot

我正在尝试基于应用程序的不同配置文件来实现spring cloud
config的模式匹配功能。根据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.properties,user-development.properties,user-
test.properties

根据文档-与应用程序名称无关,如果模式匹配* / development,即localhost:8888 / user /
development或localhost:8888 / demo /
development,则我的配置服务器应匹配概要文件模式并获取适当的属性。例如: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>

阅读 244

收藏
2020-05-30

共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

要么

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

模式:* / development。yml文件错误-预期为字母或数字字符,但找到但找到了/。

无法识别概要文件模式git
repo的原因是:尽管spring允许yml文件中以’-‘开头的模式有多个数组值,但是模式匹配器将’-‘作为要匹配的字符串。即它正在寻找一种模式,'-*/development'而不是'*/development'

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

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

 repos:
        development:
         pattern:
          - */development    
          - */staging
2020-05-30