• SpringCloudConfig分布式配置中心介绍与搭建使用以及ConfigClient实现动态刷新配置


    场景

    SpringCloud中集成Gateway网关实现路由配置的两种方式以及动态路由:

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124989896

    在上面的基础上学习SpringCloudConfig的使用。

    分布式系统面临的问题

    配置文件较多,各个模块下都有application.yml,无法统一配置。

    环境不同,配置文件不同,比如开发环境、测试环境、生产环境等的配置文件。

    微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置文件才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

    SpringCloud提供了ConfigServer来解决这个问题,我们每一个服务带着一个application.yml,上百个配置文件的管理工作量巨大。

    SpringCloudConfig

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

    SpringCloudConfig分为服务端和客户端两部分。

    服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

    客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

    能做什么

    1、集中管理配置文件。

    2、不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release。

    3、运行期间动态调整配置配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。

    4、当配置发生变化时,服务不需要重启即可感知到配置的变化并应用新的配置。

    5、将配置信息以REST接口的形式暴露。

    与Git整合配置

    由于SpringCloudConfig默认使用Git来存储配置文件(也有其他方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https的访问形式。

    可以将配置文件放在Github或者Gitee之上,然后由运维人员拉取代码仓库的配置文件,然后进行提交更改即可。

    这里使用Gitee示例使用,在此之前确保Gitee拉取代码的相关配置已经做好。

    在Gitee上新建仓库springcloud-config

    然后拉取到本地,并新增三个配置文件,模拟开发、生产、测试环境下的配置文件

    config-dev.yml

    config:
        info: "master branch,springcloud-config/config-dev.yml version=1"

    config-prod.yml

    config:
        info: "master branch,springcloud-config/config-prod.yml version=1"

    config-test.yml

    config:
        info: "master branch,springcloud-config/config-test.yml version=1"

    这三个配置文件大体相同,只有一个明显的标识不用。然后将这三个文件推送到远程仓库中。

     

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    1、上面讲过Config分为服务端和客户端,这里先实现服务端。

    按照新建Eureka Server端的流程

    SpringCloud中集成Eureka实现服务注册(单机Eureka构建):

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124688609

    新建cloud-config-center-3344模块

    修改其pom文件,添加如下依赖

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

    因为这里使用了Eureka作为服务注册中心,所以还需要引入EurekaClient的依赖,完整的pom

    <?xml version="1.0" encoding="UTF-8"?>
    <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>SpringCloudDemo</artifactId>
            <groupId>com.badao</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-config-center-3344</artifactId>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
        </dependencies>
    </project>

    然后新建并修改application.yml配置文件

    server:
      port: 3344
    
    spring:
      application:
        name:  cloud-config-center #注册进Eureka服务器的微服务名
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/badaoliumang/git仓库名字.git #Gitee上面的git仓库名字
            ####搜索目录
              search-paths:
                - springcloud-config
              #username: 如果是私有仓库则需要配置用户名密码
              #password: 如果是私有仓库则需要配置用户名密码
          ####读取分支
          label: master
    
    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka

    注意这里的仓库url使用https的方式,并且如果是私有仓库的话,需要配置用户名和密码。

    还有就是这里的搜索目录search-paths是gitee的仓库名,label是要读取的分支名。

    新建启动类并添加@EnableConfigServer注解

    package com.badao.springclouddemo;
    
    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.class, args);
        }
    }

    启动3344ConfigServer,然后在浏览器中访问

    http://127.0.0.1:3344/master/config-prod.yml

    2、配置读取的规则

    上面读取配置文件的路径中

    master代表分支,后面跟着是文件名,也有其他读取的规则。具体可以参考其官网

    https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

    3、Config客户端配置

    上面配置了Config的服务端并实现读取配置文件,下面配置Config客户端。

    与上面流程一样,新建cloud-config-client3355模块

    修改pom文件添加Config Client的依赖

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

    这里还需要引入web相关的依赖,所以完整pom文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <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>SpringCloudDemo</artifactId>
            <groupId>com.badao</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-config-client3355</artifactId>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    </project>

    新建并修改bootstrap.yml,为什么不是application.yml

    application.yml是用户级的资源配置项。

    bootstarp.yml是系统级的,优先级更高。

    所以可以通过bootstrap.yml来加载Config Server的配置内容。

    而同时再新建application.yml来进行单独的配置。

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          name: config
          profile: dev
          uri: http://localhost:3344
    
    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka

    其中spring.cloud.config是Config的客户端的配置

    label是配置的分支名称,name是配置文件名称,profile是读取后缀名称,uri是配置中心地址。

    上述四个配置组装成

    http://localhost:3344/master/config-dev.yml

    新建启动类并添加Eureka Client的注解

    package com.badao.springclouddemo;
    
    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.class, args);
        }
    }

    新建Controller用来测试获取Config Server的配置

    package com.badao.springclouddemo.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
    public class ConfigClientController
    {
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo()
        {
            return configInfo;
        }
    }

    启动3355,访问如下地址获取3344的配置文件效果

    http://127.0.0.1:3355/configInfo

    4、下面模拟运维人员修改gitee中的配置文件,分别查看Config Server 3344 和 Config Client 是否都能获取到

    可以看到Config Server 3344会动态进行更新配置文件,而Config Client 3355不会动态更新,除非重启3355服务。

    可是不能每次修改配置文件,都要重启每一个Config Client吧。

    5、Config Client配置动态刷新

    借助于actuator的实现。

    SpringBoot中集成Actuator实现监控系统运行状态:

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124272494

    所以需要在pom中引入如下依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>

    前面已经进行引入

    在配置文件中添加暴露端点的配置

    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    完整配置文件

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          name: config
          profile: dev
          uri: http://localhost:3344
    
    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    
    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    然后再Controller上添加@RefreshScope注解

    package com.badao.springclouddemo.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;
    }
    }

    然后再修改了Gitee中的配置文件之后,只需要使用post的请求方式请求如下接口

    http://localhost:3355/actuator/refresh

    即可实现ConfigClient动态刷新配置

     

  • 相关阅读:
    3. Longest Substring Without Repeating Characters
    2. Add Two Numbers
    1. Two Sum
    关于LSTM核心思想的部分理解
    常用正则表达式RE(慕课网_Meshare_huang)
    安装Keras出现的问题
    win系统下如何安装xgboost,开发环境是anaconda,以及这中间需要注意的问题
    Shell基础
    关机与重启命令
    压缩与解压缩命令
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/16328305.html
Copyright © 2020-2023  润新知