SpringCloud 分布式配置Config Git

个人学习SpringCloud系列 分布式配置Config Git篇

Github Link: https://github.com/panjianlong13/SpringBoot-SpringCloud/tree/master/spring-cloud-config-git


随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错。配置中心便是解决此类问题的灵丹妙药,实现了配置文件统一管理,实时更新。在Spring Cloud中,有分布式配置中心组件spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

Spring Cloud Config简介

配置中心提供的核心功能

1.提供服务端和客户端支持

2.集中管理各环境的配置文件

3.配置文件修改之后,可以快速的生效

4.可以进行版本管理

5.支持大的并发查询

6.支持各种语言

Spring Cloud Config可以完美的支持以上所有的需求。Spring Cloud Config就是我们通常意义上的配置中心,把应用原本放在本地文件的配置抽取出来放在中心服务器,从而能够提供更好的管理、发布能力。SpringCloudConfig分服务端和客户端,服务端负责将git svn中存储的配置文件发布成REST接口,客户端可以从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这需要每个客户端通过POST方法触发各自的/refresh

SpringCloudBus通过一个轻量级消息代理连接分布式系统的节点。这可以用于广播状态更改(如配置更改)或其他管理指令。SpringCloudBus提供了通过POST方法访问的endpoint/bus/refresh,这个接口通常由git的钩子功能调用,用以通知各个SpringCloudConfig的客户端去服务端更新配置。

扫描二维码关注公众号,回复: 4753147 查看本文章

1.Git仓库Config变动

2.Notify Change via Git WebHook

3.Publish Message to Spring Cloud Bus

4.Notify 相关使用该Config的Application

5.Reload Config

6.通过Config Server Pull Config到本地


Spring Cloud Config实战

新建两个SpringBoot项目

1.Config Server

添加依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Enable Config Server服务

application.properties

spring.application.name=config-server
//Config Server端口号
server.port=8888
//Branch Name
spring.cloud.config.label=master
//Git URI
spring.cloud.config.server.git.uri=https://github.com/panjianlong13/SpringBoot-SpringCloud.git
//Property Path
spring.cloud.config.server.git.search-paths=spring-cloud-config-git

spring.cloud.config.server.git.username=Your Git Username
spring.cloud.config.server.git.password=Your Git Password

远程仓库中新建Property文件

启动服务后可以通过访问URL http://localhost:8888/springCloudConfig/dev/master 获得Config

2.Client Server 

添加依赖

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-client</artifactId>
</dependency>

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Enable Config Client 从Config获取Content参数

application.properties

spring.application.name=config-client
//Branch Name
spring.cloud.config.label=master
//使用的Property环境前缀
spring.cloud.config.profile=test
//Config Server访问地址
spring.cloud.config.uri=http://localhost:8080/
//Client Server Port
server.port=8081

启动服务后访问URLhttp://localhost:8081/ 进行测试

猜你喜欢

转载自blog.csdn.net/panjianlongWUHAN/article/details/85614548