Config分布式配置中心

分布式配置中心概述

1、分布式系统面临的配置问题?

微服务意味着要将单体应用中心的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设置是必不可少的。SpringCloud 提供了 ConfigServer来解决这个问题。

2、SpringCloud Config原理图

SpringCloud Config为微服务架构中的微服务提供集中式的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
在这里插入图片描述

3、SpringCloud Config 分为服务端和客户端

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

4、作用

(1)集中管理配置文件。
(2)不同环境不同配置,动态话的配置更新,分环境部署比如:dev、test、prod、beta、release。
(3)运行期间动态调整配置,不在需要在每个服务部署的机制上编写配置文件,服务会向配置中心统一拉取配置自己的信息,当配置发生变动时,服务不在需要重启即可感应到配置的变化并应用新的配置。(类似观察者模式)
(4)将配置信息以 REST 接口的形式暴露。

Config服务端配置与测试

一、在github上建立仓库(本文以idea上构建文件为例)
在这里插入图片描述
二、构建cloud-config-center-server3344 模块
1、引入 pom 依赖

        <!--SpringCloud config配置中心服务端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--Eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--导入公共实体或者API依赖-->
        <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>

2、写 yml 配置文件

# 服务端口号
server:
  port: 3344

# 服务名称
spring:
  application:
    name: cloud-config-center-server
  # 配置中心
  cloud:
    config:
      # 读取分支
      label: master
      server:
        git:
          #填写自己的github路径
          uri: https://github.com/1914526816lhw/springcloud-config-repository.git
          #搜索目录
          search-paths:
            - springcloud-config-repository
          # 连接 github 的账户(登录的账号密码)
          username: 账号
          password: 密码
          # 设置连接 github 超时时间
          timeout: 60
          # 强制拉取
          force-pull: true


# 服务注册中心
eureka:
  client:
    #表示收将自己注册到EurekaServer,默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka  #单机版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版
  instance:
    instance-id: config-center-server3344
    #访问路径可以显示IP地址
    prefer-ip-address: true
    #eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认为30秒)
    lease-renewal-interval-in-seconds: 1
    #eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认为90秒),超时将删除服务
    lease-expiration-duration-in-seconds: 2

3、主启动类

@SpringBootApplication
//激活配置中心
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}

4、测试
浏览器访问:http://config3344.com:3344/master/config-dev.yml 能够正常获取到远程仓库config-dev.yml配置文件内容。
在这里插入图片描述
三、Config配置读取规则

label:分支(branch)
application:服务名
profile:环境(dev、test、prod)

1、/{label}/{application}-{profile}.yml(或 .properties)推荐使用这种方式】,如图:
在这里插入图片描述
2、/{application}-{profile}.yml(或 .properties)在配置文件中没有配置label的情况下。默认读取master分支,master 分支没有的时候再去找其他分支,如果不存在,则返回空。
在这里插入图片描述
3、/{application}/{profile}/[{label}],读取配置文件信息返回的是 json格式,可自己进行解析。
在这里插入图片描述

Config客户端配置与测试

1、构建 cloud-config-center-client3355 模块
2、引入 pom 依赖

        <!--SpringCloud config配置中心客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--Eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--导入公共实体或者API依赖-->
        <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>

3、写 yml 文件配置(此处为:bootstrap.yml

bootstrap.yml概念

(1)application.yml 是用户级的资源配置项。
(2)bootstrap.yml 是系统级的,优先级更高

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

bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: cloud-config-center-client
  cloud:
    # Config 客户端配置
    config:
      discovery:
        #开启服务发现
        enabled: true
        # 服务id
        service-id: cloud-config-center-server
      label: master #分支名称
      name: config  #配置文件名
      profile: dev  #配置文件后缀名称
      # uri: http://localhost:3344 #配置中心服务端地址
      username: 账号
      password: 密码


# 服务注册中心
eureka:
  client:
    #表示收将自己注册到EurekaServer,默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka  #单机版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版
  instance:
    instance-id: config-center-client3355
    #访问路径可以显示IP地址
    prefer-ip-address: true
    #eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认为30秒)
    lease-renewal-interval-in-seconds: 1
    #eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认为90秒),超时将删除服务
    lease-expiration-duration-in-seconds: 2

4、controller

@RestController
public class ConfigClientController {

    @Value(value = "${config.info}")
    private String configInfo;

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

5、测试:浏览器访问 http://localhost:3355/getConfigInfo 得到与http://config3344.com:3344/master/config-dev.yml内容,即:远程配置文件中的值,如下:

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

在这里插入图片描述

问题: 当我们修改远程仓库配置文件内容后,配置中心服务端通过刷新可以获取心的配置信息,但是客户端刷新后没有任何反应,还是原来的配置信息。除了重启。那么如何解决实时更新配置问题?接着往下 Config客户端动态刷新

Config客户端动态刷新

一、修改 cloud-config-center-client3355 模块

1、引入 pom 的 actuator 监控依赖

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

2、修改 yml 文件,暴露监控端口

management:
  endpoints:
    web:
      exposure:
        include: "*"

3、controller业务类中加入@RefreshScope注解

@RefreshScope
@RestController
public class ConfigClientController {

    @Value(value = "${config.info}")
    private String configInfo;

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

4、测试

理论上内容同步流程:github --> 3344 --> 3355,实际上情况如下:

(1)访问http://config3344.com:3344/master/config-dev.yml能够正常访问到配置文件内容。
(2)直接刷新 http://localhost:3355/getConfigInfo没有任何反应,获取的是原来的值。需要通过POST请求刷新3355:curl -X POST "http://localhost:3355/actuator/refresh",再次访问时,更新了最新的配置内容。虽然避免了重启客户端服务。但是每个服务都需要POST请求刷新,还是很不理想。该如何处理呢?接着往下看。

二、广播(观察者模式)
SpringCloud Bus 消息总线:https://blog.csdn.net/qq_36763419/article/details/120851600

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌守

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值