SpringCloud笔记——Config分布式配置中心

概述


分布式系统面临的问题

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

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

SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理…你就等着哭去吧

是什么?

在这里插入图片描述

Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持

配置服务器为各个不同微服务应用的所有应用环境提供了一个中心化的外部配置

怎么玩呢?

SpringCloud Config分为服务端和客户端两部分

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

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

能干嘛?

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

与GitHUb整合配置

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

Config服务端配置与测试


新建Repository

用自己的账号在GitHub上新建一个名为SpringCloud-config的新Repository

在这里插入图片描述

由上一步获得你新建的Repository地址 https://github.com/1098301679/springcloud-config

新建模块并配置

新建mould模块cloud-config-center3344

修改Pom.xml引入依赖

    <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</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>
    </dependencies>

新建mould

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri:  https://github.com/1098301679/springcloud-config  #填写你自己的github路径
          search-paths:
            - /**						  #搜寻路径
      label: main				   #读取分支   注意2020年10月后就不是master了
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

主启动类ConfigCenterMain3344

@EnableConfigServer   //添加 @EnableConfigServer 使其具备 Config Server 功能
@SpringBootApplication
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class,args);
    }
}

测试

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

启动微服务3344 -> http://localhost:3344/main/config-dev.yml

config:				#网页上返回的内容!
  info: master branch,springcloud-config/config-dev.yml version=1

配置读取规则

官网说明:5种

在这里插入图片描述

常用的规则

  • /{label}/{application}-{profile}.yml (最推荐,例子如下)

    main分支dev分支
    http://localhost:3344/main/config-dev.ymlhttp://localhost:3344/dev/config-dev.yml
    http://localhost:3344/main/config-test.ymlhttp://localhost:3344/dev/config-test.yml
    http://localhost:3344/main/config-prod.ymlhttp://localhost:3344/dev/config-prod.yml
  • /{application}-{profile}.yml

    http://localhost:3344/config-dev.yml
    http://localhost:3344/config-test.yml
    http://localhost:3344/config-prod.yml
  • /{application}-{profile}[/{label}]

    http://localhost:3344/config-dev.yml/main
    http://localhost:3344/config-test.yml/main
    http://localhost:3344/config-prod.yml/main

重要配置细节总结

/{label}/{application}-{profile}.yml

label:分支(branch)

application:服务名

profile:环境(dev/test/prod)

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

Config客户端配置与测试


新建模块并配置

新建cloud-config-client3355

修改POM.xml

<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</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>
    </dependencies>

bootstrap.yml

applicaiton. ym1是用户级的资源配置项,bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个“BootstrapContext”,作为Spring应用的Application Context 的父上下文。初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment

Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap contextApplication Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap ContextApplication Context配置的分离

要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的

因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #config客户端配置
    config:
      label: main   #分支名称
      name: config  #配置文件名称
      profile: dev  #读取后缀名称
                    #上述三个综合:main分支上config-dev.yml的配置文件被读取     http://localhost:3344/main/config-dev.yml
      uri: http://localhost:3344  #配置文件中心地址

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

主启动类

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

业务类

@RestController
public class ConfigClientController {
	//你在配置文件中写的内容 
    @Value("${config.info}")
    private String configInfo;

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

测试

启动Config配置中心3344微服务并自测通过 http://localhost:3344/main/config-dev.yml

config:				#网页上返回的内容!
  info: master branch,springcloud-config/config-dev.yml version=1

启动3355微服务作为Client准备访问 http://localhost:3355/configInfo

master branch,springcloud-config/config-dev.yml version=1

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

问题:分布式配置动态刷新问题

Linux运维工程师修改GitHub上配置文件的内容

刷新3344,发现ConfigServer配置中心立即响应

刷新3355,发现ConfigClient客户端没有任何响应

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

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

Config客户端之动态刷新


避免每次更新配置都要重新启动客户端微服务3355

动态刷新

修改3355模块的YML配置,暴露actuator监控端口

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

@RefreshScope业务类Controller修改

@RefreshScope
@RestController
public class ConfigClientController {

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

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

此时修改GitHub上的配置文件将config-dev.yml最后的version由1改为2

再次测试3344与3355

http://localhost:3344/main/config-dev.yml 成功改为2

http://localhost:3355/configInfo 还是1,还得需要重启,失败!

为什么会失败呢?

需要运维人员发送一个Post请求刷新3355

curl -X POST “http://localhost:3355/actuator/refresh”

C:\Users\10983>curl -X POST "http://localhost:3355/actuator/refresh"
["config.client.version","config.info"]

再次访问http://localhost:3355/configInfo会发现修改成功!

成功实现了客户端3355刷新到最新配置文件内容,避免了客户端的重启

想想还有什么问题?

  • 假如有多个微服务客户端3355/3366/3377。。。
  • 每个微服务都要执行一次post请求,手动刷新?
  • 可否广播,一次通知,处处生效?
  • 我们想大范围的自动刷新,求方法
  • 100台只通知98台,该通知的通知,不该通知的不通知?

答案:SpringCloud Bus消息总线!

curl -X POST “http://localhost:3355/actuator/refresh”

C:\Users\10983>curl -X POST "http://localhost:3355/actuator/refresh"
["config.client.version","config.info"]

再次访问http://localhost:3355/configInfo会发现修改成功!

成功实现了客户端3355刷新到最新配置文件内容,避免了客户端的重启

想想还有什么问题?

  • 假如有多个微服务客户端3355/3366/3377。。。
  • 每个微服务都要执行一次post请求,手动刷新?
  • 可否广播,一次通知,处处生效?
  • 我们想大范围的自动刷新,求方法
  • 100台只通知98台,该通知的通知,不该通知的不通知?

答案:SpringCloud Bus消息总线!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大恐龙的小弟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值