SpringCloud config 分布式配置中心

博客参考学习视频: https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=4388336378730572330

上一篇SpringCloud 服务网关:https://blog.csdn.net/qq_45738810/article/details/109147121

① 概述

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

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

​ SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己的带着一个application.yml, 上百个配置文件的管理…o(╥﹏╥)o

2.是什么

image-20201018232001880

  • 是什么
    SpringCloud Config 为微服务架构中微服务提供了集中化的外部配置支持,配置服务器为各个不同微服务应用的的所有环境提供了一个中心化的外部配置。

  • 如何使用

    Spring Cloud 分为服务端和客户端两部分

    服务端也称为分布式配置中心,它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供获取配置信息,加密/ 解密信息等访问接口。

    客户端则是通过制定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取好加载配置信息配置服务器默认采用 git 来存储配置信息,这样就有助于缓解配置惊醒版本管理,并且可以通过 git 客户端工具来方便管理和访问配置内容。

3.能干嘛

  • 集中管理配置文件
  • 不同的环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不在需要在每个服务部署的机器上编写配置文件,服务回向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并用新的配置
  • 将配置信息以 REST 接口的形式暴露, post、curl 访问刷新均可…

4.与 Github 整合配置

由于SpringCloud Config默认使用 Git 来存储配置文件(也有其他方式,比如支持svn 和 本地文件,但是推荐的还是git ,而且使用的是 http/https 访问的形式)

5.官网

https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

image-20201018232902083

② Config 服务端配置与测试

1.用 你 自 己 的 账 号 在 Github 上 新 建 一 个 名 为sprincloud-config 的新 Repository

2.由上一步获得刚新建的 git 地址写你自己的仓库地址

3.本地硬盘上新建 git 仓库并 clone

image-20201018233746596

  • 本地地址: D:\44\SpringCloud2020
  • git 命令: git clone xxx

4.此 时 在 本 地 D 盘 符 下 D:\44\SpringCloud2020\springcloud-config

image-20201018233830424

表示多个环境的配置文件,保存格式必须为 UTF-8,如果需要修改, 此处模拟运维人员操作 git 和 g 。

  1. git add
  2. git commit -m "init yml"
  3. git push origin master

5.新建 Module 模块 cloud-config-center-3344 它既为Cloud 的配置中心模块 cloudConfig Center

image-20201019165411961

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clould</artifactId>
        <groupId>com.oy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>

    <dependencies>
        <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>
        <dependency>
            <groupId>com.oy</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</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>
    </dependencies>
</project>

YML

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/OY6090/sprincloud-config.git
          #GitHub上面的git仓库名字
          ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: main
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

主启动类

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

Windows 下修改 hosts 文件, 增加映射

路径:C:\WINDOWS\System32\drivers\etc

image-20201019170450212

测试通过 Config 微服务是否可以从 Github 上获取配置内容

  1. 启动微服务 3344
  2. http://config-3344.com:3344/main/config-dev.yml

image-20201019171232495

6.读取配置规则

  1. 官网

image-20201019203901660

  1. /{label}/{application}-{profile}.yml(最推荐使用这种方式)

main(maste) 分支

  1. http://config-3344.com:3344/main/config-dev.yml
  2. http://config-3344.com:3344/main/config-test.yml
  3. http://config-3344.com:3344/main/config-prod.yml

dev 分支

  1. http://config-3344.com:3344/dev/config-dev.yml
  2. http://config-3344.com:3344/dev/config-test.yml
  3. http://config-3344.com:3344/dev/config-prod.yml
  1. /{application}-{profile}.yml
  1. http://config-3344.com:3344/config-dev.yml
  2. http://config-3344.com:3344/config-test.yml
  3. http://config-3344.com:3344/config-prod.yml
  4. http://config-3344.com:3344/config-xxxx.yml(不存在的配置)
  1. /{application}-{profile}[/{label}]
  1. http://config-3344.com:3344/config/dev/main
  2. http://config-3344.com:3344/config/test/main
  3. http://config-3344.com:3344/config/prod/main
  1. 重要配置细节总结
  • /{name}-{profiles}.yml

  • /{label}-{name}-{profiles}.yml

  • label:分支(branch)

  • name:服务名

  • profiles:环境(dev/test/prod)

成功实现了用 SpringCloud Config 通过 GitHub 获取配置信息

③ Config 客户端配置与测试

1. 新建 cloud-config-client-3355

2.POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clould</artifactId>
        <groupId>com.oy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>

    <dependencies>
        <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>com.oy</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</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>
    </dependencies>
</project>

3.bootstrap.yml

image-20201019232544875

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

说明

image-20201019232917919

4.修改 config-dev.yml 配置并提交到 GitHub 中, 比如加个变量 age 或者版本号 version

5.主启动

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

6.业务类

@RestController
public class ConfigClientController {
    
    @Value("${config.info}")
    private String configInfo;
    
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

7.测试

  • 启动 Config 配置中心 3344 微服务并自测
    • http://config-3344.com:3344/main/config-dev.yml
    • http://config-3344.com:3344/main/config-test.yml

image-20201019234730097

  • 启动 3355 作为 Client 准备访问
    • http://localhost:3355/configInfo

image-20201019234802075

8.成功实现了客户端 3355 访问 SpringCloud Config3344通过 GitHub 获取配置信息

9.问题随之而来, 分布式配置的动态刷新

  1. Linux 运维修改 GitHub 上的配置文件内容做调整
  2. 刷新 3344, 发现 ConfigServer 配置中心立刻响应

image-20201019235104513

  1. 刷新 3355, 发现 ConfigServer 客户端没有任何响应

    image-20201019235127413

  2. 3355 没有变化除非自己重启或者重新加载

  3. 难道每次运维修改配置文件, 客户端都需要重启? ? 噩梦

④ Config 客户端之动态刷新

1.避免每次更新配置都要重启

2.动态刷新

修改 3355 模块, POM文件引入 actuator 监控

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

修改YML, 暴露监控端口

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

@RefreshScope 业务类 Controller 修改

@RestController
@RefreshScope
public class ConfigClientController {

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

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

此时修改 github—> 3344 —> 3355

image-20201020150340427

  1. http://localhost:3355/configInfo

image-20201020150354347

  1. 3355 改变了没有? ? ? 没有改变, (┬_┬)
  2. 需要运维人员发送 Post 请求刷新 3355
  3. 必 须 是 Post 请 求 : curl -X POST “http://localhost:3355/actuator/refresh”

image-20201020150556178

  1. 再次: http://localhost:3355/configInfo ,成功实现了客户端 3355 刷新到最新配置内容,避免了服务的重启

image-20201020150639862

3.想想还有什么问题?

​ 假如有多个微服务客户端 3355/3366/3377。。。。每个微服务都要执行一次 post 请求, 手动刷新?可否广播, 一次通知, 处处生效?我们想大范围的自动刷新, 求方法-----------结合消息总线 Bus 解决以上问题 。

​ 具体请参考下篇博客:SpringCloud Bus 消息总线:https://blog.csdn.net/qq_45738810/article/details/109190898

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值