我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.
它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。

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

客户端是微服务架构中的各个微服务应用或基础设施,它们通过制定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
由于配置中心默认采用Git来存储配置信息,因此我们会用到Git相关的内容,如果没有用过Git或者忘记怎么用了,可以参考下廖雪峰老师的Git教程。
另外,我自己用的Git远程仓库是码云。
====================华丽的分割线===================
接下来看下代码怎么实现。

一、准备远程Git仓库

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

二、构建配置中心

  先给出最终代码结构:

  

  搭建过程如下:

  1. 新建maven工程config-server
  2. 修改POM文件
    <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.sam</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version> <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    </parent> <properties>
    <javaVersion>1.8</javaVersion>
    </properties>
    <!-- 使用dependencyManagement进行版本管理 -->
    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies> </dependencyManagement> <dependencies>
    <!-- 引入config server依赖 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency> </dependencies> </project>
  3. 创建启动类
    /**
    * @EnableConfigServer
    *
    * 开启Spring Cloud Config 的服务端功能
    *
    */
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApp { public static void main(String[] args) {
    SpringApplication.run(ConfigServerApp.class, args);
    }
    }
  4. 配置application.properties文件,指定远程仓库信息
    server.port=7001
    spring.application.name=config-server #配置Git仓库的地址
    spring.cloud.config.server.git.uri=https://gitee.com/sam-uncle/spring-cloud-learning/
    #配置仓库路径下的相对搜索位置,可以配置多个
    spring.cloud.config.server.git.search-paths=spring-cloud-config-file
    #这里配置你的Git仓库的用户名
    spring.cloud.config.server.git.username=用户名
    #这里配置你的Git仓库的密码
    spring.cloud.config.server.git.password=密码
  5. 启动并验证

    访问配置信息的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:7001/sam/dev/config-label-test,结果如下:

    

三、实现客户端

    最终代码结构:

  

  搭建过程如下:

  1. 新建maven工程config-client
  2. 修改POM文件
    <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.sam</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version> <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    </parent> <properties>
    <javaVersion>1.8</javaVersion>
    </properties>
    <!-- 使用dependencyManagement进行版本管理 -->
    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies> </dependencyManagement> <dependencies>
    <!-- 引入config依赖 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency> </dependencies> </project>
  3. 创建启动类
    @SpringBootApplication
    public class ConfigClientApp { public static void main(String[] args) {
    SpringApplication.run(ConfigClientApp.class, args);
    } }
  4. 配置bootstrap.properties文件,指定config-server位置
    server.port=7002
    #{application}
    spring.application.name=sam
    #{profile}
    spring.cloud.config.profile=dev
    #{label}
    spring.cloud.config.label=master #config server uri
    spring.cloud.config.uri=http://localhost:7001/
  5. 创建controller
    @RefreshScope
    @RestController
    public class TestController { /**
    * 通过@Value 来讲配置文件中的值写入到代码中
    */
    @Value("${from}")
    private String from; @RequestMapping("/from")
    public String from() {
    return from;
    }
    }
  6. 启动并测试

    

四、工作原理
Spring Cloud Config配置中心的工作原理如下:

  1. 客户端启动时,根据bootstrap.properties中配置的应用名{application}、环境名{profile}、分支名{label},向Config Server请求获取配置信息。
  2. Config Server根据自己维护的Git仓库信息和客户传递过来的配置定位信息去查找配置信息。
  3. 通过git clone命令将找到的配置信息下载到本地(Config Server的文件系统中)。在通过页面访问或启动客户端的时候,我们在服务端能看到如下下载的log:
    -- ::58.055  INFO  --- [nio--exec-] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-/spring-cloud-config-file/sam-dev.properties
    -- ::58.055 INFO --- [nio--exec-] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-/spring-cloud-config-file/sam.properties
  4. Config Server创建Spring 的ApplicationContext实例,并从Git本地仓库中加载配置文件,最后将这些配置内容读取出来返回给客户端。
  5. 客户端在获取外部配置信息后加载到客户端的applicationContext实例。

spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config的更多相关文章

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

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

  2. 【SpringCloud】第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  3. 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...

  4. SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...

  5. SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

  6. SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...

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

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

  8. Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config

    目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...

  9. 第六篇: 分布式配置中心(Spring Cloud Config)

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

随机推荐

  1. spring boot 2.x拦截器导致静态资源404终极解决办法

    首先添加application文件static路径,我的是yml文件 spring: mvc: static-path-pattern: /static/**然后注册拦截器类如下方法; @Overri ...

  2. [NOIP2016]换教室(概率期望$DP$)

    其实吧我老早就把这题切了--因为说实话,这道题确实不难啊--李云龙:比他娘的状压DP简单多了 今天我翻以前在Luogu上写的题解时,突然发现放错代码了,然后被一堆人\(hack\)--蓝瘦啊\(ORZ ...

  3. Jmeter测试实例

    说明:测试某系统登陆功能,登陆中的验证码已被屏蔽,辅助工具Firebug. 1.打开被测系统准备好firebug,准备抓包登陆操作:

  4. python codecs模块 ---处理数据写入文件时会有编码不统一的问题

    我们用open方法打开文件有时候会有一些问题,因为open打开文件只能写入str类型,而不会管字符串是什么编码方式. 示例: >>> fr = open('test.txt','a' ...

  5. Hibernate工作原理及为什么要用?. Struts工作机制?为什么要使用Struts? spring工作机制及为什么要用?

    三大框架是用来开发web应用程序中使用的.Struts:基于MVC的充当了其中的试图层和控制器Hibernate:做持久化的,对JDBC轻量级的封装,使得我们能过面向对象的操作数据库Spring: 采 ...

  6. 12c数据库重启后自动启动pdb

    由于最近管理12c数据库,创建了9个新的pdb.相对于以前11g版本每天一个一个的环境检查,方便了很多. 但是因为一次意外宕机,数据库重启.虽然数据库重启,但是管理的pdb确不能自动重启,所以需要手动 ...

  7. 位图索引对于DML操作的影响

    位图索引相对于常规的B-tree 索引,有着体积更加小的优势,节省空间.对于重复率特别高的字段,比如性别,比如省份.查询效率要优于B-tree 索引.那为什么我们总被告知在业务库中不要使用呢? 业务库 ...

  8. solve the promble of VMware Workstation Ubuntu18.04 ethernet interface losting

    $ ifconfig -aens33: flags=4098<BROADCAST,MULTICAST>  mtu 1500    ether 00:**:**:**:**:**  txqu ...

  9. MySQL+Service+Servlet+Jsp实现Table表格分页展示数据

    下面以一个示例讲解如何使用MySQL+Service+Servlet+Jsp实现Table表格分页展示数据: eg:请假管理系统 要求如下: 一.打开首页页面, 访问查询请假记录的 servlet , ...

  10. 请问在一个命令上加什么参数可以实现下面命令的内容在同一行输出。 echo "zhaokang";echo "zhaokang"

    请问在一个命令上加什么参数可以实现下面命令的内容在同一行输出. echo "zhaokang";echo "zhaokang" [root@zhaokang t ...