ZooKeeper节点的类型分为如下几类:程序员
持久节点:节点建立后就一直存在,直到有删除操做来主动删除该节点json
临时节点:临时节点的生命周期和建立该节点的客户端会话绑定,即若是客户端会话失效(客户端宕机或下线),这个节点自动删除bash
时序节点:建立节点是能够设置这个属性,ZooKeeper会自动为给定的节点加上一个数字后缀,做为新的节点名。数字后缀的范围是整型的最大值分布式
临时性时序节点:同时具有临时节点与时序节点的特性,主要用于分布式锁的实现工具
配置中心使用ZooKeeper的持久节点的特性,将配置信息写入到持久节点。客户端启动时,从ZooKeeper读取配置信息,进而初始化内部资源,达到配置统一管理的目的。再结合ZooKeeper的Watch特性,配置信息变化实时推送到客户端,即时生效,无需重启客户端,达到配置热更新的效果。ui
基于ZooKeeper实现简易配置中心的示例以下:this
往服务端写入配置,代码以下:spa
public static void main(String[] strings){
Map<String,String> env = new HashMap<String,String>();
env.put("DB","222.111.188.187");
env.put("SERVICE","222.111.188.188");
env.put("REDIS","222.111.188.186");
String json = JSON.toJSONString(env);
new ConfigServer("localhost",2181).initConfig(json.getBytes());
}
private void initConfig(byte[] data) {
ZooKeeper zooKeeper = null;
try {
zooKeeper = new ZooKeeper(this.ip + ":" + this.port,
30000, new Watcher() {
// 监控全部被触发的事件
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!" + event.getPath());
}
});
//写入配置
Stat stat = zooKeeper.exists(EVN_PATH,false);
if (stat != null){
//获取配置
data = zooKeeper.getData(EVN_PATH,true,null);
System.out.println("原有配置:" + new String(data));
zooKeeper.setData(EVN_PATH,data,-1);
}else{
zooKeeper.create(EVN_PATH,data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
System.out.println("写入配置:" + new String(data));
} catch (Exception e) {
e.printStackTrace();
if (zooKeeper != null){
try {
zooKeeper.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
复制代码
客户端从ZooKeeper查询配置,代码以下:code
public static void main(String[] strings){
ConfigClient client = new ConfigClient("localhost",2181);
byte[] config = client.getConfig();
if (config == null || config.length < 1){
System.out.println("获取配置失败");
}
String configStr = new String(config);
System.out.println("配置为:" + configStr);
while(isRunning){
try {
synchronized (client) {
client.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
private byte[] getConfig() {
ZooKeeper zooKeeper = null;
byte[] data = null;
try {
zooKeeper = new ZooKeeper(this.ip + ":" + this.port,
30000, new Watcher() {
// 监控全部被触发的事件
// Watch 是一次性的,若是 watch 事件发生了,还想 watch 须要再设置新的watch
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!" + event.getPath()
+ "," + event.getState());
}
});
//获取配置
data = zooKeeper.getData(EVN_PATH,true,null);
} catch (Exception e) {
e.printStackTrace();
if (zooKeeper != null){
try {
zooKeeper.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
return data;
}
复制代码
以上就是简单的配置中心实现样例。可是还存在如下改进空间:cdn
1.没有对数据变化进行监听,须要在数据变化时实时获取新的配置。(注:Zookeeper的API中的watch只能消费一次,需循环设置监听)
2.能够使用ZKClient或者Curator等ZooKeeper客户端工具,解决了很是底层的细节开发工做,包括链接重连、反复注册Watcher和NodeExistsException异常等
若是各位有好的想法,欢迎关注个人公众号(程序员顺仔)留言讨论~