SpringCloud配置中心实战
SpringCloud配置中心实战
部分优质内容来源:
简书:CD826:SpringCloud文集
1、统一配置中心(Config)
Spring Cloud Config具有中心化、版本控制、支持动态更新和语言独立等特性。其特点是:
- 提供服务端和客户端支持(Spring Cloud Config Server和Spring Cloud Config Client);
- 集中式管理分布式环境下的应用配置;
- 基于Spring环境,实现了与Spring应用无缝集成;
- 可用于任何语言开发的程序;
- 默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理;
Spring Cloud Config的结构图如下:
从图中可以看出Spring Cloud Config有两个角色(类似Eureka): Server和Client。Spring Cloud Config Server作为配置中心的服务端承担如下作用:
- 拉取配置时更新Git仓库副本,保证是配置为最新;
- 支持从yml、json、properties等文件加载配置;
- 配合Eureke可实现服务发现,配合Cloud Bus(这个后面我们在详细说明)可实现配置推送更新;
- 默认配置存储基于Git仓库(可以切换为SVN),从而支持配置的版本管理.
而对于,Spring Cloud Config Client则非常方便,只需要在启动配置文件中增加使用Config Server上哪个配置文件即可。
1.1 Spring项目配置加载顺序
- 这里是列表文本命令行参数
- SPRING_APPLICATION_JSON 参数
- 从java:comp/env 加载 JNDI 属性
- Java系统属性 (System.getProperties())
- 操作系统环境变量
- 如果有使用 random.* 属性配置,则使用 RandomValuePropertySource 产生
- 外部特定应用配置文件 例如:application-{profile}.properties 或者 YAML variants
- 内部特定应用配置文件 例如:application-{profile}.properties 或者 YAML variants
- 外部应用配置文件 例如:application.properties 或者 YAML variants
- 内部应用配置文件 例如:application.properties 或者 YAML variants
- 加载@Configuration类的 @PropertySource 或者 @ConfigurationProperties 指向的配置文件
- 默认配置,通过SpringApplication.setDefaultProperties 设置
1.2 配置规则详解
Config Client从Config Server中获取配置数据的流程:
- Config Client启动时,根据bootstrap.properties中配置的应用名称(application)、环境名(profile)和分支名(label),向Config Server请求获取配置数据;
- Config Server根据Config Client的请求及配置,从Git仓库(这里以Git为例)中查找符合的配置文件;
- Config Server将匹配到的Git仓库拉取到本地,并建立本地缓存;
- Config Server创建Spring的ApplicationContext实例,并根据拉取的配置文件,填充配置信息,然后将该配置信息返回给Config Client;
- Config Client获取到Config Server返回的配置数据后,将这些配置数据加载到自己的上下文中。同时,因为这些配置数据的优先级高于本地Jar包中的配置,因此将不再加载本地的配置。
Config Server又是如何与Git仓库中的配置文件进行匹配的呢?通常,我们会为一个项目建立类似如下的配置文件:
- mallweb.properties: 基础配置文件;
- mallweb-dev.properties: 开发使用的配置文件;
- mallweb-test.properties: 测试使用的配置文件;
- mallweb-prod.properties: 生产环境使用的配置文件;
当我们访问Config Server的端点时,就会按照如下映射关系来匹配相应的配置文件:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的Url将会映射为格式为:{application}-{profile}.properties(yml)的配置文件。另外,label则对应Git上分支名称,是一个可选参数,如果没有则为默认的master分支。
而Config-Client的bootstrap.properties配置对应如下:
- spring.application.name <==> application;
- spring.cloud.config.profile <==> profile;
- spring.cloud.config.label <==> label.
1.3 Git仓库配置
Config Server默认使用的就是Git,所以配置也非常简单,如上面的配置(application.properties):
spring.cloud.config.server.git.uri=http://
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
那么客户端在请求时服务端就会到该仓库中进行查找。
1.3.1 使用占位符
在服务端配置中我们也可以使用{application}、{profile} 和 {label}占位符,如下:
spring.cloud.config.server.git.uri=http://github.com/cd826/{application}
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
这样,我们就可以为每一个应用客户端创建一个单独的仓库。
这里需要注意的是,如果Git的分支或标签中包含"/“时,在{label}参数中需要使用”(_)"替代,这个主要是避免与Http URL转义符处理的冲突。
1.3.2 模式匹配
也可以使用{application}/{profile}进行模式匹配,以便获取到相应的配置文件。配置示例如下:
spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo
spring.cloud.config.server.git.repos.simple=https://github.com/simple/config-repo
spring.cloud.config.server.git.repos.special.pattern=special*/dev*,*special*/dev*
spring.cloud.config.server.git.repos.special.uri=https://github.com/special/config-repo
spring.cloud.config.server.git.repos.local.pattern=local*
spring.cloud.config.server.git.repos.local.uri=file:/home/configsvc/config-repo
如果模式中需要配置多个值,那么可以使用逗号分隔。
如果{application}/{profile}没有匹配到任何资源,则使用spring.cloud.config.server.git.uri配置的默认URI。
当我们使用yml类型的文件进行配置时,如果模式属性是一个YAML数组,也可以使用YAML数组格式来定义。这样可以设置成多个配个配置文件,如:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
development:
pattern:
- */development
- */staging
uri: https://github.com/development/config-repo
staging:
pattern:
- */qa
- */production
uri: https://github.com/staging/config-repo
1.3.3 搜索目录
当我们把配置文件存放在Git仓库中子目录中时,可以通过设置serch-path来指定该目录。同样,serch-path也支持上面的占位符。示例如下:
spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo
spring.cloud.config.server.git.searchPaths=foo,bar*
这样系统就会自动搜索foo的子目录,以及以bar开头的文件夹中的子目录。
2、配置中心服务端
2.1 pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR5</version>
<relativePath/>
</parent>
·······
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
踩过的坑:
- 第一次添加依赖时,还加入了springboot starter web的依赖,导致项目无法启动。后来发现,注入spring-cloud-starter-parent的父类依赖,然后引入spring-cloud-config-server依赖后,就可以导入对应的包。
- 需要找到对应的spring-cloud的父版本依赖库。
2.2 启动类Application
/**
* @author aishiyushijiepingxing
* @description:添加@EnableConfigServer注解
* @date 2020/6/28
*/
@EnableConfigServer
@SpringBootApplication
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class, args);
}
}
2.3 配置application.yml
server:
port: 7900
spring:
application:
name: config-server
cloud:
config:
server:
git:
# Git仓库地址
uri: xxxx
# Git仓库账号
username: xxxx
# Git仓库密码
password: xxxx
#search-paths: xxxx
# 读取分支
label: master
3、配置中心客户端
config-client可以是任何一个基于Spring boot的应用。
3.1 pom.xml
添加cloud客户端依赖以及cloud-context上下文环境依赖。
<dependency>
<!--Spring Cloud 上下文依赖,为了使bootstrap配置文件生效-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.1.0.RC2</version>
</dependency>
<!--Spring Cloud Config 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.0.RC2</version>
</dependency>
3.2 bootstrap.yml 多环境
server:
port: 8081
management:
security:
enabled: false #SpringBoot 1.5.X 以上默认开通了安全认证,如果不关闭会要求权限
spring:
application:
name: config_client
---
spring:
profiles: dev
cloud:
config:
uri: xxx #Config server的uri
profile: dev #指定的环境
label: master #指定分支
---
spring:
profiles: test
cloud:
config:
uri: xxx #Config server的uri
profile: test #指定的环境
label: master #指定分支
---
spring:
profiles: prod
cloud:
config:
uri: xxx #Config server的uri
profile: prod #指定的环境
label: master #指定分支
4、动态刷新配置
Config-Client中提供了一个refresh端点来实现配置文件的刷新。要想使用该功能,我们需要在Config-Client的pom.xml文件中增加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
这样,当修改配置文件并提交到Git仓库后,就可以使用:http://localhost:8080/refresh刷新本地的配置数据。
最好的方式还是和Spring Cloud Bus进行整合,这样才能实现配置的自动分发,而不是需要手工去刷新配置。
SpringCloud配置中心实战的更多相关文章
- K8S(11)配置中心实战-单环境交付apollo三组件
k8s配置中心实战-交付apollo三组件 目录 k8s配置中心实战-交付apollo三组件 1 apollo简单说明 1.1 apollo最简架构图: 1.2 apollo组件部署关系 2 为app ...
- K8S(12)配置中心实战-多环境交付apollo三组件
k8s配置中心实战-多环境交付apollo三组件 目录 k8s配置中心实战-多环境交付apollo三组件 1.环境准备工作 1.1 zk环境拆分 1.2 namespace分环境 1.3 数据库拆分 ...
- K8S(10)配置中心实战-configmap资源
k8s配置中心实战-configmap资源 目录 k8s配置中心实战-configmap资源 0 configmap前置说明 0.1.1 configmap和secret 0.1.2 怎么使用conf ...
- Consul作为SpringCloud配置中心
一.背景介绍 在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等.一句话非常重要. 另外一篇文章也是这样说的,哈哈. Consul 作为Spring 推荐的分布式调度系统其也具备配置中 ...
- springcloud~配置中心的使用
配置中心作为springcloud里最底层的框架,所发挥的意思是举足轻重的,所以的组件的配置信息都可以通过springcloud config来管理,它会把配置信息分布式的存储到git上,所以信息安全 ...
- zookeeper配置中心实战--solrcloud zookeeper配置中心原理及源码分析
程序的发展,需要引入集中配置: 随着程序功能的日益复杂,程序的配置日益增多:各种功能的开关.参数的配置.服务器的地址…… 并且对配置的期望也越来越高,配置修改后实时生效,灰度发布,分环境.分集群管理配 ...
- springcloud配置中心
SpringCloud Config简介 Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持 ...
- SpringCloud配置中心config
1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config Apollo 实现分布式配置中心 zookeeper:实现分布式配置中心, ...
- springcloud~配置中心实例搭建
server端 build.gradle相关 dependencies { compile('org.springframework.cloud:spring-cloud-config-server' ...
随机推荐
- Java学习日报7.31
package bank;import java.util.*;public class Bank { private Scanner sc=new Scanner(System.in); priva ...
- linux零基础之--使用putty配置
PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件.随着Linux在服务器端应用的普及,Linux系统管理越来越依赖于远程.在各种远程登录工具中,Putty是出色的工具之 ...
- Vue 面试题总结
1. Vue 框架的优点是什么? (1)轻量级框架:只关注视图层,大小只有几十Kb: (2)简单易学:文档通顺清晰,语法简单: (3)数据双向绑定,数据视图结构分离,仅需操作数据即可完 ...
- mysql使用全文索引实现大字段的模糊查询
0.场景说明 centos7 mysql5.7 InnoDB引擎 0.1创建表 DROP TABLE IF EXISTS tbl_article_content; CREATE TABLE tbl_a ...
- MySQL/MariaDB二进制安装
本文说明MySQL/MariaDB二进制安装的过程 mysql和mariadb的安装方式基本一致,唯一初始化方式有点不一样 1.规划: 数据文件存储位置 /data/my ...
- Label_img&a
绝对路径 相对路径 从根目录开始写 从引用的文件所在目录开始写 也可以作为链接提示 target = _blank 新窗口打开 = _self 默认值 本窗口打开 = _new 新窗口打开 = _pa ...
- 【老孟Flutter】为什么 build 方法放在 State 中而不是在 StatefulWidget 中
老孟导读:此篇文章是生命周期相关文章的番外篇,在查看源码的过程中发现了这一有趣的问题,欢迎大家一起探讨. Flutter 中Stateful 组件的生命周期:http://laomengit.com/ ...
- MATLAB OPC错误OPCenum service is not operating correctly解决办法
错误截图: 出错原因:C:\Windows\SysWOW64下没有OpcEnum.exe等文件,opc需要这些文件才能正常运行.有些系统内置了,有些系统没有. 解决方法:去opc官网https://o ...
- sublime python 去掉单行超出字数的白色框框 (E501)
方法一 E501错误:行过长 (大于79个字符),在配置文件里设置 忽略E501错误即可 首选项-->Package Settings-->Anaconda-->Settings - ...
- 通用寄存器_MOV_ADD_SUB_AND_OR_NOT
通用寄存器 MOV指令 注意:目标操作数与操作数宽度必须一样 MOV 目标操作数,源操作数 作用:拷贝源操作数到目标操作数 1.源操作数可以是立即数.通用寄存器.段寄存器.或者内存单元. 2.目标操作 ...