1.简介

Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。

其中服务端也称为分布式配置中心,他是独立的微服务应用,用来连接配置仓库并为客户端提供获取接口(这些接口返回配置信息、加密、解密信息等);

客户端是微服务架构中的各个微服务应用或基础设施,它们通过制定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
由于配置中心默认采用Git来存储配置信息.
另外,我自己用的Git远程仓库是码云。

2.git仓库数据准备

  1. 在Gitee上新建一个项目https://gitee.com/colozhu/spring-cloud-learning.git
  2. 在项目下新建子目录spring-cloud-config-file,然后新建三个文件
  3. 内容分别是 from=git-dev-1.0、from=git-test-1.0、from=git-1.0
  4. 新建一个分支feature,新分支里面新建三个同名的文件,不过内容分别是from=git-dev-2.0、from=git-test-2.0、from=git-2.0

  

3.Demo结构

       

这里使用maven父项目,两个子项目.

4.父项目

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.colo</groupId>
<artifactId>colo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <modules>
<module>config-server</module>
<module>config-client</module> </modules> <!-- 使用dependencyManagement进行版本管理 -->
<dependencyManagement>
<dependencies>
<!--Greenwich版本-支持Spring Boot 2.1.X -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies> </project>

5.配置中心(config-server)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.colo</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description> <parent>
<artifactId>colo</artifactId>
<groupId>com.colo</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<!-- 引入config server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

启动类:

package com.colo.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; /**
* @EnableConfigServer 开启Spring Cloud Config 的服务端功能
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
// 分别测试master分支和feature分支
//http://localhost:7002/colozhu/dev/feature
//http://localhost:7002/colozhu/test/master
//http://localhost:7002/colozhu/dev/master
//http://localhost:7002/colozhu/test //默认master分支
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
} }

配置文件:

server.port=7002
spring.application.name=config-server #--------指定远程仓库信息----
# clientApplication客户端启动时候获取分支上的配置参数${from}时候,配置中心会从git仓库拉取colo-dev.properties,colo.properties等文件到本地
# 例如:Adding property source: file:/C:/Users/600336/AppData/Local/Temp/config-repo-1543229677936769440/spring-cloud-config-file/colo-dev.properties #配置Git仓库的地址
spring.cloud.config.server.git.uri=https://gitee.com/colozhu/spring-cloud-learning
#配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#这里配置你的Git仓库的用户名
spring.cloud.config.server.git.username=xxxxxx@qq.com
#这里配置你的Git仓库的密码
spring.cloud.config.server.git.password=xxxxx

启动并验证:

    访问配置信息的URL与配置文件的映射关系如下:

    • /{application}/{profile} [/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{appliction}-{profile}.properties

    上面的url会映射{application}-{profile}.properties对应的配置文件,其中{label}对应Git上不同的分支,默认是master。

    通过浏览器访问 http://localhost:7002/colozhu/dev/feature ,结果如下:

  

6.客户端(config-client)

pom.xml (springboot)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.colo</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot</description> <parent>
<artifactId>colo</artifactId>
<groupId>com.colo</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<!-- 引入config依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

启动类:

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

配置bootstrap.properties文件,指定config-server位置

server.port=7003
#{application} 应用名
spring.application.name=colo
#{profile} 环境名
spring.cloud.config.profile=dev
#{label} 分支名
spring.cloud.config.label=master #config server uri
#指定config-server位置
spring.cloud.config.uri=http://localhost:7002/ # gitee上面的文件colo-dev.properties里面有 from=git-dev-1.0

创建controller:

package com.colo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RefreshScope
@RestController
public class TestController { /**
* 通过@Value 来讲配置文件中的值写入到代码中,
* clientApplication客户端启动时候获取分支上的配置参数${from}时候,配置中心会从git仓库拉取colo-dev.properties,colo.properties等文件到本地
* 例如:Adding property source: file:/C:/Users/600336/AppData/Local/Temp/config-repo-1543229677936769440/spring-cloud-config-file/colo-dev.properties
*/
@Value("${from}")
private String from; //http://localhost:7003/from
@RequestMapping("/from")
public String from() {
return from;
}
}

启动并测试

localhost:7002/from

7.工作原理

  1. 客户端启动时,根据bootstrap.properties中配置的应用名{application}、环境名{profile}、分支名{label},向Config Server请求获取配置信息。
  2. Config Server根据自己维护的Git仓库信息和客户传递过来的配置定位信息去查找配置信息。
  3. 通过git clone命令将找到的配置信息下载到本地(Config Server的文件系统中)。在通过页面访问或启动客户端的时候,我们在服务端能看到如下下载的log(mac上):
-- ::47.552  INFO  --- [nio--exec-] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/var/folders/rv/97y9wj7j3_d759f5tymjvb080000gn/T/config-repo-/spring-cloud-config-file/colo-dev.properties
-- ::47.552 INFO --- [nio--exec-] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/rv/97y9wj7j3_d759f5tymjvb080000gn/T/config-repo-/spring-cloud-config-file/colo.properties

  4.Config Server创建Spring 的ApplicationContext实例,并从Git本地仓库中加载配置文件,最后将这些配置内容读取出来返回给客户端。

  5.客户端在获取外部配置信息后加载到客户端的applicationContext实例。

参考:https://www.cnblogs.com/sam-uncle/p/9036053.html

SpringCloud搭建分布式配置中心(基于git)的更多相关文章

  1. SpringCloud第六步:搭建分布式配置中心

    什么是配置中心 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud con ...

  2. SpringCloud与微服务Ⅹ --- SpringCloud Config分布式配置中心

    一.SpringCloud Config是什么 分布式系统面临的问题 --- 配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个 ...

  3. SpringCloud学习笔记(九):SpringCloud Config 分布式配置中心

    概述 分布式系统面临的-配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...

  4. springCloud学习-分布式配置中心(Spring Cloud Config)

    1.简介 Spring Cloud Config :分布式配置中心,方便服务配置文件统一管理,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中.在spring cloud co ...

  5. SpringCloud Config 分布式配置中心

    一.分布式系统面临的问题---配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的 ...

  6. SpringCloud分布式配置中心Config

    统一管理所有配置. 1.微服务下的分布式配置中心 简介:讲解什么是配置中心及使用前后的好处 什么是配置中心: 一句话:统一管理配置, 快速切换各个环境的配置 相关产品: 百度的disconf 地址:h ...

  7. 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)

    上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...

  8. spring cloud 入门系列七:基于Git存储的分布式配置中心

    我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...

  9. spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

    我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...

随机推荐

  1. 拾遗:Perl 在 Shell 脚本编程中的应用

    Perl 对我用途,仅是作为 Shell 脚本中的文本处理器:在较大的软件工程里,更多的是使用 C.go 等编译型语言. Perl 是一种历史比较悠久的动态编程语言,在各种类 Unix 系统中得到了应 ...

  2. 剑指offer——18打印从1到最大的n位数

    题目: 输入数字n,按顺序打印出从1到最大的n位十进制数.比如输入3,则打印出1.2.3一直到最大的3位数999. 题解: 注意大数溢出问题,故使用字符串更靠谱 class Solution { pu ...

  3. javascript面向对象编程笔记(函数之闭包)

    3 函数 3.5 闭包(closures) 3.5.1 作用域链 与很多程序设计语言不同,javascript不存在大括号级的作用域,但它有函数作用域,即在函数内定义的变量在函数外是不可见的.但如果该 ...

  4. extern const 不能一起用

    转载至:https://www.cnblogs.com/herenzhiming/articles/5442893.html 常变量在定义的时候必须初始化,所以当你在a.cpp中定义extern co ...

  5. JS函数 编程练习 使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数。

    编程练习 使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数. 任务 第一步: 编写代码完成一个函数的定义吧. 第二步: 我们来补充函数体中的控制语句,完成函数功能吧. 提示: ...

  6. 我也可以独立(引用JS外部文件)

    我也可以独立(引用JS外部文件) 通过前面知识学习,我们知道使用<script>标签在HTML文件中添加JavaScript代码,如图: JavaScript代码只能写在HTML文件中吗? ...

  7. ionic js 加载动画 ionSpinner 提供了许多种旋转加载的动画图标。当你的界面加载时,你就可以呈现给用户相应的加载图标。 该图标采用的是SVG

    ionic 加载动画 ion-spinner ionSpinner 提供了许多种旋转加载的动画图标.当你的界面加载时,你就可以呈现给用户相应的加载图标. 该图标采用的是SVG. 用法 <ion- ...

  8. PHP算法之整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 3: 输入: 120输出: 21注 ...

  9. day12 bash中的if、for

    bash 变量bash 定义:x= 作用:记录状态 规则:字母开头,后面可以接字母.数字.下划线 export args:将变量定义为全局变量 $$[]:括号中可以进行简单的数学整数运算,可以用ech ...

  10. SpringBoot2.0+ 使用Log4j2日志输出

    据说Log4j2相比log4j效率有很大提升. pom.xml导入 <dependency> <groupId>org.springframework.boot</gro ...