Config配置中心

Posted by Beyonderwei on 2020-12-21
Words 1.1k and Reading Time 4 Minutes
Viewed Times

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

一、简介

1.配置中心介绍

问题:微服务将单体应用拆分成一个个的子服务,导致服务数量多,由于每个服务都有一套自己的配置信息,为了对服务的相同配置(如数据库、服务注册等配置)进行统一的管理,因此需要一套集中的、动态配置中心。

SpringCloud Config:分布式配置中心为一个独立的微服务,配置服务为各个不同的微服务应用的所有环境提供了一个中心好的外部配置。用来连接服务器并为其他微服务提供配置信息,加密、解密、信息访问等接口。微服务在启动时,从配置中心获取和加载服务的配置信息,默认通过git来存储配置信息,这样有利于对配置环境进行版本管理,可以通过git客户端工具来管理和访问。

2. SpringCloud Config

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release(开发、测试、生产、发布、灰度等)
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露
  • SpringCloud Config默认使用Git来存储配置文件,使用的是http/https访问的形式。

二、创建配置中心(服务端)

1. 创建github仓库

​ 用于配置的版本管理的远程仓库。包含以下三个配置文件。

2. 工程创建

  • pom依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  • yaml配置

    注:这里必须使用SSH的地址,因为github是通过此方式在PC上授权的。

    ​ 2020年10月后,github的主分支变为了main。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    server:
    port: 端口
    spring:
    application:
    name: config-center
    cloud:
    config:
    server:
    git:
    uri: github项目路径(git@github.com:Beyonderwei/SpringCloudConfig.git)
    search-paths:
    - SpringCloudConfig
    label: main
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:端口/eureka
  • 主启动类添加注解 @EnableConfigServer

  • 访问:http://localhost:端口/main/config-dev.yml 即可访问到配置文件的内容

3. 常用规则(官网)

  • /{label}/{application}-{profile}.yml 采用了上图第三个。
    • label:分支
    • application:服务名(如XXX服务的名字)
    • profile:环境(dev、test、prod)

三、创建配置中心(客户端)

Config客户端即微服务端。

  • pom依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  • bootstrap配置文件

    系统级的配置文件,优先级更高,比application先加载,SpringCloud会创建一个 Bootstrap Context,作为Spring应用的Applcation Context 的父上下文,初始化时,Bootstrap Context从外部源加载胚子属性并解析配置。这两个上下文共享一个从未外部获取的 Environment。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    server:
    port: 端口

    spring:
    application:
    name: config-client
    cloud:
    config:
    label: main
    name: config
    profile: dev
    uri: http://localhost:端口
    eureka:
    client:
    service-url:
    defaultZone: http://ip:端口/eureka
  • 启动类 无需加任何特殊注解

  • 测试从配置中心获取的配置信息。通过@Value 读取配置文件。

    注:要求github配置文件内容有

    1
    2
    config: 
    info: xxxxx内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @RestController
    public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

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

四、客户端动态刷新

​ 为了解决配置中心的配置更改以后(配置中心从github加载配置,实际上是github上的配置信息修改以后),各个客户端(微服务)需要重启才能从配置中心加载最新的配置。

  • pom依赖引入 actuator 监控

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  • bootstrap.yaml 配置暴露监控的端口

    1
    2
    3
    4
    5
    management:
    endpoints:
    web:
    exposure:
    include: "*"
  • controller 业务类 添加注解 @RefreshScope

  • 此时修改github后,在命令行访问如下地址,或postman发送请求,不用重启微服务即可加载最新配置。

    1
    curl -X POST "http://localhost:微服务端口/actuator/refresh"
  • 如果想实现自动的完场上述过程,避免手动发送POST请求加载新配置。那么需要通过消息总线来解决。


本文为作者原创文章,未经作者允许不得转载。

...

...

00:00
00:00