Zookeeper笔记之基于zk的分布式配置中心
一、场景 & 需求
集群上有很多个节点运行同一个任务,这个任务会有一些可能经常改变的配置参数,要求是当配置参数改变之后能够很快地同步到每个节点上,如果将这些配置参数放在本地文件中则每次都要修改本地文件费时费力还可能会有遗漏,所以这个时候一个比较自然的想法就是将配置单独提取出来作为一个服务,比如自己开发一个http服务器提供一个接口来获取服务,这有两个问题,其一是配置下发这其实是一个推模型,当配置发生改变时需要服务器去主动推给客户端而不是客户端不断地去轮询,其二是配置中心不能是单点故障,对配置中心的可用性有一定要求,这时候如果有zookeeper集群的话直接拿来作为配置中心使用不失为一种简单的方案。
二、分析
一个配置中心的核心是什么:
1. 低延迟:配置改变后能够尽快的将最新配置同步给每一个节点。
2. 高可用:配置中心需要能够稳定不间断的提供服务。
第一点可以通过zookeeper的watcher机制实现,约定一个节点用来存放配置信息,每个客户端都监听这个节点的NodeDataChanged事件,当配置发生改变时将最新的配置更新到这个节点上(谁更新无所谓,任意一个节点都可以更新,或者做一个另外的配置管理后台用来更新都没有问题),这个节点触发NodeDataChanged事件,通知所有监听此节点NodeDataChanged事件的客户端获取此节点的最新值,因为watcher是一次性的,所以在获取最新值的时候需要重新设置监听事件,因为getData是原子性操作,所以能够保证获取到的一定是最新的值。这里需要注意的是存放在节点上的配置文件不宜过大,如果配置文件部分很大而每次变更的只是一部分的话或许可以考虑对其进行拆分,存放在多个节点上。
第二点的高可用性就是交由zookeeper集群来保证,在应用层面不需要做额外的工作。
下面是分布式配置管理中心简单的示意图:
程序运行过程中不断的重复123步骤。
三、代码实现
ConfigManager.java:
package cc11001100.zookeeper.configManager; import cc11001100.zookeeper.utils.ZooKeeperUtil;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper; import java.io.IOException; /**
* 使用zookeeper做分布式配置管理的例子
*
* @author CC11001100
*/
public class ConfigManager { private String configNode;
private ZooKeeper zooKeeper; public ConfigManager(String configNode) throws IOException {
this.configNode = configNode;
zooKeeper = ZooKeeperUtil.getZooKeeper();
registerConfigChangeListener();
} private void registerConfigChangeListener() {
try {
byte[] newConfig = zooKeeper.getData(configNode, event -> registerConfigChangeListener(), null);
if (newConfig != null) {
handleNewConfig(new String(newConfig, "UTF-8"));
}
} catch (IOException | InterruptedException | KeeperException e) {
e.printStackTrace();
}
} protected void handleNewConfig(String newConfig) {
System.out.println(Thread.currentThread().getName() + " config changed: " + newConfig);
} public void updateConfig(String newConfig) {
try {
zooKeeper.setData(configNode, newConfig.getBytes(), -1);
} catch (InterruptedException | KeeperException e) {
e.printStackTrace();
}
} }
ConfigManagerTest.java:
package cc11001100.zookeeper.configManager; import cc11001100.zookeeper.utils.ZooKeeperUtil;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper; import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit; /**
* 测试更新
*
* @author CC11001100
*/
public class ConfigManagerTest { private static void sleep(int mils) {
try {
TimeUnit.MILLISECONDS.sleep(mils);
} catch (InterruptedException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException, KeeperException, InterruptedException { final String CONFIG_NODE = "/config-node";
ZooKeeper zooKeeper = ZooKeeperUtil.getZooKeeper();
if (zooKeeper.exists(CONFIG_NODE, false) == null) {
zooKeeper.create(CONFIG_NODE, "default-config".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} // 模拟有10个客户端,每个都有一定几率更新配置
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
ConfigManager configManager = new ConfigManager(CONFIG_NODE);
Random random = new Random();
sleep(random.nextInt(3000));
while (true) {
if (Math.random() < 0.2) {
// 节点可以自己更新配置
String newConfig = Thread.currentThread().getName() + " update, new config " + random.nextLong();
configManager.updateConfig(newConfig);
}
sleep(3000);
}
} catch (IOException e) {
e.printStackTrace();
}
}, "client-" + i).start();
} // 也可以有专门的配置中心来管理更新配置
Random random = new Random();
while (true) {
if (Math.random() < 0.2) {
String newConfig = "config master update, new config " + random.nextLong();
zooKeeper.setData(CONFIG_NODE, newConfig.getBytes(), -1);
}
sleep(1000);
} } }
控制台输出:
...
client-4 config changed: client-5 update, new config -845834592719987179
client-5 config changed: client-5 update, new config -845834592719987179
client-3 config changed: client-5 update, new config -845834592719987179
client-6 config changed: client-5 update, new config -845834592719987179
client-1 config changed: client-5 update, new config -845834592719987179
client-7 config changed: client-5 update, new config -845834592719987179
client-2 config changed: client-5 update, new config -845834592719987179
client-8 config changed: client-5 update, new config -845834592719987179
client-9 config changed: client-5 update, new config -845834592719987179
client-0 config changed: client-5 update, new config -845834592719987179
client-1-EventThread config changed: config master update, new config -1193927892495196391
client-2-EventThread config changed: config master update, new config -1193927892495196391
client-6-EventThread config changed: config master update, new config -1193927892495196391
client-9-EventThread config changed: config master update, new config -1193927892495196391
client-8-EventThread config changed: config master update, new config -1193927892495196391
client-3-EventThread config changed: config master update, new config -1193927892495196391
client-4-EventThread config changed: config master update, new config -1193927892495196391
client-7-EventThread config changed: config master update, new config -1193927892495196391
client-5-EventThread config changed: config master update, new config -1193927892495196391
client-0-EventThread config changed: config master update, new config -1193927892495196391
client-1-EventThread config changed: client-9 update, new config -3172102246096982581
client-6-EventThread config changed: client-9 update, new config -3172102246096982581
client-0-EventThread config changed: client-9 update, new config -3172102246096982581
client-2-EventThread config changed: client-9 update, new config -3172102246096982581
client-4-EventThread config changed: client-9 update, new config -3172102246096982581
client-7-EventThread config changed: client-9 update, new config -3172102246096982581
client-8-EventThread config changed: client-9 update, new config -3172102246096982581
client-9-EventThread config changed: client-9 update, new config -3172102246096982581
client-5-EventThread config changed: client-9 update, new config -3172102246096982581
client-3-EventThread config changed: client-9 update, new config -3172102246096982581
client-8-EventThread config changed: config master update, new config -8802002496608532059
client-2-EventThread config changed: config master update, new config -8802002496608532059
client-4-EventThread config changed: config master update, new config -8802002496608532059
client-1-EventThread config changed: config master update, new config -8802002496608532059
client-6-EventThread config changed: config master update, new config -8802002496608532059
client-7-EventThread config changed: config master update, new config -8802002496608532059
client-0-EventThread config changed: config master update, new config -8802002496608532059
client-5-EventThread config changed: config master update, new config -8802002496608532059
client-9-EventThread config changed: config master update, new config -8802002496608532059
client-3-EventThread config changed: config master update, new config -8802002496608532059
client-4-EventThread config changed: client-2 update, new config -4848584377488801943
client-1-EventThread config changed: client-2 update, new config -4848584377488801943
client-6-EventThread config changed: client-2 update, new config -4848584377488801943
client-2-EventThread config changed: client-2 update, new config -4848584377488801943
client-8-EventThread config changed: client-2 update, new config -4848584377488801943
client-7-EventThread config changed: client-2 update, new config -4848584377488801943
client-5-EventThread config changed: client-2 update, new config -4848584377488801943
client-9-EventThread config changed: client-2 update, new config -4848584377488801943
client-3-EventThread config changed: client-2 update, new config -4848584377488801943
client-0-EventThread config changed: client-2 update, new config -4848584377488801943
client-6-EventThread config changed: client-9 update, new config 6591542797374556406
client-4-EventThread config changed: client-9 update, new config 6591542797374556406
client-3-EventThread config changed: client-9 update, new config 6591542797374556406
client-9-EventThread config changed: client-9 update, new config 6591542797374556406
client-1-EventThread config changed: client-9 update, new config 6591542797374556406
client-2-EventThread config changed: client-9 update, new config 6591542797374556406
client-8-EventThread config changed: client-9 update, new config 6591542797374556406
client-7-EventThread config changed: client-9 update, new config 6591542797374556406
client-0-EventThread config changed: client-9 update, new config 6591542797374556406
client-5-EventThread config changed: client-9 update, new config 6591542797374556406
client-6-EventThread config changed: client-8 update, new config -6225415529835558983
client-4-EventThread config changed: client-8 update, new config -6225415529835558983
client-8-EventThread config changed: client-8 update, new config -6225415529835558983
client-2-EventThread config changed: client-8 update, new config -6225415529835558983
client-1-EventThread config changed: client-8 update, new config -6225415529835558983
client-5-EventThread config changed: client-8 update, new config -6225415529835558983
client-0-EventThread config changed: client-8 update, new config -6225415529835558983
client-7-EventThread config changed: client-8 update, new config -6225415529835558983
client-9-EventThread config changed: client-8 update, new config -6225415529835558983
client-3-EventThread config changed: client-8 update, new config -6225415529835558983
client-1-EventThread config changed: client-4 update, new config -703954571020619435
client-4-EventThread config changed: client-4 update, new config -703954571020619435
client-8-EventThread config changed: client-4 update, new config -703954571020619435
client-6-EventThread config changed: client-4 update, new config -703954571020619435
client-2-EventThread config changed: client-4 update, new config -703954571020619435
client-7-EventThread config changed: client-4 update, new config -703954571020619435
client-0-EventThread config changed: client-4 update, new config -703954571020619435
client-5-EventThread config changed: client-4 update, new config -703954571020619435
client-3-EventThread config changed: client-4 update, new config -703954571020619435
client-9-EventThread config changed: client-4 update, new config -703954571020619435
...
四、总结
使用zk作为配置分发的优点是低延迟、高可靠性,当然也有缺点,因为watcher是跟会话绑定的,而要维护每个会话需要一个tcp一直连接到服务器,这对集群来说也是一种负载,不过考虑到2c4g单台机器支持几百连接并发很轻松,再加上整个zookeeper集群中会有多台机器平均一下,这点负载基本忽略了。
相关资料:
1. 一篇好TM长的关于配置中心的文章 - 阿里中间件团队博客(废话有点多,但很值得一看)
.
Zookeeper笔记之基于zk的分布式配置中心的更多相关文章
- zookeeper笔记之基于zk实现分布式锁
一.分布式锁概述 Java中基于AQS框架提供了一系列的锁,但是当需要在集群中的多台机器上互斥执行一段代码或使用资源时Java提供的这种单机锁就没了用武之地,此时需要使用分布式锁协调它们.分布式锁有很 ...
- 基于ZK构建统一配置中心的方案和实践
背景: 近期使用Zk实现了一个简单的配置管理的小东西,在此开源出来,有兴趣的希望提出您的宝贵意见.如果恰巧您也使用或者接触过类似的东西, 也希望您可以分享下您觉得现在这个项目可以优化和改进的地方. 项 ...
- 基于zookeeper实现分布式配置中心(一)
最近在学习zookeeper,发现zk真的是一个优秀的中间件.在分布式环境下,可以高效解决数据管理问题.在学习的过程中,要深入zk的工作原理,并根据其特性做一些简单的分布式环境下数据管理工具.本文首先 ...
- 基于zookeeper实现分布式配置中心(二)
上一篇(基于zookeeper实现分布式配置中心(一))讲述了zookeeper相关概念和工作原理.接下来根据zookeeper的特性,简单实现一个分布式配置中心. 配置中心的优势 1.各环境配置集中 ...
- spring cloud 入门系列七:基于Git存储的分布式配置中心
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...
- 基于winserver的Apollo配置中心分布式&集群部署实践(正确部署姿势)
基于winserver的Apollo配置中心分布式&集群部署实践(正确部署姿势) 前言 前几天对Apollo配置中心的demo进行一个部署试用,现公司已决定使用,这两天进行分布式部署的时候 ...
- spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...
- SpringCloud学习笔记(九):SpringCloud Config 分布式配置中心
概述 分布式系统面临的-配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...
- SpringCloud搭建分布式配置中心(基于git)
1.简介 Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用 ...
随机推荐
- 20135202闫佳歆--week6 分析Linux内核创建一个新进程的过程——实验及总结
week 6 实验:分析Linux内核创建一个新进程的过程 1.使用gdb跟踪创建新进程的过程 准备工作: rm menu -rf git clone https://github.com/mengn ...
- 第二阶段冲刺——five
个人任务: 王金萱:合并程序(统计团队博客). 季方:合并并排除运行团队博客时出现的错误. 马佳慧:优化登录.注册信息的填写判断. 司宇航:完成打印名单的功能. 站立会议: 任务看板和燃尽图:
- 基于SSH框架的网上书店系统开发的质量属性
基于SSH框架的网上书店系统开发的质量属性 对于我的基于SSH框架的网上书店系统的开发要实现的质量属性有可用性.可修改性.性能.安全性.易用性和可测试性. 1.对于可用性方面的战术: 可用性(Avai ...
- Beta 冲刺 二
团队成员 051601135 岳冠宇 031602629 刘意晗 031602248 郑智文 031602330 苏芳锃 031602234 王淇 照片 项目进展 岳冠宇 昨天的困难 fragment ...
- JSON中JObject和JArray的修改
一.JObject 和JArray的添加.修改.移除 1.先添加一个json字符串,把json字符串加载到JObject中,然后转换成JObject.根据索引修改对象的属性值,移除属性,添加属性 us ...
- BZOJ2299 HAOI2011向量(数论)
设最后的组成为x=x0a+x1b,y=y0a+y1b.那么容易发现x0和y0奇偶性相同.x1和y1奇偶性相同.于是考虑奇偶两种情况,问题就变为是否存在x和y使ax+by=c,那么其充要条件是gcd(a ...
- 【刷题】BZOJ 3522 [Poi2014]Hotel
Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了 ...
- Linux下修改tomcat内存
由于服务器上放的tomcat太多,造成内存溢出. 常见的内存溢出有以下两种: java.lang.OutOfMemoryError: PermGen space java.lang.OutOfMemo ...
- NetBeans无法使用svn相关功能
本来中NetBeans使用subversion功能只要下载过subversion客户端就可以直接使用了,我也一直是这样的,可是今天忽然的不能用了,发现问题是因为我在文件中使用subversion的比较 ...
- 【BZOJ1303】[CQOI2009]中位数图(模拟)
[BZOJ1303][CQOI2009]中位数图(模拟) 题面 BZOJ 洛谷 题解 把大于\(b\)的数设为\(1\),小于\(b\)的数设为\(-1\).显然询问就是有多少个横跨了\(b\)这个数 ...