Spring Cloud(九)Config配置中心

逃离我推掉我的手 2022-05-15 15:00 272阅读 0赞

1 概述

架构图

1.1 产生背景

分布式系统中,会将服务拆分成一个个独立的服务,这些服务都要通过配置文件配置相应信息才能运行,随着系统内微服务数量的增多,配置文件也会不断的增多,大量的配置文件的管理成为一个繁琐的问题。因此一套集中式的、动态的配置管理设施是必不可少的。

1.2 Spring Cloud Config配置中心

  • SpringCloud Config是一个提供外部集中式配置管理的组件,配置服务器为各种不同的微服务提供了一个中心化的外部配置中心。
  • SpringCloud Config分为服务端和客户端两部分:

    • 服务端:分布式配置中心,是一个独立的微服务,用来连接并为客户端提供配置、加密/解密信息等访问入口。
    • 客户端:通过指定的配置中心获取配置资源,推荐用git来存储配置文件。

1.3 解决的问题

  • 集中管理配置文件。
  • 不同的环境对应不同的配置,动态化的更新配置。
  • 不需要在每个服务部署的机器上编写配置文件,让服务中心统一为服务拉取配置文件。
  • 将配置信息以REST接口形式暴露。

2 案例

2.1 服务端

  1. 新建github远程仓库,如:spring-cloud-config。
  2. 将新建的远程仓库克隆到本地,新建date-dev.yml配置文件,将该文件上传至远程仓库。

    date:
    description: today is 2018-10-31

    保存为UTF-8格式

  3. 新建config server服务模块,加入依赖。



    org.springframework.cloud
    spring-cloud-config-server

  4. appliacation.yml配置文件。

    server:
    port: 2233
    spring:
    application:

    1. name: spring-cloud-config-server

    cloud:

    1. config:
    2. server:
    3. git:
    4. uri: https://github.com/chenyulin19930815/spring-cloud-config.git #GitHub上面的git仓库名字
    5. username: xxx #git仓库用户名
    6. password: xxxx #github仓库密码
  5. 编写主启动类,加入@EnableConfigServer注解。

    @SpringBootApplication
    @EnableConfigServer
    public class SpringCloudConfigServerApplication2233 {

    1. public static void main(String[] args) {
    2. SpringApplication.run(SpringCloudConfigServerApplication2233.class, args);
    3. }

    }

  6. 启动服务,访问配置文件。

有以下访问规则:

  • {application}:配置文件的文件名
  • {profile}:读取的环境
  • {lable}:分支
  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{label}/{application}-{profile}.properties
  1. 本示例有以下访问方式。

    • http://localhost:2233/date-dev.yml
      在这里插入图片描述
    • http://localhost:2233/master/date-dev.yml
      在这里插入图片描述
    • http://localhost:2233/date-dev.yml/master
      在这里插入图片描述

2.2 客户端

  1. 在本地仓库新建spring-cloud-config-client.yml上传至远程仓库。

    spring:
    profiles:

    1. active: dev

    server:
    port: 2244
    spring:
    profiles: dev
    application:

    1. name: spring-cloud-config-client

    server:
    port: 2245
    spring:
    profiles: test
    application:

    1. name: spring-cloud-config-client
  2. 新建config client服务模块,导入依赖。



    org.springframework.cloud
    spring-cloud-starter-config


    org.springframework.boot
    spring-boot-starter-web

  3. 新建bootstrap.yml1配置文件。

    spring:
    cloud:

    1. config:
    2. name: spring-cloud-config-client #从git仓库读取的配置文件资源名称,没有后缀
    3. profile: dev
    4. label: master
    5. uri: http://localhost:2233 #本微服务启动后先去找配置中心服务端,以获取GitHub的服务地址
  4. 新建application.yml

    spring:
    application:

    1. name: spring-cloud-config-client
  5. 启动类。

    @SpringBootApplication
    public class SpringCloudConfigClientApplication2244 {

    1. public static void main(String[] args) {
    2. SpringApplication.run(SpringCloudConfigClientApplication2244.class, args);
    3. }

    }

  6. web测试controller。

    @RestController
    public class ConfigController {

    1. @Value("${spring.application.name}")
    2. private String applicationName;
    3. @Value("${spring.profiles}")
    4. private String springProfiles;
    5. @Value("${server.port}")
    6. private String port;
    7. @RequestMapping("/config")
    8. public String getConfig() {
    9. return "applicationName: " + applicationName +"<br/>springProfiles:"+springProfiles+"<br/>port: " + port;
    10. }

    }

  7. 测试:依次启动config server,config client服务。
    首先,dev启动的端口为2244,访问:http://localhost:2244/config
    在这里插入图片描述
    然后,更改bootstrap.yml中profile: dev为profile: test,端口切换为了2245,访问:http://localhost:2245/config
    在这里插入图片描述

遗留问题
将spring-cloud-config-client.yml文件中test的spring.application.name=spring-cloud-config-client改为spring.application.name=spring-cloud-config-client-test,再次访问发现名字没有更换,说明修改github配置信息后,系统不会主动去获取。

看源码点这里


  1. 一,bootstrap.yml比application.yml具有更高的优先级。
    二,bootstrap.yml是系统级的资源配置项,application.yml是用户级的资源配置项。
    三,SpringCloud会创建”BootStrap Context”作为”ApplicationContext”的父上下文。初始化的时候BootStrap Context负责从外部源加载配置属性并解析。这两个上下文共享一个”Environment”,BootStrap 具有更高优先级,他们不会被本地配置覆盖。 ↩︎

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Cloud Config远程配置

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud