您的位置:首页 > 其它

diamond源码阅读-diamond-server

2016-10-26 18:14 302 查看
diamond-server

1 增加一条数据 /diamond-server/admin.do?method=postConfig

1.1 调用 this.configService.addConfigInfo(dataId, group, content);

public void addConfigInfo(String dataId, String group, String content) {
ConfigInfo configInfo = new ConfigInfo(dataId, group, content);//md5加密content
// 保存顺序:先数据库,再磁盘
try {
checkParameter(dataId, group, content);//参数校验
persistService.addConfigInfo(configInfo);//插入数据库
// 切记更新缓存
this.contentMD5Cache.put(generateMD5CacheKey(dataId, group), configInfo.getMd5());// key = group + "/" + dataId value=MD5
diskService.saveToDisk(configInfo);//保存到文件
// 通知其他节点() HTTP 方式通知
this.notifyOtherNodes(dataId, group);//http://ip:port/diamond-server/notify.do?method=notifyConfigInfo&dataId=" + dataId + "&group=" + group
}
catch (Exception e) {
log.error("保存ConfigInfo失败", e);
throw new ConfigServiceException(e);
}
}

1.2 其他节点接收到信息后

this.configService.loadConfigInfoToDisk(dataId, group);

public void loadConfigInfoToDisk(String dataId, String group) {
try {
ConfigInfo configInfo = this.persistService.findConfigInfo(dataId, group);//数据库读取配置
if (configInfo != null) {
this.contentMD5Cache.put(generateMD5CacheKey(dataId, group), configInfo.getMd5());//保存到内存 key = group + "/" + dataId value=MD5
this.diskService.saveToDisk(configInfo);//保存到文件
}
else {
// 删除文件
this.contentMD5Cache.remove(generateMD5CacheKey(dataId, group));
this.diskService.removeConfigInfo(dataId, group);
}
}
catch (Exception e) {
log.error("保存ConfigInfo到磁盘失败", e);
throw new ConfigServiceException(e);
}
}

1.3 删除同样是修改数据库,修改内存,修改本地文件,通知其他节点

2 定时任务TimerTaskService DumpConfigInfoTask

如果通过http请求并没有通知到其他实例,通过定时任务保证从数据库读取信息,更新缓存信息和写入硬盘文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: