Springcloud-Config

前言

目前国内主流的分布式配置中心使用的时SpringcloudAlibaba的Nacos,这篇文章主要是简单介绍一下Springcloud Config组件的思想和基本使用。

简介

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

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

Spring Cloud Config分为 服务端客户端 两部分。

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

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

202210091426183

作用

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

搭建Config服务端

  • 通过Github搭建远程仓库并创建配置文件

    202210091426184

  • 导入依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  • 在yml文件中添加如下配置

    spring:
      cloud:
        # 配置服务配置中心
        config:
          server:
            git: # 从git远程仓库读取
              uri: https://github.com/Pigman66/springcloud-config.git # 远程仓库地址
              search-paths: # 搜索目录(git仓库名字)
                  - springcloud-config
              skip-ssl-validation: true
          label: master # 读取的分支
          
    #服务注册到EurekaServer
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    
  • 主启动类

    package cn.pigman.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer //启用服务配置中心
    public class ConfigCenterMain3344 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344.class, args);
        }
    }
    

搭建Config客户端

  • 导入依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
  • 新创建一个bootstrap.yml文件,添加如下配置

    server:
      port: 3355
    
    spring:
      application:
        name: cloud-config-client
      cloud:
        config:
          label: master # 分支名称
          name: application # 配置文件名
          profile: dev # 读取后缀名称
          uri: http://config3344.com:3344 # 配置中心地址
          #总结:以上四个配置可以综合为:读取http://config3344.com:3344配置中心的master分支上的application-dev.yml文件
    
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    
  • 主启动类

    package cn.pigman.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class ConfigClientMain3355 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientMain3355.class, args);
        }
    }
    
  • 添加controller类测试

    package cn.pigman.springcloud.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope //开启Controller动态刷新功能
    public class ConfigController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo()
        {
            return configInfo;
        }
    }
    

    注意:Config客户端需要用到 bootstrap.yml 文件来读取Config服务端的配置,那这里为什么不用application.yml 来读取配置而是使用bootstrap.yml ,而 bootstrap.ymlapplication.yml 又有什么不同呢?

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

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

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

    动态刷新

​ 搭建完Config客户端后,我们面临着一个问题:如果后面又更多的微服务需要读取配置中心的配置,那我们每次改完配置以后都得重启Config客户端。那么要如何解决这个问题呢?

使用动态刷新

  • 导入actuator监控依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  • 在 bootstrap.yml 文件中添加如下配置

    #暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
  • 在controller类上添加刷新注解

    @RestController
    @RefreshScope //开启Controller动态刷新功能
    public class ConfigController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo()
        {
            return configInfo;
        }
    }
    
  • 发送post请求刷新

    curl -X POST "http://localhost:3355/actuator/refresh"
    

到这,其实我们大概就可以实现不重启客户端来动态读取配置中心的配置了,但这种方式还是存在着一定的问题:

如果我们需要根据条件刷新不同的客户端或者是刷新多个的客户端的话,我们使用上面的方式会比较麻烦,那么有没有更好的方法能让我们更便捷地实现刷新客户端呢?
在这里我们可以采用消息总线组件来实现动态刷新的功能,但因为SpringcloudConfig在国内的应用并不是很广泛,所以这里就不做多的介绍了,等到后续我们通过讲解Nacos来作为服务配置中心的时候再来讲动态刷新的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小嵌_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值