模块的设计
基本架构
配置管理模块是很多分布式系统的基础,它的主要功能是将用户更新的配置信息实时的同步到各个节点的服务上,以便它们近似实时的加载这些配置,典型的架构如下图所示
工作流程
- 服务启动后,ConfigFetcher模块与ZooKeeper服务通讯,读取znode节点/serviceA/conf中保存的配置数据,并向该znode节点注册一个watcher以监听节点数据的变更。
- 用户通过ConfigUpdater动态的变更znode节点/serviceA/conf中的配置数据。
- 各个服务受到节点配置数据变动通知,读取新的配置数据,并重新注册watcher以监听下一次变更。
核心代码实现
代码实现
- 配置服务Zookeeper客户端核心代码。该类封装了与ZooKeeper服务端交互的基本函数。
<?php
/**
* ConfigClient.class.php
* 配置模块客户端类
*/
class ConfigClient
{
private $pZk = null;
private $strConfPath = '';
public function __construct($host)
{
//实例化ZooKeeper客户端类
$this->pZk = new Zookeeper($host);
//设置ZooKeeper日志级别
$this->pZk->setDebugLevel(Zookeeper::LOG_LEVEL_ERROR);
}
/**
* 设置节点路径
* @param String $strConfPath 节点路径
*/
public function setConfPath($strConfPath)
{
$this->strConfPath = $strConfPath;
}
/**
* 判断节点是否存在
* @return [type] [description]
*/
public function checkExist()
{
return $this->pZk->exists(