SpringCloud(十二):SringCloud Config-配置Git仓库详解

本文详细介绍了如何配置Git仓库以支持Spring Cloud Config Server,包括版本审计、分布式配置、模式匹配、搜索路径、cloneOnStart属性使用、账号密码配置及占位符应用。通过这些配置,可以实现灵活高效的微服务配置管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Git仓库配置详解

  • 使用git的几个好处:
    • 可以做版本审计:做了修改可以查看历史记录,查看是谁修改的等;
    • 用来做分布式等都比较方便,像用本地文件存储,就不能高可用,除非再弄一个nfs或者其他的分布式的文件系统
    • 官方也建议使用git

参考地址:Finchley.SR2文档

基础使用方式

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mmzs/microservice-spring-cloud-config-test
复制代码

通配符

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
		  # {application}表示根据应用名称寻找配置信息
          uri: https://gitee.com/mmzs/{application}
复制代码

模式匹配和多个存储库

模式匹配
server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
		  # 公用;即当simple和special都匹配不到时,就是用该仓库下的配置信息
          uri: https://gitee.com/mmzs/microservice-spring-cloud-config-test      
            simple: https://gitee.com/mmzs/simple
            special:
			  # 请求时使用:localhost:8080/mmzs/special-dev.properties
			  # 请求时使用:localhost:8080/mmzs/special-test.properties
              pattern: special*/dev*,special*/test*
              uri: https://gitee.com/mmzs/special
复制代码
搜索路径
server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mmzs/microservice-spring-cloud-config-test      # 公用
          search-paths:
            - foo   # foo路径
            - bar   # bar路径
复制代码
cloneOnStart属性的使用
server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mmzs/microservice-spring-cloud-config-test      # 公用
		  # 默认是false;即启动时不会连git仓库,把需要的资源都下载下来;而是首次请求的时候才下载
          clone-on-start: true  
          repos:
            simple: https://gitee.com/mmzs/simple
            special:
              pattern: special*/dev*,special*/test*
              uri: https://gitee.com/mmzs/special
              cloneOnStart: false   # 默认是false
复制代码

账号密码配置

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mmzs/microservice-spring-cloud-config-test
          username: xxx
          password: xxx
复制代码

占位符在Git搜索路径中的使用

Spring Cloud Config Server还支持带有占位符的搜索路径,用于{application}和{profile}(以及{label},如果需要),如以下示例所示:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mmzs/microservice-spring-cloud-config-test
          searchPaths: '{application}'
复制代码

上面的配置导致在存储库中搜索与目录(以及顶层)同名的文件,通配符在带占位符的搜索路径中也有效(搜索中包含任何匹配的目录)。

转载于:https://juejin.im/post/5cfdcecf51882563ed6ad9eb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值