天天看点

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

SpringCloud Config分布式配置中心

  • 分布式配置中心概述
  • Config服务端配置与测试
  • Config客户端配置与测试
  • Config客户端动态刷新

分布式配置中心概述

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

微服务意味着要将单体应用中心的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。

由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设置是必不可少的。

SpringCloud 提供了 ConfigServer来解决这个问题。

2、SpringCloud Config原理图

SpringCloud Config

为微服务架构中的微服务

提供集中式的外部配置支持

,配置服务器为各个不同微服务应用的所有环境提供了

一个中心化的外部配置。

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

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

(1)

服务端也成为分布式配置中心

,它是一个

独立的微服务应用

,用来连接配置服务器并

为客户端提供获取配置信息,加密/解密信息的访问接口。

(2)

客户端

则是通过指定的配置中心来管理应用资源,以及与业务相业务相关的配置内容,并

在启动的时候从配置中心获取和加载配置信息

。配置服务器默认采用 git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。

4、作用

(1)集中管理配置文件。

(2)不同环境不同配置,动态话的配置更新,分环境部署比如:dev、test、prod、beta、release。

(3)运行期间动态调整配置,不在需要在每个服务部署的机制上编写配置文件,服务会向配置中心统一拉取配置自己的信息,

当配置发生变动时,服务不在需要重启即可感应到配置的变化并应用新的配置。(类似观察者模式)

(4)将配置信息以

REST 接口

的形式暴露。

Config服务端配置与测试

一、在

github

上建立仓库(本文以idea上构建文件为例)

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

二、构建

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配置文件内容。

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

三、Config配置读取规则

label

:分支(branch)

application

:服务名

profile

:环境(dev、test、prod)

1、

/{label}/{application}-{profile}.yml(或 .properties)

【推荐使用这种方式】,如图:

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

2、

/{application}-{profile}.yml(或 .properties)

在配置文件中没有配置

label

的情况下。默认读取

master

分支,master 分支没有的时候再去找其他分支,如果不存在,则返回空。

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

3、

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

,读取配置文件信息返回的是

json格式

,可自己进行解析。

SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

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 #配置中心服务端地址
      uri: lb://cloud-config-center-server
      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"
           
SpringCloud Config分布式配置中心分布式配置中心概述Config服务端配置与测试Config客户端配置与测试Config客户端动态刷新

问题:

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

待更新