一.什么是分布式配置中心?

  就是为微服务架构中的微服务提供集中化的外部配置支持,配置中心为各个微服务应用的所有环境提供了中心化的外部配置(可能比较难理解,想知道是什么意思就要知道为什么这么配置:这么配置就是为了解决微服务中很多个provider中的application.properties配置管理问题,以及配置冗余问题,把这些配置集中到一起进行存放,并且把重复的配置提取出来解决冗余)

二.图解运行


  1 . git hub 上存放我们的配置文件
  2 . config-server 远程连接到 git hub
  3 . config-client 连接到config-server
  运行:当我们启动config-client 服务的时候,client 会通过连接的 config-server 拿到远程git 上面的配置文件,然后通过 Spring 加载到对象中。

三.如何简单实现springcloud config的分布式配置中心

1.创建github账号

2.在github上面创建repository(仓库)

  一个github账号可以有很多个仓库—>一个仓库只能对应一个项目—>所以仓库的名称就是所要提交的项目名
  如果是一个新的账号,就必须先有一个命名空间(也是自己创建的,可以随意起名)

3.使用github desktop把项目加载到本地

  选择File–>clone repository–>选择需要加载到本地的项目

4.创建三个文件(分为dev:开发环境,test:测试环境,pro:上线环境)

  开发环境:
  application-dev.properties
  spring.profiles=dev   server.port=3081
    
  spring.application.name=application-dev
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/dev?useSSL=false
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  测试环境:
  application-test.properties
  spring.profiles=test   server.port=3081   spring.application.name=application-test
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

5.把配置好的三个文件提交到github上

  当使用github desktop提交代码到github上的时候,只能一个一个的提交,不能一起提交
  选择commit to master(记住这个master)
  选择repository选择–>push

四.分布式配置中心的访问规则:

  无论是yml还是properties都可以使用该规则进行访问:
  /{application}/{profile}[/{label}]

  properties文件:
  /{application}-{profile}.properties
  /{label(分支)}/{application}-{profile}.properties

  yml文件:
  /{application}-{profile}.yml
  /{label}/{application}-{profile}.yml

五.配置springcloud config

  5.1 server层的配置

    5.1.1 jar包
    <dependencies>
     <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
    </dependencies>
    5.1.2 application.properties配置
    #首先还是常规的:
    server.port=4081
    server.servlet.context-path=/
    #配置application.name (可配置,可不配置) ,在此配置是为了提醒eureka中的这个配置(因为eureka中服务的发现就是找的这个名字),不要忘记
    spring.application.name=springcloud-config-server-4081
    #开始配置GitHub
    #先配置GitHub的仓库的地址(在浏览器的地址栏上,直接复制就好)
    spring.cloud.config.server.git.uri=https://github.com/命名空间/仓库名     #配置GitHub的账号和密码
    spring.cloud.config.server.git.username=邮箱/账号
    spring.cloud.config.server.git.password=密码
    #配置GitHub的仓库的搜索路径(固定的不要补全!!!)
    spring.cloud.config.server.git.search-paths=config-repo
    #跳过SSL的认证
    spring.cloud.config.server.git.skip-ssl-validation=true
  
  5.1.3 ApplicationRun启动类注解
    除了常规的@@SpringBootApplication外,还有一个@EnableConfigServer,标明是server层的配置中心

5.2 client 层的配置

  5.2.1 jar包
  <dependencies>
   <dependency>
   <groupId>com.wlx.springcloud</groupId>
   <artifactId>20191108-management-model</artifactId>
   <version>1.0-SNAPSHOT</version>
   </dependency>
   <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   </dependency>
   <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   </dependency>
   <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   </dependency>
   <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
  </dependencies>
  5.2.2 配置config 文件

  此处的配置文件和之前的有区别,有两个配置文件 bootstrap.properties 和 application.properties 文件,配置两个文件的作用就是:查漏补缺。
把相同的配置放到了GitHub上,有差异的配置放在了application文件中,加载的时候会把这两个文件进行合并
  Bootstrap.properties文件:

  #从github上读取所要配置的文件的名称
  #从GitHub上的repository读取文件名
  #根据读取的规则:不要加后缀名 .properties或.yml
  spring.cloud.config.name=application-dev
  #配置prpfile的名称
  #必须要和GitHub上文件配置中心中的spring.profile的值一致,否则匹配不到
  spring.cloud.config.profile=dev
  #配置label(master) 如果使用默认的就不需要配置
  spring.cloud.config.label=master
  #配置config的 服务器端 的地址及端口
  spring.cloud.config.uri=http://localhost:端口号

  Application.properties文件:

  #一定要和bootstrap.properties中的spring.cloud.config.name的值一致,否则映射不到`
  spring.application.name=application-dev
  5.2.3 测试是否链接成功server层的服务器,加载云端的配置文件

  新创建一个controller目录 –-> 创建一个controller测试类 TestController ,利用@Value注解获取配置文件中的值

  @RestController
  public class TestController {      @Value("${spring.datasource.driver-class-name}")
   private String driverClassName; @RequestMapping("/test")
public String test(){
return driverClassName;
}
}

初次写博客,不喜勿喷!!!

Springcloud 2.x 版本 分布式配置中心的更多相关文章

  1. SpringCloud(6)分布式配置中心Spring Cloud Config

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

  2. 白话SpringCloud | 第八章:分布式配置中心的服务化及动态刷新

    前言 上一章节,简单介绍了分布式配置中心Spring Cloud Config的使用.同时,我们也遗漏了一些问题,比如如何配置实时生效,当服务端地址变更或者集群部署时,如何指定服务端地址?回想,在服务 ...

  3. SpringCloud使用Consul作为分布式配置中心

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_36027670/article/de ...

  4. SpringCloud学习之Config分布式配置中心(八)

    统一配置中心概述 如果微服务架构中没有使用统一配置中心时,所存在的问题: 配置文件分散在各个项目里,不方便维护 配置内容安全与权限,实际开发中,开发人员是不知道线上环境的配置的 更新配置后,项目需要重 ...

  5. SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管 ...

  6. SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...

  7. Spring-cloud微服务实战【九】:分布式配置中心config

      回忆一下,在前面的文章中,我们使用了spring cloud eureka/ribbon/feign/hystrix/zuul搭建了一个完整的微服务系统,不管是队内还是对外都已经比较完善了,那我们 ...

  8. SpringCloud系列之分布式配置中心极速入门与实践

    SpringCloud系列之分布式配置中心极速入门与实践 @ 目录 1.分布式配置中心简介 2.什么是SpringCloud Config? 3.例子实验环境准备 4.Config Server代码实 ...

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

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

随机推荐

  1. POJ 1912 凸包

    题目: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib& ...

  2. pycharm如何添加固定代码块

    1. file -- settings -- 搜索框输入live,找到 Live Templates 2. 选择你要添加到哪个语言中去,打开python组,并点击右上角 “+”,选择 1.Live T ...

  3. springboot(二).springboot整合logback用于日志输出

    springboot整合logback用于日志输出 我们项目的基本框架已经完成,http请求已经可以访问,现在给我们的框架添加日志记录的功能并能将每天的记录记录到文件中去 在这里,我们使用logbac ...

  4. Oracle-SQL程序优化3

    最近一个星期ETL无论在凌晨或是在中午的JOB执行过程中经常卡住,导致不能按时完成系统引擎的运行,对业务产生影响. 通过生成AWR报告,发现有三条SQL消耗大量的CPU,而且还没有执行完成被终止的.如 ...

  5. 听说你还不理解JavaScript闭包

    闭包(Closure) 闭包是一个函数和词法环境的组合,函数声明在这个词法环境中 词法作用域 看下面一个例子 function init() { var name = 'Mozilla'; // na ...

  6. php GD库简单使用和封装

    GD库创建图像步骤 <?php //1.创建画布 $width = 300; $height= 200; $image=imagecreatetruecolor($width,$height); ...

  7. iOS自动化--Spaceship使用实践

    Spaceship ### 脚本操作 证书,app,provision等一些列apple develop后台操作,快速高效. github地址 spaceship开发文档 文档有列出常用的api调用d ...

  8. Sensor在内核中的驱动框架【转】

    本文转载自:http://blog.csdn.net/armfpga123/article/details/52840370 内核中对sensor的抽象:drivers/sensors/sensors ...

  9. Dubbo负载均衡:最少活跃数(LeastActive)

    官方文档定义 最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差. 使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大. 关于活跃数 最少活跃数负载均衡,最关键的点在于活跃数.活跃 ...

  10. acl权限设置

    acl权限分配 1.setfacl设置文件或目录的权限 setfacl -m u:user:rw text setfacl -m user:rwx /mnt 2.getfacl查看文件或目录的权限 g ...