部分优质内容来源:

简书: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作为配置中心的服务端承担如下作用:

  1. 拉取配置时更新Git仓库副本,保证是配置为最新;
  2. 支持从yml、json、properties等文件加载配置;
  3. 配合Eureke可实现服务发现,配合Cloud Bus(这个后面我们在详细说明)可实现配置推送更新;
  4. 默认配置存储基于Git仓库(可以切换为SVN),从而支持配置的版本管理.

    而对于,Spring Cloud Config Client则非常方便,只需要在启动配置文件中增加使用Config Server上哪个配置文件即可。

1.1 Spring项目配置加载顺序

  1. 这里是列表文本命令行参数
  2. SPRING_APPLICATION_JSON 参数
  3. 从java:comp/env 加载 JNDI 属性
  4. Java系统属性 (System.getProperties())
  5. 操作系统环境变量
  6. 如果有使用 random.* 属性配置,则使用 RandomValuePropertySource 产生
  7. 外部特定应用配置文件 例如:application-{profile}.properties 或者 YAML variants
  8. 内部特定应用配置文件 例如:application-{profile}.properties 或者 YAML variants
  9. 外部应用配置文件 例如:application.properties 或者 YAML variants
  10. 内部应用配置文件 例如:application.properties 或者 YAML variants
  11. 加载@Configuration类的 @PropertySource 或者 @ConfigurationProperties 指向的配置文件
  12. 默认配置,通过SpringApplication.setDefaultProperties 设置

1.2 配置规则详解

Config Client从Config Server中获取配置数据的流程:

  1. Config Client启动时,根据bootstrap.properties中配置的应用名称(application)、环境名(profile)和分支名(label),向Config Server请求获取配置数据;
  2. Config Server根据Config Client的请求及配置,从Git仓库(这里以Git为例)中查找符合的配置文件;
  3. Config Server将匹配到的Git仓库拉取到本地,并建立本地缓存;
  4. Config Server创建Spring的ApplicationContext实例,并根据拉取的配置文件,填充配置信息,然后将该配置信息返回给Config Client;
  5. Config Client获取到Config Server返回的配置数据后,将这些配置数据加载到自己的上下文中。同时,因为这些配置数据的优先级高于本地Jar包中的配置,因此将不再加载本地的配置。

Config Server又是如何与Git仓库中的配置文件进行匹配的呢?通常,我们会为一个项目建立类似如下的配置文件:

  • mallweb.properties: 基础配置文件;
  • mallweb-dev.properties: 开发使用的配置文件;
  • mallweb-test.properties: 测试使用的配置文件;
  • mallweb-prod.properties: 生产环境使用的配置文件;

当我们访问Config Server的端点时,就会按照如下映射关系来匹配相应的配置文件:

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{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>

踩过的坑:

  1. 第一次添加依赖时,还加入了springboot starter web的依赖,导致项目无法启动。后来发现,注入spring-cloud-starter-parent的父类依赖,然后引入spring-cloud-config-server依赖后,就可以导入对应的包。
  2. 需要找到对应的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配置中心实战的更多相关文章

  1. K8S(11)配置中心实战-单环境交付apollo三组件

    k8s配置中心实战-交付apollo三组件 目录 k8s配置中心实战-交付apollo三组件 1 apollo简单说明 1.1 apollo最简架构图: 1.2 apollo组件部署关系 2 为app ...

  2. K8S(12)配置中心实战-多环境交付apollo三组件

    k8s配置中心实战-多环境交付apollo三组件 目录 k8s配置中心实战-多环境交付apollo三组件 1.环境准备工作 1.1 zk环境拆分 1.2 namespace分环境 1.3 数据库拆分 ...

  3. K8S(10)配置中心实战-configmap资源

    k8s配置中心实战-configmap资源 目录 k8s配置中心实战-configmap资源 0 configmap前置说明 0.1.1 configmap和secret 0.1.2 怎么使用conf ...

  4. Consul作为SpringCloud配置中心

    一.背景介绍 在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等.一句话非常重要. 另外一篇文章也是这样说的,哈哈. Consul 作为Spring 推荐的分布式调度系统其也具备配置中 ...

  5. springcloud~配置中心的使用

    配置中心作为springcloud里最底层的框架,所发挥的意思是举足轻重的,所以的组件的配置信息都可以通过springcloud config来管理,它会把配置信息分布式的存储到git上,所以信息安全 ...

  6. zookeeper配置中心实战--solrcloud zookeeper配置中心原理及源码分析

    程序的发展,需要引入集中配置: 随着程序功能的日益复杂,程序的配置日益增多:各种功能的开关.参数的配置.服务器的地址…… 并且对配置的期望也越来越高,配置修改后实时生效,灰度发布,分环境.分集群管理配 ...

  7. springcloud配置中心

    SpringCloud Config简介 Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持 ...

  8. SpringCloud配置中心config

    1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config Apollo 实现分布式配置中心 zookeeper:实现分布式配置中心, ...

  9. springcloud~配置中心实例搭建

    server端 build.gradle相关 dependencies { compile('org.springframework.cloud:spring-cloud-config-server' ...

随机推荐

  1. java ListNode 链表

    链表是一种数据结构:由数据和指针构成,链表的指针指向下一个节点. java ListNode 链表 就是用Java自定义实现的链表结构. 基本结构: class ListNode { //类名 :Ja ...

  2. Python将GIF图片转换成png图片帧

    效果图: 转换之后保存到文件夹中: 代码如下:(第三方库pillow,安装方法:在cmd中输入:  pip install pillow) from PIL import Image import o ...

  3. Java学习日报7.14

    package fabs;import java.util.Scanner;public class Fabs { public static void main(String args[]) { S ...

  4. 第13章节 BJROBOT 雷达跟随【ROS全开源阿克曼转向智能网联无人驾驶车】

    雷达跟随说明:注意深度摄像头的 USB 延长线,可能会对雷达扫描造成影响, 所以在雷达跟随前,把深度摄像头的 USB 延长线取下.另外雷达跟随范围大概是前方 50cm 和 120°内扫描到的物体都可以 ...

  5. 用python+sklearn(机器学习)实现天气预报数据 模型和使用

    用python+sklearn机器学习实现天气预报 模型和使用 项目地址 系列教程 0.前言 1.建立模型 a.准备 引入所需要的头文件 选择模型 选择评估方法 获取数据集 b.建立模型 c.获取模型 ...

  6. 字典实现:python-----VS----java

    对比python和java的字典数据结构,以下就LeetCode面试题 17.10. 主要元素为栗子,进行学习.是一道简单题目,重点看数据结构的运用与实现. 普通的思路,使用一个字典结构记录每个元素出 ...

  7. java反射-Method中的invoke方法的用法-以及函数式接口和lambda表达式

    作者最近研究框架底层代码过程中感觉自己基础不太牢固,于是写了一点案例,以防日后忘记 接口类:Animals 1 public interface Animals { 2 3 public void e ...

  8. wpf 中用 C# 代码创建 PropertyPath ,以对间接目标进行 Storyboard 动画.

    如图,一个 Rectangle 一个 Button ,点击按钮时要通过动画完成对 Rectangle填充色的渐变动画. Xaml: 1 <Window 2 x:Class="WpfAp ...

  9. 八:架构,搭建,WAF

    WAF防护分析 什么是WAF应用 如何快速识别WAF 识别WAF对于安全测试的意义 CMS识别技术 源码获取技术 架构信息获取 站点搭建分析 搭建习惯-目录型站点 sti.blcu-bbs 目录型站点 ...

  10. 基于numpy.einsum的张量网络计算

    张量与张量网络 张量(Tensor)可以理解为广义的矩阵,其主要特点在于将数字化的矩阵用图形化的方式来表示,这就使得我们可以将一个大型的矩阵运算抽象化成一个具有良好性质的张量图.由一个个张量所共同构成 ...