ZooKeeper节点的类型分为以下几类:
1. 持久节点:节点创建后就一直存在,直到有删除操作来主动删除该节点
2. 临时节点:临时节点的生命周期和创建该节点的客户端会话绑定,即如果客户端会话失效(客户端宕机或下线),这个节点自动删除
3. 时序节点:创建节点是可以设置这个属性,ZooKeeper会自动为给定的节点加上一个数字后缀,作为新的节点名。数字后缀的范围是整型的最大值
4. 临时性时序节点:同时具备临时节点与时序节点的特性,主要用于分布式锁的实现
配置中心使用ZooKeeper的持久节点的特性,将配置信息写入到持久节点。客户端启动时,从ZooKeeper读取配置信息,进而初始化内部资源,达到配置统一管理的目的。再结合ZooKeeper的Watch特性,配置信息变化实时推送到客户端,即时生效,无需重启客户端,达到配置热更新的效果。
基于ZooKeeper实现简易配置中心的实例代码如下:
往服务端写入配置,代码如下:
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查询配置,代码如下:
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;
}
以上就是简单的配置中心实现样例。但是还存在以下改进空间:
1.没有对数据变化进行监听,需要在数据变化时实时获取新的配置。(注:Zookeeper的API中的watch只能消费一次,需循环设置监听)
2.可以使用ZKClient或者Curator等ZooKeeper客户端工具,解决了非常底层的细节开发工作,包括连接重连、反复注册Watcher和NodeExistsException异常等
欢迎关注我的微信公众号(Sunnick,请扫码关注),随时留言交流~