配置文件的 Spring Cloud 配置模式匹配

标签 spring spring-boot cloud config spring-cloud

我正在尝试根据应用程序的不同配置文件实现 Spring Cloud 配置的模式匹配功能。基于 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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44232d30043c3c3c232d302c31266a272b29" rel="noreferrer noopener nofollow">[email protected]</a>/development.git
            test:
             pattern: 
               -*/test
               -*/perf
             uri: ${HOME}/Documents/cloud-config-sample-test

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

根据文档 - 无论应用程序名称如何,如果模式匹配 */development 即 localhost:8888/user/development 或 localhost:8888/demo/development 我的配置服务器应该匹配配置文件模式并获取适当的属性。 例如:http://localhost:8888/demo/development 我应该从 ssh://[email protected] 获取 demo-development.properties/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>

最佳答案

对 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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e1eff2c6fefefee1eff2eef3e4a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>/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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e69677a4e76767669677a667b6c206d6163" rel="noreferrer noopener nofollow">[email protected]</a>/development.git

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

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

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

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

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

关于配置文件的 Spring Cloud 配置模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36487146/

相关文章:

java - Delphi XE5 中的谷歌云消息传递?

java - 使用 JPA native 查询和 MemSql 选择 json 列

spring - 使用 Spring Boot 实现 JsonApi

java - Thymeleaf Switch-Case、嵌入类和辅助变量

java - 没有 EntityManager 具有可用于当前线程的实际事务

sql - 我们可以将本地 SQL Server 数据库中的表连接到 Azure Delta Lake 中 Delta 表中的表吗?我有什么选择

没有 id 或名称的 Spring bean

java - 如何使用过滤器进行分页

java - Logback RollingFileAppender FileNotFoundException

azure - Windows Azure 上的 JavaEE 技术堆栈?