SpringCloud-Config

前提:已经配置并启动了Eureka注册中心,假设地址为http://localhost:8205/eureka/(根据实际情况修改地址)

环境:码云(Git),maven,springboot

server(配置中心服务端)

spring:
  application:
    name: base-config
  #配置中心
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/besttop_erp_btspc/config.git
          search-paths: '{application}'
          username: 123@qq.com
          password: passwd

#注册中心
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8205/eureka/

解释
        1、spring.application.name:  配置中心的服务名
        2、spring.application.cloud.config.server.git.uri: Git仓库地址
        3、spring.application.cloud.config.server.git.search-paths:表示配置文件的根路径
            比如:服务service存放在git仓库的config/service/config-dev.yml,此时这里的值就可以为config/service。而图中的'{application}'表示动态取服务客户端的服务名作为路径名。这样的话就可以针对不同的客户端服务分配不同的文件夹。
        4、spring.application.cloud.config.server.git.username:Git账户名
        5、spring.application.cloud.config.server.git.password:Git账户的密码
备注:
        1、注意search-paths后的值有单引号
        2、还可以通过非对称密钥的方式进行鉴权,具体可自己探索。


服务端添加配置中心的pom依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

client(配置中心客户端)
        特别注意:配置中心客户端配置的注册中心应该与服务端配置的注册中心是同一个

spring:
  application:
    name: btspc-metadata
  
  #配置中心客户端配置
  cloud:
    config:
      name: ${spring.application.name}
      profile: local
      label: develop
      fail-fast: true
      discovery:
        enabled: true
        service-id: base-config

#注册中心
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8205/eureka/

解释
        1、fail-fast为true时表示先检查配置中心的配置是否可用
        2、discovery.enable表示是否开启根据服务名发现配置中心
        3、discovery.service-id表示配置中心的服务名称

客户端添加pom依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>



服务btspc-metadata服务在git中的存放路径如下
备注:
        1、文件夹位于仓库的根目录下
        2、文件名的组成方式 (详见客户端配置)  name-profile
      

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值