弹簧云配置模式匹配配置文件 [英] spring cloud config pattern match for profile

查看:82
本文介绍了弹簧云配置模式匹配配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 服务器:
端口:8888
春季:
云:
配置:
服务器:
git:
uri:ssh://xxxx@github/sample/cloud-config-properties.git
仓库:
开发:
模式:
-* /开发
-* / staging
uri:ssh://git@xxxgithub.com/development.git
测试:
模式:
-* /测试
-* / 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 /开发我的配置服务器应该与配置文件模式匹配并获取适当的属性。
例如: 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>春季快照连续< / name>
< url> http://repo.spring.io/libs-snapshot-continuous-local< / url>
< snapshots>
< enabled> true< / enabled>
< / snapshots>
< / repository>
< repository>
< id"春天里程碑< / id>
< name>春季里程碑< / name>
< url> http://repo.spring.io/libs-milestone-local< / url>
< snapshots>
< enabled> false< / enabled>
< / snapshots>
< / repository>
< repository>
< id> spring-releases< / id>
< name>春季发布< / 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"春天里程碑< / id>
< name>春季里程碑< / name>
< url> http://repo.spring.io/libs-milestone-local< / url>
< snapshots>
< enabled> false< / enabled>
< / snapshots>
< / pluginRepository>
< / pluginRepositories>


解决方案

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

  application.yml 
服务器:
端口:8888
春季:
云:
配置:
服务器:
git:
uri:ssh:// xxxx @ github / sample / cloud-config-properties.git
仓库:
开发:
模式:'* / development'##引用
uri:ssh://git@xxxgithub.com /development.git

OR

 开发:
模式:xx * / development,* / development ##,因为在该模式之后,我首先给出了一个以通配符('*')开头的值,通用匹配,但第二个值为* / development。由于模式采用多个值,因此第二个模式将与配置文件
uri匹配:ssh://git@xxxgithub.com/development.git

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



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

 回购:
开发:
模式:
-* /开发
-* / staging

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

 回购:
开发:
模式:
-* /开发
-* /分段


I am trying to implement the pattern matching feature of spring cloud config based on the different profiles for an application. Based on the documentation in http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_environment_repository it is possible to match the repositories based on profiles. Below is my config server 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

I have an config client application "user" and have user.properties, user-development.properties, user-test.properties

Based on the documentation - irrespective of the application name , if the pattern matches */development i,e localhost:8888/user/development or localhost:8888/demo/development my config server should match the profile pattern and fetch the appropriate properties. Ex: http://localhost:8888/demo/development I should get demo-development.properties from ssh://git@xxxgithub.com/development.git

But in my application, the default uri is used for all the profiles i.e my property file demo.properties is returned from uri: ssh://xxxx@github/sample/cloud-config-properties.git

Any pointers on this?

EDIT: 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>

解决方案

after some debugging on the PatternMatching source code here is how I resolved the issue: You can choose one of the two ways.

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 on yml file- expected alphabetic or numeric character, but found but found /.

The reason the profile pattern git repo was not identified because : although spring allows multiple array values for pattern beginning with a '-' in the yml file, the pattern matcher was taking the '-' as string to be matched. i.e it is looking for a pattern '-*/development' instead of '*/development'.

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

Another issue i observed was, I was getting a compilation error on yml file if i had to mention the pattern array as '- */development' - note space after hyphen(which is meant to show that it can hold multiple values as array) and start with a '*/development' with an error: expected alphabetic or numeric character, but found but found /

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

这篇关于弹簧云配置模式匹配配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆