Spring Cloud Config分布式配置中心

Spring Cloud Config分布式配置中心

Config概述

  • SpringCloud Config解决了在分布式场景下多环境配置文件的管理和维护
  • 使用SpringCloud Config的好处:
    1. 集中管理配置文件
    2. 不同环境不同配置,动态化的配置更新
    3. 配置信息改变时,不需要重启即可更新配置信息到服务
      在这里插入图片描述
Config快速入门
config server服务端搭建

步骤:

  1. 使用gitee创建远程仓库,上传配置文件

  2. 搭建config server模块

  3. 导入config-server依赖

        <!--config-server的依赖-->
    		<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
  4. 编写配置,设置gitee远程仓库地址

    server:
      port: 9527 #默认8888
    
    spring:
      application:
        name: config-server
        #spring cloud config的配置
      cloud:
        config:
          server:
          #git的远程地址
            git:
              uri: https://gitee.com/ww2016002615/qiyun-configs.git
            # username:
            # password:
          label: master #分支配置
    
    
  5. 测试访问远程配置文件

    http://localhost:9527/config-dev.yml

#####config client客户端搭建

步骤:

  1. 导入starter-config依赖

            <!--config-client的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-client</artifactId>
            </dependency>
    
  2. 创建bootstrap.yml文件(必须为此名)

  3. 在bootstrap.yml文件配置config server地址,读取配置文件名称等信息

    #1.配置config-server的地址
    #2.配置获得配置文件的名称等信息
    spring:
      cloud:
        config:
        #配置config-server的地址
          uri: http://localhost:9527
          #配置获得配置文件的名称等信息
          name: config #文件名
          profile: dev #profile指定,config-dev.yml
          label: master #分支
    
  4. 获取配置值

    @Value("${qiyun}")
    private String qiyun;
    
  5. 启动测试

http://localhost:8001/goods/findOne/2

config client刷新

配置信息改变时,不需要重启即可更新配置信息到服务,但是客户端需要进行config client刷新才能生效

步骤:

  1. 在config客户端引入actuator依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  2. 获取配置信息类,添加@RefreshScope注解(谁获取配置信息就在谁那里加)

  3. 添加配置

    #1.配置config-server的地址
    #2.配置获得配置文件的名称等信息
    spring:
      cloud:
        config:
        #配置config-server的地址
          uri: http://localhost:9527
          #配置获得配置文件的名称等信息
          name: config #文件名
          profile: dev #profile指定,config-dev.yml
          label: master #分支
    management:
      endpoints:
        web:
          exposure:
            include: '*'   #refresh
    
    
  4. 使用curl工具发送post请求

    curl -X POST http://localhost:8001/actuator/refresh
    
config集成eureka

yml文件中的uri文件写得太固定,耦合度太强,后期的维护成本太高,所以需要集成eureka

  • config-client配置:

    #1.配置config-server的地址
    #2.配置获得配置文件的名称等信息
    spring:
      cloud:
        config:
        #配置config-server的地址
    #      uri: http://localhost:9527
          #配置获得配置文件的名称等信息
          name: config #文件名
          profile: dev #profile指定,config-dev.yml
          label: master #分支
          #从注册中心中去寻找config-server的地址
          discovery:
            enabled: true
            service-id: CONFIG-SERVER
    management:
      endpoints:
        web:
          exposure:
            include: '*'   #refresh
    
    
  • config-server配置:

    #将自己注册到eureka中
    eureka:
       client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值