Spring Cloud Config:外部集中化配置管理

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实!

阶段1、深入多线程

阶段2、深入多线程设计模式

阶段3、深入juc源码解析


阶段4、深入jdk其余源码解析


阶段5、深入jvm源码解析

码哥源码部分

码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】

码哥讲源码【炸雷啦!炸雷啦!黄光头他终于跑路啦!】

码哥讲源码-【jvm课程前置知识及c/c++调试环境搭建】

​​​​​​码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】

码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】

码哥讲源码【你水不是你的错,但是你胡说八道就是你不对了!】

码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】

终结B站没人能讲清楚红黑树的历史,不服等你来踢馆!

打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】

# Spring Cloud Config:外部集中化配置管理

Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分,本文将对其用法进行详细介绍。

# Spring Cloud Config 简介

Spring Cloud Config 分为服务端和客户端两个部分。服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。客户端可以通过配置中心来获取配置信息,在启动时加载配置。Spring Cloud Config 的配置中心默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。

# 在Git仓库中准备配置信息

由于Spring Cloud Config 需要一个存储配置信息的Git仓库,这里我们先在Git仓库中添加好配置文件再演示其功能,Git仓库地址为:https://gitee.com/macrozheng/springcloud-config

# 配置仓库目录结构

# master分支下的配置信息

  • config-dev.yml:
    config:
      info: "config info for dev(master)"
  • config-test.yml:
    config:
      info: "config info for test(master)"
  • config-prod.yml:
    config:
      info: "config info for prod(master)"

# dev分支下的配置信息

  • config-dev.yml:
    config:
      info: "config info for dev(dev)"
  • config-test.yml:
    config:
      info: "config info for test(dev)"
  • config-prod.yml:
    config:
      info: "config info for prod(dev)"

# 创建config-server模块

这里我们创建一个config-server模块来演示Spring Cloud Config 作为配置中心的功能。

# 在pom.xml中添加相关依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

# 在application.yml中进行配置

    server:
      port: 8901
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git: #配置存储配置信息的Git仓库
              uri: https://gitee.com/macrozheng/springcloud-config.git
              username: macro
              password: 123456
              clone-on-start: true #开启启动时直接从git获取配置
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8001/eureka/

# 在启动类上添加@EnableConfigServer注解来启用配置中心功能

    @EnableConfigServer
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    
    }

# 通过config-server获取配置信息

这里我们通过config-server来演示下如何获取配置信息。

# 获取配置文件信息的访问格式
    # 获取配置信息
    /{label}/{application}-{profile}
    # 获取配置文件信息
    /{label}/{application}-{profile}.yml
# 占位符相关解释
  • application:代表应用名称,默认为配置文件中的spring.application.name,如果配置了spring.cloud.config.name,则为该名称;
  • label:代表分支名称,对应配置文件中的spring.cloud.config.label;
  • profile:代表环境名称,对应配置文件中的spring.cloud.config.profile。
# 获取配置信息演示

# 创建config-client模块

我们创建一个config-client模块来从config-server获取配置。

# 在pom.xml中添加相关依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

# 在bootstrap.yml中进行配置

    server:
      port: 9001
    spring:
      application:
        name: config-client
      cloud:
        config: #Config客户端配置
          profile: dev #启用配置后缀名称
          label: dev #分支名称
          uri: http://localhost:8901 #配置中心地址
          name: config #配置文件名称
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8001/eureka/

# 添加ConfigClientController类用于获取配置

    /**
     * Created by macro on 2019/9/11.
     */
    @RestController
    public class ConfigClientController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo() {
            return configInfo;
        }
    }

# 演示从配置中心获取配置

    config info for dev(dev)

# 获取子目录下的配置

我们不仅可以把每个项目的配置放在不同的Git仓库存储,也可以在一个Git仓库中存储多个项目的配置,此时就会用到在子目录中搜索配置信息的配置。

  • 首先我们需要在config-server中添加相关配置,用于搜索子目录中的配置,这里我们用到了application占位符,表示对于不同的应用,我们从对应应用名称的子目录中搜索配置,比如config子目录中的配置对应config应用;
    spring:
      cloud:
        config:
          server:
            git: 
              search-paths: '{application}'
    config info for config dir dev(dev)

# 刷新配置

当Git仓库中的配置信息更改后,我们可以通过SpringBoot Actuator的refresh端点来刷新客户端配置信息,以下更改都需要在config-client中进行。

  • 在pom.xml中添加Actuator的依赖:
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  • 在bootstrap.yml中开启refresh端点:
    management:
      endpoints:
        web:
          exposure:
            include: 'refresh'
  • 在ConfigClientController类添加@RefreshScope注解用于刷新配置:
    /**
     * Created by macro on 2019/9/11.
     */
    @RestController
    @RefreshScope
    public class ConfigClientController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo() {
            return configInfo;
        }
    }
  • 重新启动config-client后,调用refresh端点进行配置刷新:

    update config info for config dir dev(dev)

# 配置中心添加安全认证

我们可以通过整合SpringSecurity来为配置中心添加安全认证。

# 创建config-security-server模块

  • 在pom.xml中添加相关依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  • 在application.yml中进行配置:
    server:
      port: 8905
    spring:
      application:
        name: config-security-server
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/macrozheng/springcloud-config.git
              username: macro
              password: 123456
              clone-on-start: true #开启启动时直接从git获取配置
      security: #配置用户名和密码
        user:
          name: macro
          password: 123456
  • 启动config-security-server服务。

# 修改config-client的配置

  • 添加bootstrap-security.yml配置文件,主要是配置了配置中心的用户名和密码:
    server:
      port: 9002
    spring:
      application:
        name: config-client
      cloud:
        config:
          profile: dev #启用配置后缀名称
          label: dev #分支名称
          uri: http://localhost:8905 #配置中心地址
          name: config #配置文件名称
          username: macro
          password: 123456
    config info for dev(dev)

# config-sever集群搭建

在微服务架构中,所有服务都从配置中心获取配置,配置中心一旦宕机,会发生很严重的问题,下面我们搭建一个双节点的配置中心集群来解决该问题。

  • 启动两个config-server分别运行在8902和8903端口上;
  • 添加config-client的配置文件bootstrap-cluster.yml,主要是添加了从注册中心获取配置中心地址的配置并去除了配置中心uri的配置:
    spring:
      cloud:
        config:
          profile: dev #启用环境名称
          label: dev #分支名称
          name: config #配置文件名称
          discovery:
            enabled: true
            service-id: config-server
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8001/eureka/
  • 以bootstrap-cluster.yml启动config-client服务,注册中心显示信息如下:

    config info for config dir dev(dev)

# 使用到的模块

    springcloud-learning
    ├── eureka-server -- eureka注册中心
    ├── config-server -- 配置中心服务
    ├── config-security-server -- 带安全认证的配置中心服务
    └── config-client -- 获取配置的客户端服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值