Config 分布式配置中心

概述

微服务意味着要将单体应用中的业务拆分成个个子服务,每个服务的粒度相对较小因此系统中会出现大量的服务
由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的
Spring Cloud提供了 ConfigServer 来解决这个问题,我们每个微服务自己带着一个 application.yml,上百个配置文件的管理会导致膨胀
官方地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/

Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置攴持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的配置
Spring Cloud Config分为服务端客户端两部分

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

主要分支

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

服务端配置

首先要在 Github 上创建一个 Config 仓库,来配置环境

  1. 新建一个module
  2. 修改 pom 依赖
<!-- config-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 配置 yml
server:
  port: 3344

spring:
  application:
    name: Config-center
  cloud:
    config:
      server:
        git:
          # Github 上仓库的名字
          uri: https://github.com/odousnag/SpringCloudStudy.git
          # 搜索目录
          search-paths:
            - springcloud-config
      # 读取分支
      label: master

# Eureka
eureka:
  client:
    service-url:
      # 单机版
      # defaultZone: http://localhost:7001/eureka/
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  1. 主启动类开启注解
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain.class,args);
    }
}
  1. windows 下更改 hosts 增加映射
  2. 测试是否能从 Github 上获取配置内容

    启动配置中心:http://config3344.com:3344/master/config-dev.yml

    7.配置读写规则
    官网上的配置读写规则
常用的三种:
/{label}/{application}-{profile}.properties
  master 分支:http://config3344.com:3344/master/config-xxx.yml
  dev 分支:http://config3344.com:3344/dev/config-xxx.yml

/{application}/{profile}[/{label}]
  http://config3344.com:3344/config-xxx.yml

/{application}-{profile}.yml
  http://config3344.com:3344/config/xxx/master

客户端配置

  1. 新建一个module
  2. 修改 pom 依赖
<!-- spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. 新建一个 bootstrap.yml
    applicaiton.yml 是用户级的资源配置项,bootstrap.yml是系统级的,优先级更加高
    Spring Cloud会创建一个 "Bootstrap Context",作为 Spring应用的 Application Context的父上下文
    初始化的时候, Bootstrap Context 负责从外部源加载配置属性并解析配置,这两个上下文共享—个从外部获取的 Environment
    将 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
      # 配置中心地址
      uri: http://localhost:3344

# Eureka
eureka:
  client:
    service-url:
      # 单机版
      # defaultZone: http://localhost:7001/eureka/
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  1. 修改 application-dev.yml 配置并提交到 Github 中,比如加个变量 age 或者版本号 version
  2. 业务类测试
@RestController
public class ClientController {

    /**
     * 获取application-dev的信息
     */
    @Value("${config.info}")
    private String configInfo;

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


实现了客户端访问SpringCloudConfig通过GitHub获取配置信息

客户端动态刷新

动态刷新问题

Linux运维修改GitHub上的配置文件内容做调整,刷新服务端,发现ConfigServer配置中心立刻响应,刷新客户端,发现ConfigClient客户端没有任何响应
修改客户端

  1. 引入 actuator 监控依赖
<!-- spring-boot-starter-actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 修改 yml 配置,暴露监控端口
# 暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. 业务类修改,增加注解 @RefreshScope
  2. 发送Post请求刷新 客户端
curl -X POST "http://localhost:3355/actuator/refresh"

出现的问题:
现在每次更改,都要手动发动请求,这不合适,实现广播,一次通知,处处修改
下一章到消息总线会讲到

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/hewenhao-blogs/p/14728446.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程