如何让Spring Cloud Config服务器从特定分支检出配置?

27

我有以下的Spring Cloud Config配置文件application.yml:

spring: 
  application: 
    name: configserver
  cloud: 
    config: 
      server: 
        git: 
          uri: https://xyz@bitbucket.org/xyz/microservices-configs.git
          username: xyz
          password: xyz
          basedir: target/configs
server:
  port: 8881  

以下是我用户微服务的bootstrap.yml文件:

spring: 
  application: 
    name: userservice
  cloud: 
    config: 
      uri: http://localhost:8881/  
场景 - 1
当我像这样在浏览器中访问配置服务器:
http://localhost:8881/development/userservice-development.yml
它会正常地提供文件。而且当我查看basedir即target/config时,我可以看到:
- userservice.yml  
- gateway.yml  

这正是我想要的,因为我只在开发分支中添加了这两个文件。

场景 - 2
当我使用以下命令运行我的用户服务微服务项目时:
mvn clean spring-boot:run -Dspring.profiles.active=development

它从git获取了正确的文件,但它从主分支检出!而不是从开发分支,这正是我期望的。我是否期望正确?(FYI,我在主分支中既有开发版yml又有生产版yml)

那么问题是,我们如何使用配置服务器?是否有任何配置可以设置仅从特定分支获取yml?我相信我们需要设置一些标签,因为根据文档,默认标签为主分支。有人能告诉我如何在上述场景中设置标签吗?

4个回答

51
根据文档,您想在配置客户端中设置的配置为:
spring.cloud.config.label=mybranch

其中 mybranch 是你的 Git 仓库中已存在的分支。


3
谢谢。我将把标签都设置到配置服务器中。那么这是否与配置客户端应用程序中的绑定有关?在决定是使用服务器的还是客户端的 spring.cloud.config 属性时有些令人困惑。 - Aman Gupta
2
我的分支有一个斜杠(/),那我该如何配置呢?我尝试了 spring.cloud.config.label=release/1.0.0,release_1.0.0,release(_)1.0.0 但是不起作用。 - Nikhil Talreja
4
我已经弄清楚了。应该是 release(_)1.0.0 - Nikhil Talreja
6
对我来说,上述的属性没有起作用,当我改为以下内容时,它正常工作了:spring.cloud.config.server.git.default-label = BranchName。我正在使用Spring Boot 2.1.3.RELEASE版本和spring-cloud-config-server 2.1.1.RELEASE版本。 - Tarun Kumar
6
谁会把“分支”称作“标签”?直接称之为“分支”即可,应该是spring.cloud.config.branch - Philip Rego
显示剩余4条评论

23

您可以通过属性spring.cloud.config.server.git.default-label指定配置服务器使用的默认分支(更一般地说,是Git标签),如果客户端未指定标签,请尝试使用此选项。对我来说肯定解决了问题!


17

如果只想在 yml 文件中使用分支,只需进行以下配置:

spring:
  cloud:
    config:
      server:
        git: 
          uri: https://gitlab.com/somerepo.git
          username: someuser
          password: somepass
          default-label: branchname

3
分支名称也不应包含斜杠 /,否则它将无法正常工作。 - Dmitry Bakhtiarov
那么在分支名称中,我们应该用什么来替换斜杠呢? - user5163359
1
@user5163359 在Spring Cloud Config中如何在分支名或标签中转义斜杠字符? - buræquete

15

配置服务器使用配置文件来区分不同的环境。 示例:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

分支使配置不一致。

配置服务器的概念基于12因素配置 (http://12factor.net/config)。

请查看详细原因。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接