SpringCloud H版系列9--Config配置中心


书接上文,继续跟着周阳老师学习SpringCloud的Config,特此整理如下

一、Config配置中心

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。Spring Cloud Config项目是就是这样一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

二、Config特性

Spring Cloud Config 是把应用原本放在本地文件的配置放到github,从而提供更好的管理、发布能力。

Spring Boot Config提供了基于应用、环境、版本(其他的配置中心缺少)的配置管理。

2.1 Spring Cloud Config 优势

基于应用、环境、版本三个纬度管理,每次提交代码,都有版本号,可以跳转到指定版本

配置存储支持Git,后端基于Git存储,有很好的版本支持,支持其他的存储例如本地文件、SVN等

与Spring 无缝集成,支持Spring 里面Environment和PropertySource接口,迁移成本低

2.2Spring Cloud Config 缺点

动态配置能力弱,调整配置需要重新部署,添加代码比较多

治理能力弱,安全审计能力弱

不算严格企业级,适用于小型项目

三、项目实战

3.1 在GitHub或者Gitee上创建公开的项目springcloud-config

本文在Gitee上创建springcloud-config在这里插入图片描述

3.2 新建配置中心模块3344 cloud-config-center3344

本地hosts文件配置如下
在这里插入图片描述
application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/123/springcloud-config
          search-paths:
            - springcoud-config  #gitee上面自己的仓库地址
      label: master
  #15672是管理界面端口,5672是MQ访问的端口
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

#服务注册到eurake地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
#rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: "bus-refresh"
        #通过访问curl -X POST "http://localhost:3344/actuator/bus-refresh"实现3344广播通知3355、3366

ConfigCenterMain3344

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        //springcloud config 为微服务提供集中化资源配置支持,配置服务器为各个不同微服务应用的所有环境
        //提供一个中心化的外部配置
        //这里已经将windows的hosts配置了映射
        //启动7001和3344,访问http://config-3344.com:3344/master/config-dev.yml
        SpringApplication.run(ConfigCenterMain3344.class, args);

        //通过访问curl -X POST "http://localhost:3344/actuator/bus-refresh"实现3344广播通知3355、3366
        //bus-refresh为bootstrap.yml中配置的bus暴露点
        //通过curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355实现定点通知3355
    }
}

启动7001和3344,访问http://config-3344.com:3344/master/config-dev.yml,得到结果和Gitee上面一致。
在这里插入图片描述
在这里插入图片描述

3.3 新建cloud-config-client-3355

当我们github上更新配置后,我们的服务要重新启动才能使用新的配置,否则配置无法生效。
解决方案就是动态的修改配置。使用标签:@RefreshScope
bootstrap.yml

server:
  port: 3355
#bootstrap.yml属于系统级别,优先级高于application
spring:
  application:
    name: config-client
  cloud:
    #config客户端配置
    config:
      label: master  #分支名称
      name: config   #配置文件名称
      profile: dev   #读取后缀名称  上述3个综合:master分支上config-dev.yml配置文件
                      #被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344  #配置中心地址
  #15672是管理界面端口,5672是MQ访问的端口
  rabbitmq:
      host: 127.0.0.1
      port: 5672
      username: guest
      password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点,运维人员修改配置文件比如gitee上配置信息,客户端3355等频繁重启问题
management:
  endpoints:
    web:
      exposure:
        include: "*"

ConfigClientMain3355

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
    /**测试
     * 启动Euraka注册中心7001
     * 启动Config配置中心3344,访问http://config-3344.com:3344/master/config-dev.yml
     * 启动配置中心3355作为Client准备访问,访问http://localhost:3355/configInfo
     *
     * 通过restful风格获取配置信息
     * 访问eurake地址http://eureka7001.com:7001/
     * 实现客户端3355访问3344,通过gitee获取配置信息
     *
     * 结论:发现http://localhost:3355/configInfo和http://config-3344.com:3344/master/config-dev.yml结果内容一致
     * */

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class,args);
    }
}

ConfigClientController

@RestController
@Slf4j         //手动刷新功能
@RefreshScope  //此注解RefreshScope解决每次运维修改配置文件比如gitee上配置信息,客户端3355等频繁重启问题
public class ConfigClientController {
    //注意:此处config.info是https://gitee.com/123/springcloud-config的config-dev.yml中的info内容
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }

    //运维人员修改完gitee配置信息后,手动发送post请求(必须是post),使得3355客户端不用重启获取到最新配置
    //curl -X POST "http://lcoalhost:3355/actuator/refresh"
}

启动Euraka注册中心7001,启动Config配置中心3344和3355,
访问http://config-3344.com:3344/master/config-dev.yml
启动配置中心3355作为Client准备访问,
访问http://localhost:3355/configInfo
在这里插入图片描述
通过restful风格获取配置信息
访问eurake地址http://eureka7001.com:7001/
实现客户端3355访问3344,通过gitee获取配置信息

结论:发现http://localhost:3355/configInfo和http://config-3344.com:3344/master/config-dev.yml结果内容一致
在这里插入图片描述
运维人员修改完gitee配置信息后,手动发送post请求(必须是post),使得3355客户端不用重启获取到最新配置
curl -X POST “http://lcoalhost:3355/actuator/refresh”

四、项目整体

在这里插入图片描述

源码下载地址

spring-cloud-01

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值