Spring Cloud 学习(26) --- Spring Cloud Config(一) 配置中心、实例

本文围绕 Spring Cloud Config 配置中心展开,介绍了其在微服务中的重要性及具备的功能,如集中化管理配置、实时生效等。阐述了配置中心的流转和支撑体系,还说明了 Spring Cloud Config 的组成、存储形式及 git 版工作原理,并给出入门案例,包括配置仓库、服务端和客户端的使用与验证。

Spring Cloud Config 是 Spring Cloud 微服务体系中的配置中心,是微服务中不可或缺的一部分,其能够很好的将程序中配置日益增多的各种功能的开关、参数的配置、服务器的地址等配置修改后实时生效、灰度发布,分环境、分集群管理配置等进行全面的集中化管理,有利于系统的配置管理、维护。

Spring Cloud Config 配置中心

配置中心对比

对比方面重要性SpringCloud ConfigNetflix archaius携程 Apollodisconf
静态配置管理基于 file支持支持
动态配置管理支持支持支持支持
统一管理无,需要 git、数据库等支持支持
多维度管理无,需要 git、数据库等支持支持
变更管理无,需要 git、数据库等
本地配置缓存支持支持
配置更新策略
配置锁支持不支持不支持不支持
配置校验
配置生效时间重启生效、手动刷新手动刷新失效实时实时
配置更新推送需要手动触发需要手动触发支持支持
配置定时拉取支持配置更新目前依赖事件驱动,client 重启或者 server 推送操作
用户权限管理无,需要 git、数据库等支持支持
授权、审核、审计无,需要 git、数据库等界面直接提供发布历史、回滚按钮操作记录存在数据库中,但是无查询接口
配置版本管理git支持操作记录存在数据库中,但是无查询接口
配置合规检测不支持不支持支持(不完整) 
实例配置监控需要结合 spring admin不支持支持支持,可以查看每个配置再哪台机器上加载
灰度发布不支持不支持支持不支持部分更新
告警通知不支持不支持支持邮件方式告警支持邮件方式告警
统计报表不支持不支持不支持不支持
依赖关系不支持不支持不支持不支持
支持 SpringBoot原生支持支持与 SpringBoot 无关
支持 Spring Config原生支持支持与 SpringBoot 无关
客户端支持javajavajava、.netjava
业务系统入侵入侵性弱入侵性弱入侵性弱入侵性弱、支持注解和 xml
单点故障支持 HA 部署支持 HA 部署支持 HA 部署支持 HA 部署、高可用由 zk 提供
多数据中心部署支持支持支持支持
配置界面无,需要 git、数据库等统一界面统一界面

配置中心具备的功能

  • Open API
  • 业务无关性
  • 配置生效监控
  • 一致性 K-V 存储
  • 统一配置实时推送
  • 配合灰度与更新
  • 配置全局恢复、备份、历史
  • 高可用集群

spring cloud 配置中心功能图

配置中心流转

配置中心各流程流转如图:

 

Spring cloud 配置中心流转图

配置中心支撑体系

配置中心的支撑体系大致有两类

  • 开发管理体系
  • 运维管理体系

     

    Spring 开发、运维体系


Spring Cloud Config

Spring Cloud Config 概述

Spring Cloud Config 是一个集中化、外部配置的分布式系统,由服务端、客户端组成,它不依赖于注册中心,是一个独立的配置中心。Spring Cloud Config 支持多种存储配置信息的形式,目前主要有 jdbc、vault、Navicat、svn、git 等形式,默认为 git。

git 版工作原理

配置客户端启动时,会向服务端发起请求,服务端接收到客户端的请求后,根据配置的仓库地址,将 git 上的文件克隆到本地的一个临时目录中,这个目录是一个 git 的本地仓库,然后服务端再读取本地文件,返回给客户端。这样做的好处是:当 git 服务故障或网络请求异常时,保证服务端依然能正常工作。

Spring Cloud Config git 版工作原理


入门案例

config repo

使用 git 做配置中心的配置文件存储,需要一个 git 仓库,用于保存配置文件。 本例仓库地址: https://gitee.com/laiyy0728/config-repo

在仓库中,新建一个文件夹:config-simple,在文件夹内新建 3 个文件:config-simple-dev.ymlconfig-simple-test.ymlconfig-simple-prod.yml

Spring Cloud Simple Config

 

源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-config/spring-cloud-config-simple

config server

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/laiyy0728/config-repo # git 仓库地址
          search-paths: config-simple # 从哪个文件夹下拉取配置
  application:
    name: spring-cloud-config-simple-server
server:
  port: 9090
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigSimpleServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigSimpleServerApplication.class, args);
    }
}

验证 config server

启动 config server,查看 endpoints mappings

 

Spring Cloud Config Endpoints

  • label:代表请求的是哪个分支,默认是 master 分支
  • name:代表请求哪个名称的远程文件
  • profile:代表哪个版本的文件,如:dev、test、prod 等

从 mappings 中,可以看出,访问获取一个配置的信息,有多种方式,尝试获取 /config-simple/config-simple.dev.yml 配置信息:

由接口获取配置详细信息

http://localhost:9090/config-simple/dev/masterhttp://localhost:9090/config-simple/dev

{
    "name": "config-simple",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": "520b379e9c7f2e39bb56e599f914b6c08fe13c06",
    "state": null,
    "propertySources": [{
        "name": "https://gitee.com/laiyy0728/config-repo/config-simple/config-simple-dev.yml",
        "source": {
            "com.laiyy.gitee.config": "dev 环境,git 版 spring cloud config"
        }
    }]
}

由绝对文件路径获取配置文件内容

http://localhost:9090/master/config-simple-dev.ymlhttp://localhost:9090/config-simple-dev.yml

com:
  laiyy:
    gitee:
      config: dev 环境,git 版 spring cloud config

config client

在 config server 中获取配置文件以及成功,接下来需要在 config client 中,通过 config server 获取对应的配置文件

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

application.yml

spring:
  cloud:
    config:
      label: master
      uri: http://localhost:9090
      name: config-simple
      profile: dev
  application:
    name: spring-cloud-config-simple-client
server:
  port: 9091
// 用于从远程 config server 获取配置文件内容
@Component
@ConfigurationProperties(prefix = "com.laiyy.gitee")
public class ConfigInfoProperties {
    private String config;

    public String getConfig() {
        return config;
    }

    public void setConfig(String config) {
        this.config = config;
    }
}


// 用于打印获取到的配置文件内容
@RestController
public class ConfigController {

    private final ConfigInfoProperties configInfoProperties;

    @Autowired
    public ConfigController(ConfigInfoProperties configInfoProperties) {
        this.configInfoProperties = configInfoProperties;
    }

    @GetMapping(value = "/get-config-info")
    public String getConfigInfo(){
        return configInfoProperties.getConfig();
    }

}


// 启动类
@SpringBootApplication
public class SpringCloudConfigSimpleClientApplication {

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

}

验证 config client

启动 config client,观察控制台,发现 config client 拉取配置的路径是:http://localhost:8888 ,而不是在 yml 中配置的 localhost:9090。这是因为 boot 启动时加载配置文件的顺序导致的。boot 默认先加载 bootstrap.yml 配置,再加载 application.yml 配置。所以需要将 config server 配置移到 bootstrap.yml 中

Config client default fetch server

 

bootstrap.yml

spring:
  cloud:
    config:
      label: master  # 代表请求 git 哪个分支,默认 master
      uri: http://localhost:9090 # config server 地址
      name: config-simple # 获取哪个名称的远程文件,可以有多个,英文逗号隔开
      profile: dev # 代表哪个分支

application.yml

spring:
  application:
    name: spring-cloud-config-simple-client
server:
  port: 9091

config client remote server

访问 http://localhost:9091/get-config-info

get config info



作者:laiyy0728
链接:https://www.jianshu.com/p/588084c959f5
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值