Distributed Configuration Center Selection

As your business grows, configuration upgrade micro-services architecture, service number, the program is increasing (various micro service, address various servers, various parameters), the traditional way of configuration files and databases has been unable to meet the development personnel configuration management requirements:

  • Security: Configure follow the source code stored in the code base, making it easy to configure leak.
  • Timeliness: modify the configuration, you need to restart the service to take effect.
  • Limitations: Can not support dynamic adjustments: for example, the log switch, function switch.

Therefore, a distributed configuration center came into being!

Open source projects

About a distributed configuration center, the Internet has a lot of open source solutions, such as:

Apollo

Apollo (Apollo) is a research and development department Ctrip framework for distributed configuration center, capable of centralized management applications in different environments, different cluster configuration, the configuration changes can be pushed to real-time application side, and have standardized permissions, process control and other features, applicable to micro-service configuration management scenarios.

Project address: https: //github.com/ctripcorp/apollo

Diamond

Diamond Taobao research and development of distributed configuration management systems. Diamond can make use of the service process in a cluster-aware dynamic data changes, without having to restart the service to update configuration data can be achieved.

Project address: https: //github.com/gzllol/diamond

Disconf

Focusing on a variety of "distributed configuration management system," the "general assembly" and "common platform" to provide a unified "Configuration Management Service"

Project address: https: //github.com/knightliao/disconf

Comparison Project

Comprehensive comparison, think Ctrip Apollo powerful sound, the open source community on github more active, the code has been maintained, and clearly written documentation, so the Apollo chose as our distributed configuration center. The following briefly describes the next Apollo project.

About Apollo

Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
服务端基于Spring Boot和Spring Cloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。
Java客户端不依赖任何框架,能够运行于所有Java运行时环境,同时对Spring/Spring Boot环境也有较好的支持。
.Net客户端不依赖任何框架,能够运行于所有.Net运行时环境。

特性

  • 统一管理不同环境、不同集群的配置
    • Apollo提供了一个统一界面集中式管理不同环境(environment)、不同集群(cluster)、不同命名空间(namespace)的配置。
    • 同一份代码部署在不同的集群,可以有不同的配置,比如zookeeper的地址等
    • 通过命名空间(namespace)可以很方便地支持多个不同应用共享同一份配置,同时还允许应用对共享的配置进行覆盖
  • 配置修改实时生效(热发布)
    • 用户在Apollo修改完配置并发布后,客户端能实时(1秒)接收到最新的配置,并通知到应用程序
  • 版本发布管理
    • 所有的配置发布都有版本概念,从而可以方便地支持配置的回滚
  • 灰度发布
    • 支持配置的灰度发布,比如点了发布后,只对部分应用实例生效,等观察一段时间没问题后再推给所有应用实例
  • 权限管理、发布审核、操作审计
    • 应用和配置的管理都有完善的权限管理机制,对配置的管理还分为了编辑和发布两个环节,从而减少人为的错误。
    • 所有的操作都有审计日志,可以方便地追踪问题
  • 客户端配置信息监控
    • 可以在界面上方便地看到配置在被哪些实例使用
  • 提供Java和.Net原生客户端
    • 提供了Java和.Net的原生客户端,方便应用集成
    • 支持Spring Placeholder, Annotation和Spring Boot的ConfigurationProperties,方便应用使用(需要Spring 3.1.1+)
    • 同时提供了Http接口,非Java和.Net应用也可以方便地使用
  • 提供开放平台API
    • Apollo自身提供了比较完善的统一配置管理界面,支持多环境、多数据中心配置管理、权限、流程治理等特性。不过Apollo出于通用性考虑,不会对配置的修改做过多限制,只要符合基本的格式就能保存,不会针对不同的配置值进行针对性的校验,如数据库用户名、密码,Redis服务地址等
    • 对于这类应用配置,Apollo支持应用方通过开放平台API在Apollo进行配置的修改和发布,并且具备完善的授权和权限控制
  • 部署简单
    • 配置中心作为基础服务,可用性要求非常高,这就要求Apollo对外部依赖尽可能地少
    • 目前唯一的外部依赖是MySQL,所以部署非常简单,只要安装好Java和MySQL就可以让Apollo跑起来
    • Apollo还提供了打包脚本,一键就可以生成所有需要的安装包,并且支持自定义运行时参数

发布配置

通过配置中心发布配置:

填写发布信息:

客户端获取配置(Java API样例)

配置发布后,就能在客户端获取到了,以Java为例,获取配置的示例代码如下。Apollo客户端还支持和Spring整合,更多客户端使用说明请参见Java客户端使用指南和.Net客户端使用指南。

Config config = ConfigService.getAppConfig();
Integer defaultRequestTimeout = 200;
Integer requestTimeout = config.getIntProperty("requestTimeout", defaultRequestTimeout);

客户端监听配置变化

通过上述获取配置代码,应用就能实时获取到最新的配置了。
不过在某些场景下,应用还需要在配置变化时获得通知,比如数据库连接的切换等,所以Apollo还提供了监听配置变化的功能,Java示例如下:

Config config = ConfigService.getAppConfig();
config.addChangeListener(new ConfigChangeListener() {
  @Override
  public void onChange(ConfigChangeEvent changeEvent) {
    for (String key : changeEvent.changedKeys()) {
      ConfigChange change = changeEvent.getChange(key);
      System.out.println(String.format(
        "Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s",
        change.getPropertyName(), change.getOldValue(),
        change.getNewValue(), change.getChangeType()));
     }
  }
});

Spring集成样例

Apollo和Spring也可以很方便地集成,只需要标注@EnableApolloConfig后就可以通过@Value获取配置信息:

@Configuration
@EnableApolloConfig
public class AppConfig {}

@Component
public class SomeBean {
    //timeout的值会自动更新
    @Value("${request.timeout:200}")
    private int timeout;
}

总结

本文主要介绍和对比了几种开源分布式配置中心的选型,并重点介绍了下我们最终选择的携程Apollo(阿波罗)分布式配置中心的特性和简单使用,有兴趣的读者可以到具体项目的官网进行详细研究。

Guess you like

Origin www.cnblogs.com/xiaodf/p/11214775.html