• SpringCloudConfig分布式配置中心


    alt

    概述

    场景引入

    • 分布式系统面临的配置问题
    • 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
    • SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理......
    • /(ㄒoㄒ)/~~

    关于SpringCloud-Config

    SpringCloudConfig_Structrue_Image.png
    SpringCloudConfig_Structrue_Image.png
    • 是什么

    SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

    • 组成

    SpringCloud Config分为服务端和客户端两部分。

    • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
    • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

    SpringCloudConfig的作用

    • 集中管理配置文件
    • 不同环境不同配置,动态化的配置更新,分环境部署比如
      • dev(开发版)/test(测试版)/prod(发布版)/beta(试运行版)/release(发行版)
    • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
    • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
    • 将配置信息以REST接口的形式暴露
      • post、curl访问刷新均可......
    • 与GitHub/Gitee整合配置
      • 由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式
    • 官网

    image.png

    Config服务端配置与测试

    前期准备

    • 在GitHub/Gitee上创建一个远程库
    image.png
    image.png
    • 获取url地址 https://gitee.com/kobebryant81/springcloud-config.git
    • git clone 拉去到本地 git clone git@giteeGit:/kobebryant81/springcloud-config
      • 此处我本地配置了git多账号映射 giteeGit-->gitee.com
      • 原始命令 git clone git@gitee.com:kobebryant81/springcloud-config

    image.png

    构建模块

    新建Module模块cloud-config-center-3344作为Cloud的配置中心模块CloudConfig Center

    搭建环境

    • pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <parent>
        <artifactId>cloud2023artifactId>
        <groupId>top.ljzstudy.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
      parent>
      <modelVersion>4.0.0modelVersion>

      <artifactId>cloud-config-center-3344artifactId>
      <dependencies>
        <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-config-serverartifactId>
        dependency>
        <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-devtoolsartifactId>
          <scope>runtimescope>
          <optional>trueoptional>
        dependency>
        <dependency>
          <groupId>org.projectlombokgroupId>
          <artifactId>lombokartifactId>
          <optional>trueoptional>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-testartifactId>
          <scope>testscope>
        dependency>
      dependencies>

    project>
    • 1
    • application.yaml配置文件
    server:
      port: 3344

    spring:
      application:
        name:  cloud-config-center #注册进Eureka服务器的微服务名
      cloud:
        config:
          server:
            git:
    #          uri: git@giteeGit:/kobebryant81/springcloud-config #Gitee上面的git仓库名字
              uri: https://gitee.com/kobebryant81/springcloud-config.git #Gitee上面的git仓库名字
              ####搜索目录
              search-paths:
                - springcloud-config
          ####读取分支
          label: master

    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    • 1
    • 此处配置的远程库url若使用ssh协议连接回连接失败,暂时找不出原因,又知道的大佬,还请评论指教一下(●'◡'●)
    • 主启动类添加注解@EnableConfigServer
    package top.ljzstudy.springcloud;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigCenterMain3344 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344.classargs);
        }
    }

    • 1
    • 修改windows系统hosts文件添加地址映射 127.0.0.1 config-3344.com
      • hosts文件地址 C:\Windows\System32\drivers

    1695194372809.png

    测试

    GET http://config-3344.com:3344/master/config-dev.yml
    
    HTTP/1.1 200 
    Content-Type: text/plain
    Content-Length: 74
    Date: Wed, 20 Sep 2023 08:56:06 GMT
    Keep-Alive: timeout=60
    Connection: keep-alive
    
    config:
      info: master branch,springcloud-config/config-dev.yml version=6
    
    Response code: 200; Time: 1359ms; Content length: 74 bytes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    配置读取规则

    • 配置读取规则,SpringCloud官方提供了5种读取的yaml配置文件的规则
    image.png
    image.png
    • properties文件在开发中基本上已经被yaml/yml文件所替代了,因此此处不再深入探究
    • 在启动的actuator中可以看到接口列表
    image.png
    image.png

    成果

    成功实现了用SpringCloud Config通过GitHub获取配置信息

    Config客户端配置与测试

    构建模块

    • 新建cloud-config-client-3355

    搭建环境

    • pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <parent>
        <artifactId>cloud2023artifactId>
        <groupId>top.ljzstudy.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
      parent>
      <modelVersion>4.0.0modelVersion>

      <artifactId>cloud-config-client-3355artifactId>

      <dependencies>
        <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>

        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-devtoolsartifactId>
          <scope>runtimescope>
          <optional>trueoptional>
        dependency>
        <dependency>
          <groupId>org.projectlombokgroupId>
          <artifactId>lombokartifactId>
          <optional>trueoptional>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-testartifactId>
          <scope>testscope>
        dependency>
      dependencies>
    project>
    • 1

    bootstrap.yaml文件

    • 与application.yaml文件的比较
    • applicaiton.yml是用户级的资源配置项
    • bootstrap.yml是系统级的,优先级更加高
    • Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的Application Context 的父上下文。初始化的时候,Bootstrap Context 负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment`。
    • Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。 Bootstrap contextApplication Context有着不同的约定,所以新增了一个bootstrap.yml 文件,保证Bootstrap Context Application Context`配置的分离。
    • 要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
    • 因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml
    • 配置文件
    server:
      port: 3355

    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 #配置中心地址k

    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka

    • 1
    • 配置说明(一一对应)
    image.png
    image.png
    • 编写主启动类
    package top.ljzstudy.springcloud;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

    @EnableEurekaClient
    @SpringBootApplication
    public class ConfigClientMain3355 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientMain3355.classargs);
        }
    }

    • 1
    • 编写业务类(controller)
    package top.ljzstudy.springcloud.controller;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RefreshScope
    public class ConfigClientController {
        @Value("${config.info}")
        private String configInfo;

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

    测试

    image.png
    image.png

    image.png

    成果

    成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

    面临的问题

    问题随时而来,分布式配置的动态刷新问题

    • Linux运维修改GitHub上的配置文件内容做调整
    • 刷新3344,发现ConfigServer配置中心立刻响应
    • 刷新3355,发现ConfigClient客户端没有任何响应
    • 3355没有变化除非自己重启或者重新加载
    • 难到每次运维修改配置文件,客户端都需要重启??噩梦
      • 引入客户端动态刷新Config

    Config客户端之Config动态刷新

    前言

    避免每次更新配置都要重启客户端微服务3355

    Config动态刷新

    对3355模块进行修改

    • pom文件引入actuator监控
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    • 1
    • 修改YML,暴露监控端口
    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    • 1
    • @RefreshScope业务类Controller修改
    package top.ljzstudy.springcloud.controller;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RefreshScope
    public class ConfigClientController {
        @Value("${config.info}")
        private String configInfo;

        @GetMapping("/configInfo")
        public String getConfigInfo() {
            return configInfo;
        }
    }
    • 1
    • 此时,一旦远程库修改配置,传递链路为
      • gitee---> 3344 ---->3355

    测试

    • 启动注册中心7001,配置中心3344和客户端3355
    image.png
    image.png
    image.png
    image.png

    1695202907473.png
    image.png

    • 两次访问version的值均为6
    • 需要模拟运维人员发起post请求手动刷新3355
    POST http://localhost:3355/actuator/refresh
    
    HTTP/1.1 200 
    Content-Type: application/vnd.spring-boot.actuator.v3+json
    Transfer-Encoding: chunked
    Date: Wed, 20 Sep 2023 09:44:25 GMT
    Keep-Alive: timeout=60
    Connection: keep-alive
    
    [
      "config.client.version",
      "config.info"
    ]
    
    Response code: 200; Time: 6922ms; Content length: 39 bytes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1695203151609.png
    1695203151609.png
    • 成功刷新,避免客户端重启

    仍有不足

    • 假如有多个微服务客户端3355/3366/3377。。。。。。
    • 每个微服务都要执行一次post请求,手动刷新?
    • 可否广播,一次通知,处处生效?
    • 我们想大范围的自动刷新,求方法
      • 引入SpringCloudBus消息总线,见下一篇博客

    本文由 mdnice 多平台发布

  • 相关阅读:
    家政按摩预约小程序app应用场景功能介绍
    B2B2C系统亮点是什么?如何助力珠宝首饰企业打造全渠道多商户商城管理体系
    阿里云ACK(Serverless)安装APISIX网关及APISIX Ingress Controller
    Fe3O4纳米粒子/氧化锌纳米粒子/纳米氧化铈/纳米聚乙烯修饰二氧化硅微球表征探究
    简单表达式的计算(两种方法)
    pcd点云格式介绍
    JVM教程
    Allegro如何输出各层PCB视图的PDF文件
    模拟经营微信小游戏-休闲餐厅上线了
    系列七、Nginx负载均衡配置
  • 原文地址:https://blog.csdn.net/ljz66254/article/details/134328697