• SpringCloud-Config 分布式配置中心


    概述

    分布式系统面临的问题

    微服务意味着要将单体应用中的业务拆分成一个个的子服务,这些服务都需要必要的配置信息才能运行,如果有上百个微服务,上百个配置文件,管理起来是非常困难的,这时候,一套集中式的、动态的配置管理中心是必不可少的,Spring Cloud 提供了 ConfigServer 来解决这个问题。

    是什么?

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

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

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

    能干嘛?

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

    与 Github 整合配置

    Spring Cloud Config 默认使用 Git 来存储配置文件(也有其他方式,比如SVN、本地文件,但最推荐的还是 Git),而且使用的是 http/https 访问的形式

    基本使用

    服务端准备

    1、使用 GitHub 或其它代码库创建一个仓库 springcloud-config,添加几个文件,创建一个 dev 分支

    2、新建一个项目当作配置中心,添加 maven 依赖

    <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>
    

    3、在application.yml添加如下配置,配置自己的远程仓库地址,如果 ssh 无法连接可以尝试使用 https

    server:
      port: 3344
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              # 远程库地址
              uri: @*&%$%#$%
              # 搜索目录
              search-paths:
                - springcloud-config
          # 读取分支
          label: master
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    

    4、在主启动类上开启配置服务

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

    5、在浏览器输入如下地址可以访问到配置文件的信息

    官网上介绍了如下几种访问方式:

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties
    

    其中第一种方式返回的是 json 数据(如下图所示),其它方式返回的都是文件真正的内容

    客户端准备

    我们使用 bootstrap.yml 最为配置文件

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

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

    Spring Cloud 会创建一个 Bootstrap Context,作为 Spring 应用的 Application Context 的父上下文。初始化的时候,Bootstrap Context 负责从外部源加载配置属性,并解析配置。这两个上下文共享一个从外部获取的 Environment。

    Bootstrap 属性有高优先级,默认情况下,它们不会被本地配置覆盖,Bootstrap Context 和 Application Context 有着不同的约定,所以新加一个 bootstrap.yml 文件,保证 Bootstrap Context 和 Application Context 配置的分离

    1、添加 Maven 依赖

    <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>
    

    2、添加配置文件 bootstrap.yml

    server:
      port: 3355
    spring:
      application:
        name: cloud-config-client
      cloud:
        config:
          label: master #分支名
          name: config #配置文件名
          profile: test #配置文件后缀
          uri: http://config3344.com:3344
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    

    3、编写 controller,获取配置中心中的文件属性

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

    4、浏览器输入地址访问

    如果需要获取其它配置文件内容,只需要修改 bootstrap.yml 中的 labelnameprofile 即可

    存在的问题?

    当配置中心的配置文件内容发生改动,服务端和客户端是否能够动态的获取?

    经测试,服务端可以动态的获取,客户端不能!

    因为服务端直接从配置中心获取,而客户端是从上下文环境中获取已加载的属性,配置中心修改后,由于服务没有重启,获取的仍然是之前的属性。

    Config 动态刷新

    对客户端进行修改

    1、需要引入 actuator 依赖

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

    2、添加如下配置

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

    3、在 Controller 上添加注解 @RefreshScope

    4、刷新服务端后,发送 Post 请求,curl -X POST http://localhost:3355/actuator/refresh,客户端刷新即可获取最新内容,避免了服务重启

    仍然存在的问题?

    • 每个微服务都需要发送一次 POST 请求。
    • 如何广播通知?一次通知,处处生效
    • 如何进行差异化的处理,让部分服务动态获取

    下一篇为大家介绍:Spring Cloud Bus 消息总线


    上述代码获取

  • 相关阅读:
    1.3.6 详解build.gradle文件——Android第一行代码(第二版)笔记
    1.3.5 详解项目中的资源——Android第一行代码(第二版)笔记
    1.3.4分析你的第一个Android程序——Android第一行代码(第二版)笔记
    1.3创建你的第一个Android项目——Android第一行代码(第二版)笔记
    1.2搭建开发环境——Android第一行代码(第二版)笔记
    1.1.3 Android应用开发特色——Android第一行代码(第二版)笔记
    函数与方法的区别
    你真的知道敏捷到底是什么吗?
    某个应用的CPU使用率居然达到100%,我该怎么办?
    异常 lock buffer failed for format 0x23
  • 原文地址:https://www.cnblogs.com/songjilong/p/12779686.html
Copyright © 2020-2023  润新知