SpringCloud Config配置中心原理以及环境切换

springCloud config项目,用来为分布式的微服务系统中提供集成式外部配置支持,分为客户端和服务端

spring官方如下介绍:

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

简而言之: 通过配置服务(Config Server)来为所有的环境和应用提供外部配置的集中管理,这些概念都通过spring的Environment和PropertySource来抽象,所以他可以适用于各类Spring应用,它也能对应用的开发环境、测试环境、生成环境的配置做切换、迁移

原理介绍

git服务器会从远程git拉取配置文件,并存入到本地git文件库,当远程git不可用时,会从本地git文件库拉取配置信息

 

一、Config Server

  • 引入依赖
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
          <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka</artifactId>
     </dependency>

    接入配置中心,让客户端可以发现服务端,启动类加上@EnableConfigServer,@EnableDiscoveryClient注解,命名chu-config

  • 配置中心服务器,会根据spring.cloud.config.server.git.uri来找到配置数据(它可以是git存储库的位置,也可以是本地文件),这是必须的,Config server才能从远程Git服务pull资源来配置
  • 在远程码云仓储中新建application.yml和chu-user.yml配置文件

 

二、Config client

在项目中,基本上所有的基础微服务都是config client,它们都通过config server做外部配置集中管理和动态环境切换,客户端默认拉取规则如下:

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

name:即spring.application.name

profile: 激活的剖面

label: git分支,默认是master

例如,这里搭建一个chu-user服务:

  • 引入客户端依赖
 <dependency>
     <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-client</artifactId>
 </dependency>
  • 在配置文件中通过spring.cloud.config.discovery.enabled=true和spring.cloud.config.discovery.service-id=chu-config来注册发现配置中心服务端

 

测试:

上头我们介绍了配置中心的拉取规则

远程码云chu-user.yml上有个test:123的属性,在chu-user服务上可通过@Value("${test}")注解获取到,如果chu-user服务上的配置文件中也有个test:456的属性,默认情况下,Config Server优先

经过测试,我们证明了config client可以成功的拉取到远程服务器的配置文件,那么不同环境的配置切换拉取怎么做呢?

  1. 在远程码云上改造chu-user.yml配置文件如下:
    ---
    spring:
      profiles: dev
    isDev: true
    
    ---
    spring:
      profiles: pro
    isDev: false

    重启config server,在浏览器输入localhost:8888/chu-user-dev.yml可以成功拉取到配置信息

  2. 客户端通过spring.cloud.config.profile=pro/dev来指定拉取的环境配置

测试:启动config server和config client,并在chu-user服务控制台看到

分别拉取到application#pro环境和chu-user#pro环境信息,同时程序通过@Value("${isDev}")读取配置值为false

每个资源也可以选择在子目录存储配置文件,通过关键字searchPaths来查询定义的目录,例如

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/China_shenzhen_git/one-config-server
          search-paths: /config,'{application}'

将会从config目录和与application相同名字的目录中开始查询配置文件

Config server如果希望客户端能够授权访问配置中心库,可以引入security配置,引入依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
security:
  basic:
    enabled: true
  user:
    name: config-repo
    password: 123456

那么客户端就需要增加spring.cloud.config.username=config-repo ,password=123456配置来授权访问配置中心服务器

注意:

  1. spring profiles进行不同环境版本配置分离、切换,通过spring.profiles.active=dev,mysql,如果配置文件基于文件的,服务器将优先根据{applicationName}.yml,在根据application.yml创建一个Environment对象,如果这些yml文件中有了指定的spring profiles,那么这些profiles将有较高优先级
  2. spring.profile.avtive是指定spring boot运行的环境,而spring.cloud.config.profile是客户端指定拉取资源库的profile配置,如果有多个profiles,最后一个起作用
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Cloud Config配置中心有以下功能: 1. 集中管理应用程序的配置信息 2. 支持多环境配置 3. 支持加密/解密配置信息 4. 支持客户端自动更新配置信息 5. 支持版本管理 6. 支持配置信息的动态刷新 ### 回答2: Spring Cloud Config配置中心是一个可以集中管理、动态更新分布式系统中的配置的工具。它具有以下功能: 1. 集中管理:Spring Cloud Config可以将分布式系统的配置集中存储在一个地方,使得配置信息更加可控和可维护。开发人员可以通过配置服务器统一管理各个环境(如开发、测试、生产)的配置文件,减少配置文件的传统散落问题。 2. 动态更新:在分布式系统中,如果某些配置信息发生变化,传统方式需要重新部署应用程序才能使变化生效。而使用Spring Cloud Config可以实现动态更新,即可以在不重新部署应用的情况下,及时将新的配置信息更新到应用程序中,减少了重新部署对系统的影响和停机时间。 3. 版本管理:Spring Cloud Config允许配置文件进行版本管理,每次更新配置文件都会生成一个新的版本,方便进行配置的历史版本查看和回滚操作。这样可以有效地避免因为配置错误而导致的系统故障,并提高系统的可靠性和稳定性。 4. 分布式支持:Spring Cloud Config支持将配置文件分布式地存储在多个配置服务器中,可以根据需求进行水平扩展和负载均衡,确保系统可扩展性和高可用性。 5. 安全性:Spring Cloud Config提供了对配置文件的安全保护机制,开发人员可以通过对配置文件进行加密和解密操作,确保配置文件的安全性,防止敏感信息泄露。 总的来说,Spring Cloud Config配置中心提供了集中管理、动态更新、版本管理、分布式支持和安全性等功能,帮助开发人员更加方便地管理和维护分布式系统的配置信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值