• Spring Cloud Config分布式配置中心


    一、简介

    1.1 分布式系统面临的配置问题

      微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。如果没有的话,我们每个微服务自己带着一个application.yml, 上百个配置文件的管理…特别繁琐。所以Spring Cloud提供了ConfigServer来解决这个问题。

    1.2 Spring Cloud Config

    在这里插入图片描述
      Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。Spring Cloud Config分为服务端和客户端两部分。
      服务端也称为分布式配置中心,它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
      客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

    1.3 作用

    • 集中管理配置文件
    • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
    • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
    • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
    • 将配置信息以REST接口的形式暴露,post、curl访问刷新均可…

    二、Config服务端配置与测试

    2.1 与Github整合配置

      由于Spring Cloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
    2.1.1 用你自己的账号在Github上新建一个名为sprincloud-config的新Repository,并创建config-dev.yml,config-prod.yml,config-test.yml,注意文件格式必须为 xxx-xxx.yml格式
    2.1.2 本地硬盘上新建git仓库并clone,如在本地地址:D:\SpringCloud2020使用clone 命令

    git clone ”上面新建仓库地址“
    

    此时在本地D盘符下D:\SpringCloud2020\springcloud-config可以看到和仓库里面一样的文件
    在这里插入图片描述
    后期如果要修改,只需要在GitHub修改在拉取到本地即可。
    2.1.3 新建Module模块cloud-config-center-3344它既为Cloud的配置中心模块cloudConfig Center
    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>cloud2020</artifactId>
            <groupId>com.atguigu.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-config-center-3344</artifactId>
    
        <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>
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </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>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    yml 文件:

    server:
      port: 3344
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri:  填写你自己的github路径
              search-paths:
                - springcloud-config
          label: master
    eureka:
      client:
        service-url:
          defaultZone:  http://localhost:7001/eureka
    

    2.1.4 主启动类使用@EnableConfigServer 开启配置中心

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

    2.1.5 测试通过Config微服务是否可以从Github上获取配置内容
    在GitHub几个yml文件中添加一些内容,如

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

    通过以下在浏览器看是否能访问到上面的内容

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

    如果访问得到,则代表配置成功。

    2.2 配置读取规则

    2.2.1 /{label}/{application}-{profile}.yml(最推荐使用这种方式)

    返回原配置文件中的内容,按原格式
    如master 分支:

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

    dev 分支:

    http://localhost:3344/dev/config-dev.yml
    http://localhost:3344/dev/config-test.yml
    http://localhost:3344/dev/config-prod.yml
    

    2.2.2 /{application}-{profile}.yml(默认为master分支)

    http://localhost:3344/config-dev.yml
    http://localhost:3344/config-test.yml
    http://localhost:3344/config-test.yml
    http://localhost:3344/config-xxxx.yml(不存在的配置)
    

    如果访问一个不存在的配置,如果上面的 GitHub 配置成功,则会返回 {}

    2.2.3 /{application}-{profile}[/{label}]

    以 json 格式返回原文件中的内容

    http://localhost:3344/config/dev/master
    http://localhost:3344/config/test/master
    http://localhost:3344/config/prod/master
    

    配置细节:
    /{application}-{profiles}.yml
    /{label}/{application}-{profiles}.yml
    |abel: 分支(branch)
    application:服务名
    profiles: 环境(dev/test/prod)

    三、Config客户端配置与测试

    3.1 新建微服务

    3.1.1 新建cloud-config-client-3355,作为客户端服务
    pom:

    <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>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    3.1.2 新建 bootstrap.yml 加载Config 服务端的配置文件

    • applicaiton.yml是用户级的资源配置项
    • bootstrap.yml是系统级的,优先级更加高

      Spring Cloud会创建一个"Bootstrap Context" ,作为Spring应用的Application Context的父上下文。初始化的时候,"Bootstrap Context"负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。Bootstrap 属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.ym|文件,保证Bootstrap Context和Application Context配置的分离。要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.ymI优先级高 于application.yml。

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master #分支名称
          name: config  #配置文件名称
          profile: dev  #后缀名称
          uri: http://localhost:3344 #配置中心服务端地址
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    

    3.1.3 新建启动类

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

    3.1.4 新建业务类,用来测试客户端通过服务端能否获得全局配置文件信息

    @RestController
    public class ConfigClientController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo(){
            return configInfo;
        }
    }
    

    3.1.5 测试
      修改config-dev.yml配置并提交到GitHub中,比如修改版本号version。
    启动Eureka 注册中心7001,然后启动配置中心的服务端和客户端。
    先通过服务端自测,看能否获取到GitHub上的全局配置文件:

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

    然后使用客户端调用上面写的业务方法,看能否获取到同样的信息。

    http://localhost:3355/configInfo
    

    经测试可得:成功实现了客户端3355访问SpringCloud Config3344通GitHub获取配置信息
    但是还存在一个问题:分布式配置的动态刷新
    Linux运维修改GitHub上的配置文件内容做调整,刷新3344,发现ConfigServer配置中心立刻响应,刷新3355,发现ConfigServer客户端没任何响应,3355没有变化除非自己重启或者重新加载,难道每次运维修改配置文件,客户端都需要重启??噩梦。

    3.2 Config客户端之动态刷新—避免一直重启

    3.2.1 客户端的pom 必须依赖spring 的健康检查actuator监控。
    3.2.2 修改bootstrap.yml文件,暴露监控端口

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master #分支名称
          name: config  #配置文件名称
          profile: dev  #后缀名称
          uri: http://localhost:3344 #配置中心服务端地址
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    #暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    

    3.2.3 业务类Controller添加注解@RefreshScope,让每次调用之前都重新加载

    @RestController
    @RefreshScope   //刷新获得的总体配置文件
    public class ConfigClientController {
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo(){
            return configInfo;
        }
    }
    

    3.2.4 测试
      此时再次修改 GitHub上配置文件的信息,然后使用服务端自测,然后调用客户端测试,发现客户端获得的信息并没有更新,如果要获得正确的信息,还是需要重启。哈哈,别急,这是因为这个动态加载需要运维人员发送Post请求刷新3355才能生效。
    再次修改GitHub上的全局配置文件,然后先用 cmd 窗口发送,POST 请求刷新:

    curl -X POST "http://localhost:3355/actuator/refresh"
    

      然后再次进行上面的测试,你会发现现在终于实现了不用重启客户端就可以动态加载配置文件,但是遗憾的是,每次更新配置文件后都需要发送一个请求刷新一下客户端比较繁琐,于是便想用一个方法如广播,一次通知,处处生效,大范围的自动刷新。于是就有了 服务总线 Bus.,详情请看下篇博文

  • 相关阅读:
    Distribution of Data Through OCAF Tree
    OpenCASCADE Application Framework Data Framework Services
    OpenCascade Application Framework Introduction
    被限制的加法(高精入门)
    凝视
    第二题
    图论3 二分图匹配
    图论2 最近公共祖先LCA
    图论1 Tarjan算法
    最小生成树
  • 原文地址:https://www.cnblogs.com/firecode7/p/16120425.html
Copyright © 2020-2023  润新知