之前有了解过disconf,也知道它是基于zookeeper来做的,但是对于其运行原理不太了解,趁着周末,debug下源码,也算是不枉费周末大好时光哈 :) 。关于这篇文章,笔者主要是参考disconf源码和官方文档,若有不正确地方,感谢评论区指正交流~
disconf是一个分布式配置管理平台(Distributed Configuration Management Platform),专注于各种 分布式系统配置管理 的通用组件/通用平台, 提供统一的配置管理服务,是一套完整的基于zookeeper的分布式配置统一解决方案。disconf目前已经被多个公司在使用,包括百度、滴滴出行、银联、网易、拉勾网、苏宁易购、顺丰科技 等知名互联网公司。disconf源码地址 https://github.com/knightliao/disconf ,官方文档 https://disconf.readthedocs.io/zh_CN/latest/ 。
目前disconf包含了 客户端disconf-Client和 管理端disconf-Web两个模块,均由java实现。服务依赖组件包括Nginx、Tomcat、Mysql、ZooKeeper,nginx提供反向代理(disconf-web是前后端分离的),Tomcat是后端web容器,配置存储在mysql上,基于zookeeper的wartch模型,实时推送。注意,disconf优先读取本地文件,disconf只支持应用对配置的读操作,通过在disconf-web上更新配置,然后由zookeeper通知到服务实例,最后服务实例去disconf-web端获取最新配置并更新到本地。
disconf 功能特点:
- 支持配置(配置项/配置文件)分布式管理
- 配置发布统一化
- 配置发布、更新统一化,同一个上线包 无须改动配置 即可在 多个环境中(RD/QA/PRODUCTION) 上线
- 配置更新自动化:用户在平台更新配置,使用该配置的系统会自动发现该情况,并应用新配置。特殊地,如果用户为此配置定义了回调函数类,则此函数类会被自动调用
- 上手简单,基于注解或者xml配置方式
功能特点描述图

disconf 架构图

public static DisconfCoreMgr getDisconfCoreMgr(Registry registry) throws Exception { FetcherMgr fetcherMgr = FetcherFactory.getFetcherMgr(); // 不开启disconf,则不要watch了 WatchMgr watchMgr = null; if (DisClientConfig.getInstance().ENABLE_DISCONF) { // Watch 模块 watchMgr = WatchFactory.getWatchMgr(fetcherMgr); } return new DisconfCoreMgrImpl(watchMgr, fetcherMgr, registry); } public static WatchMgr getWatchMgr(FetcherMgr fetcherMgr) throws Exception { synchronized(hostsSync) { // 从disconf-web端获取 Zoo Hosts信息,及zookeeper host和zk prefix信息(默认 /disconf) hosts = fetcherMgr.getValueFromServer(DisconfWebPathMgr.getZooHostsUrl(DisClientSysConfig .getInstance() .CONF_SERVER_ZOO_ACTION)); zooPrefix = fetcherMgr.getValueFromServer(DisconfWebPathMgr.getZooPrefixUrl(DisClientSysConfig .getInstance () .CONF_SERVER_ZOO_ACTION)); /** * 初始化watchMgr,这里会与zookeeper建立连接,如果/disconf节点不存在会新建 */ WatchMgr watchMgr = new WatchMgrImpl(); watchMgr.init(hosts, zooPrefix, DisClientConfig.getInstance().DEBUG); return watchMgr; } return null; }
1.2 注册DisconfAspectJ
往Spring中注册一个aspect类D
