查看原文
其他

Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config

搜云库 搜云库技术团队 2019-04-07

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在 SpringCloud中,有分布式配置中心组件 spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在 spring cloud config 组件中,分两个角色,一是 config server,二是 config client,业界也有些知名的同类开源产品,比如百度的 disconf

相比较同类产品, SpringCloudConfig最大的优势是和 Spring无缝集成,支持 Spring里面 Environment和 PropertySource的接口,对于已有的 Spring应用程序的迁移成本非常低,在配置获取的接口上是完全一致,结合 SpringBoot可使你的项目有更加统一的标准(包括依赖版本和约束规范),避免了应为集成不同开软件源造成的依赖版本冲突。

Spring Cloud Config 简介

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

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

注意:这是工作的流程图,实际的部署中 SpringCloudBus并不是一个独立存在的服务,这里单列出来是为了能清晰的显示出工作流程。

下图是 SpringCloudConfig结合 SpringCloudBus实现分布式配置的工作流

服务端配置

Config Server

新建项目 spring-cloud-config-server

添加依赖

  1. <dependency>

  2.    <groupId>org.springframework.cloud</groupId>

  3.    <artifactId>spring-cloud-config-server</artifactId>

  4. </dependency>

开启服务注册

在程序的启动类 ConfigServerApplication 通过 @EnableConfigServer 开启 SpringCloudConfig 服务端

  1. package io.ymq.example.config.server;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.cloud.config.server.EnableConfigServer;

  5. @EnableConfigServer

  6. @SpringBootApplication

  7. public class ConfigServerApplication {

  8.    public static void main(String[] args) {

  9.        SpringApplication.run(ConfigServerApplication.class, args);

  10.    }

  11. }

添加配置

配置文件 application.properties

  1. spring.application.name=config-server

  2. server.port=8888

  3. spring.cloud.config.label=master

  4. spring.cloud.config.server.git.uri=https://github.com/souyunku/spring-cloud-config.git

  5. spring.cloud.config.server.git.search-paths=spring-cloud-config

  6. #spring.cloud.config.server.git.username=your username

  7. #spring.cloud.config.server.git.password=your password

  • spring.cloud.config.server.git.uri:配置git仓库地址

  • spring.cloud.config.server.git.searchPaths:配置仓库路径

  • spring.cloud.config.label:配置仓库的分支

  • spring.cloud.config.server.git.username:访问git仓库的用户名

  • spring.cloud.config.server.git.password:访问git仓库的用户密码

Git仓库如果是私有仓库需要填写用户名密码,示例是公开仓库,所以不配置密码。

远程Git仓库

spring-cloud-config 文件夹下有 application-dev.propertiesapplication-test.properties 三个文件,内容依次是: content=hello devcontent=hello testcontent=hello pre

测试服务

启动程序 ConfigApplication 类

访问 SpringCloudConfigServer服务:

http://localhost:8888/springCloudConfig/dev/master

  1. {

  2.    "name": "springCloudConfig",

  3.    "profiles": [

  4.        "dev"

  5.    ],

  6.    "label": "master",

  7.    "version": "b6fbc2f77d1ead41d5668450e2601a03195eaf16",

  8.    "state": null,

  9.    "propertySources": [

  10.        {

  11.            "name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties",

  12.            "source": {

  13.                "content": "hello dev"

  14.            }

  15.        }

  16.    ]

  17. }

证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

  • /{application}/{profile}[/{label}]

  • /{application}-{profile}.yml

  • /{label}/{application}-{profile}.yml

  • /{application}-{profile}.properties

  • /{label}/{application}-{profile}.properties

客户端配置

Config Client

新建项目 spring-cloud-config-client

添加依赖

  1. <dependency>

  2.    <groupId>org.springframework.cloud</groupId>

  3.    <artifactId>spring-cloud-config-client</artifactId>

  4. </dependency>

  5. <dependency>

  6.    <groupId>org.springframework.boot</groupId>

  7.    <artifactId>spring-boot-starter-web</artifactId>

  8. </dependency>

开启服务注册

在程序的启动类 ConfigClientApplication 通过 @Value 获取服务端的 content 值的内容

  1. package io.ymq.example.config.client;

  2. @RestController

  3. @SpringBootApplication

  4. public class ConfigClientApplication {

  5.    @Value("${content}")

  6.    String content;

  7.    @RequestMapping("/")

  8.    public String home() {

  9.        return "content:" + content;

  10.    }

  11.    public static void main(String[] args) {

  12.        SpringApplication.run(ConfigClientApplication.class, args);

  13.    }

  14. }

添加配置

配置文件 application.properties

  1. spring.application.name=config-client

  2. server.port=8088

  3. spring.cloud.config.label=master

  4. spring.cloud.config.profile=dev

  5. spring.cloud.config.uri=http://localhost:8888/

  • spring.cloud.config.label 指明远程仓库的分支

  • spring.cloud.config.profile

  • dev开发环境配置文件

  • test测试环境

  • pro正式环境

  • spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。

测试服务

启动程序 ConfigClientApplication 类

访问服务:http://localhost:8088/

下一篇,继续 SpringCloudConfigServer 整合 eureka, 等更多特性

源码下载

GitHub:https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config

码云:https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config

Contact

  • 作者:鹏磊

  • 出处:http://www.ymq.io/2017/12/13/spring-cloud-config/

  • 版权归作者所有,转载请注明出处

  • Wechat:关注公众号,"搜云库",专注于开发技术的研究与知识分享


    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存