第九章 Spring Cloud Config

9-1. Spring Cloud Config是什么?

​   在分布式的系统中,尤其是当我们的分布式项目越来越多,每个项目都有自己的配置文件,对配置文件的统一管理就成立一种需要,而Spring Cloud Config就提供了对各个分布式项目配置文件的统一管理支持。Spring Cloud Config也叫分布式配置中心,市面上开源的分布式配置中心有很多,比如国内的,360的QConf、淘宝的diamond、百度的disconf都是解决分布式系统配置管理问题,国外也有很多开源的配置中心,Apache的Apache Commons Configuration 、 owner、cfg4j等等。

​   Spring Cloud Config是一个解决分布式系统的配置管理方案。它包含Client和Server两个部分,Server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,Client通过接口获取数据、并依据数据初始化自己的应用。Spring Cloud使用git或svn存放配置文件,默认情况下使用git.

9-2.构建Spring Cloud Config配置中心

  1. 创建一个Spring boot项目

  2. 在pom.xml文件中添加依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  3. 在入口类的方法上添加注解@EnableConfigServer

  4. 在application.properties中配置一下git仓库信息,此处我们使用GitHub(也可以使用码云 gitee),首先在我的GitHub上创建一个名为spring-cloud-config的项目,创建之后,再做以下配置:

    server.port=3721
    spring.application.name=springcloud-config-server
    
    spring.cloud.config.server.git.uri=http://github.com/项目名/spring-cloud-config.git
    spring.cloud.config.server.git.search-paths=config-center
    spring.cloud.config.server.git.username=xxx@163.com
    spring.cloud.config.server.git.password=xxxxxx
    
    1. uri:表示配置中心所在仓库的位置
    2. search-paths:表示仓库下的子目录
    3. username:GitHub的用户名
    4. password:GitHub的密码

9-3. 构建Spring Cloud Config配置中心仓库

​   接下来我们需要在GitHub上设置好配置中心,首先在本地 创建一个文件夹叫config,然后在里面创建一个文件夹叫config-center,然后在config-center中创建四个配置文件:如下

  • application.properties
  • application-dev.properties // 生产环境下的配置文件
  • application-test.properties // 测试环境下的配置文件
  • application-online.properties // 上线环境下的配置文件

在四个文件中分别写上要测试的内容

  • url=http://www.zjtd.com
  • url=http://dev.zjtd.com
  • url=http://test.zjtd.com
  • url=http://online.zjtd.com

然后回到config目录下,一次执行如下命令将本地文件同步到GitHub的仓库中:

说明:在使用命令之前先从http://git-scm.com/网站下载安装git的Windows客户端

  1. 添加提交人的账号信息,git需要知道提交人的信息作为表示

    git config --global user.name 'xxx'
    git config --global user.email 'xxx@163.com'
    
  2. 将该目录变为git可以管理的目录

    git init 
    
  3. 将文件添加到暂存区

    git add config-center/
    
  4. 把文件提交到本地仓库

    git commit -m 'add config-center'
    
  5. 添加到远程主机 origin为主机名称

    git remote add origin http://github.com/项目名/spring-cloud-congfig.git
    
  6. 将本地的master 分支推送到origin主机

    git push -u origin master 
    

  至此,我们的配置文件就上传到GitHub上了。

  此时启动我们的配置中心,通过/{application}/{profile}/{label}就能访问到我们的配置文件了;

  • {application}表示配置文件的名字
  • {profile}表示环境有dev/test/online及默认
  • {label}表示分支,默认我们放在master分支上

  通过浏览器访问http://localhost:3721/application/dev/master返回的JSON格式的数据:

  name表示配置文件名application部分,

  profiles表示环境部分,

  label表示分支,

v  ersion表示GitHub上提交时产生的版本号,

  同时当我们访问成功后,在控制台会打印出相关的日志信息,

  当访问成功后配置中心会通过git clone 命令将远程的配置文件在本地也保存一份,以确保在git仓库故障时我们的应用还可以继续正常使用。

9-4.构建Spring Cloud Config配置中心客户端

  1. 创建一个springboot工程 名叫spring-cloud-config-client

  2. 添加相关依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
  3. 创建bootstarp.properties文件,用于获取配置信息,文件内容如下(注意配置信息一定要放在bootstarp.properties文件中才有效):

    server.port=3722
    spring.application.name=application
    spring.cloud.config.profile=dev
    spring.cloud.config.label=master
    spring.cloud.config.uri=http://localhost:3721/
    
    • name 对应配置文件中的application部分
    • profile对应的是profile部分
    • label对应的是label部分、
    • uri表示配置中心的地址
  4. 创建一个Controller进行测试

    1. 第一种方式获取
    @RestController
    public class ConfigController{
        @Value("${url}")
        private String url;
        
        @RequestMapping("/cloud/url")
        public String url(){
            return url;
        }
    }
    

    ​ 2.第二种方式获取

    @RestController
    public class ConfigController{
        @Autowired
        private Environment env;
        
        @RequestMapping("/cloud/url2")
        public String url2(){
            return env.getProperty("url");
        }
    }
    

  我们可以直接使用@Value注解注入配置的属性值,也可以通过Environment对象来获取配置的属性值。

9-5. Spring Cloud配置中心的工作原理

Spring Cloud Config Server的工作过程如下图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1hTkf6Dd-1613383280014)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210215163006028.png)]

  1. 首先需要一个远程git仓库,平时测试可以使用GitHub ,在实际生产环境中,需要搭建一个git 服务器,远程Git仓库的主要作用是保存我们的配置文件。
  2. 除了远程的Git仓库之外,我们还需要一个本地的Git仓库,每当Config Server访问远程Git仓库时,都会克隆一份到本地,这样当远程仓库无法连接时,就直接使用本地存储的配置信息。
  3. 微服务A和B则是我们具体的应用,这些应用在启动的时候会从config server 中获取相应的配置文件。
  4. 当微服务A和B尝试从config server中加载配置信息时,config server会先通过Git clone 命令克隆一份配置文件保存到本地。
  5. 由于配置文件是存储在Git仓库中,所以配置文件天然具有版本管理功能。

9-6.Spring Cloud 的安全保护

​ 生产环境中我们的配置中心肯定是不能随随便便被人访问的,我们可以加上适当的保护机制,由于微服务是构建在Spring Boot之上的,所以整合Spring Security是最方便的方式。

  1. 在spring cloud config server 项目中添加依赖

    <dependency>
    	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-cloud-starter-security</artifactId>
    </dependency>
    
  2. 在spring cloud config server项目的application.properties中配置用户名和密码

    spring.security.user.name=admin
    spring.security.user.password=123456
    
  3. 在spring cloud config client项目的bootstrap.properties中配置用户名和密码

    spring.cloud.config.username=admin
    spring.cloud.config.password=123456
    
  4. 最后验证测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值