凯发k8官方网
收集整理的这篇文章主要介绍了
zookeeper配置中心
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
场景
多个客户端从zookeeper 的配置中心拿到配置,如果配置中心没有配置就阻塞,如果配置修改了就拿到新的。
原理
因为zookeeper的同步性质,即单线程的分两段式(当发生修改时,第一阶段:leader向各个follower发送log任务,过半成功返回后进行第二段的具体的修改)的事务方式,如果节点发生更改,则要么成功且所有客户端都get到最新的修改结果,要么失败。所以代码只要注册发生修改(包括数据修改,节点增加,删除等,以下同)的事件,死循环取就行。本文只写了简单的框架,复杂的业务逻辑没有。用zookeeper模拟另外的客户端做修改的操作。
代码
目录结构
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.zookeeper
;
import org
.junit
.after
;
import org
.junit
.before
;
import org
.junit
.test
;
public class main {zookeeper zk
;@beforepublic void before() {zk
= zkutils
.getzk();}@afterpublic void after() {try {zk
.close();} catch (interruptedexception e
) {e
.printstacktrace();}}@testpublic void getconf() {watchcallback w
= new watchcallback();myconf conf
= new myconf();w
.setconf(conf
);w
.setzk(zk
);w
.await();while (true) {if (conf
.getconfstr().equals("")) {system
.out
.println("配置丢了 ....");w
.await();} else {system
.out
.println("conf: " conf
.getconfstr());}}}
}
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.watchedevent
;
import org
.apache
.zookeeper
.watcher
;import java
.util
.concurrent
.countdownlatch
;
public class defaultwatcher implements watcher {countdownlatch c
;public void setc(countdownlatch c
) {this.c
= c
;}@overridepublic void process(watchedevent e
) {event
.eventtype type
= e
.gettype();event
.keeperstate state
= e
.getstate();string path
= e
.getpath();switch (type
) {case none
:break;case nodecreated
:break;case nodedeleted
:break;case nodedatachanged
:break;case nodechildrenchanged
:break;}switch (state
) {case unknown
:break;case disconnected
:break;case nosyncconnected
:break;case syncconnected
:c
.countdown();break;case authfailed
:break;case connectedreadonly
:break;case saslauthenticated
:break;case expired
:break;}}
}
package org
.faithgreen
.conf
;
public class myconf {private string confstr
;public string
getconfstr() {return confstr
;}public void setconfstr(string confstr
) {this.confstr
= confstr
;}
}
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.asynccallback
;
import org
.apache
.zookeeper
.watchedevent
;
import org
.apache
.zookeeper
.watcher
;
import org
.apache
.zookeeper
.zookeeper
;
import org
.apache
.zookeeper
.data
.stat
;import java
.util
.concurrent
.countdownlatch
;
public class watchcallback implements watcher, asynccallback
.datacallback
, asynccallback
.statcallback
{zookeeper zk
;countdownlatch cc
= new countdownlatch(1);myconf conf
;public zookeeper
getzk() {return zk
;}public void setzk(zookeeper zk
) {this.zk
= zk
;}public countdownlatch
getcc() {return cc
;}public void setcc(countdownlatch cc
) {this.cc
= cc
;}public myconf
getconf() {return conf
;}public void setconf(myconf conf
) {this.conf
= conf
;}public void await() {zk
.exists("/appconf", this, this, "abc");try {cc
.await();} catch (interruptedexception e
) {e
.printstacktrace();}}@overridepublic void process(watchedevent e
) {string path
= e
.getpath();event
.eventtype type
= e
.gettype();event
.keeperstate state
= e
.getstate();switch (type
) {case none
:break;case nodecreated
:zk
.getdata("/appconf", this, this, "def");break;case nodedeleted
:conf
.setconfstr("");cc
= new countdownlatch(1);break;case nodedatachanged
:zk
.getdata("/appconf", this, this, "def");break;case nodechildrenchanged
:break;}}@overridepublic void processresult(int i
, string s
, object o
, byte[] bytes
, stat stat
) {if (bytes
!= null
) {string s1
= new string(bytes
);conf
.setconfstr(s1
);cc
.countdown();}}@overridepublic void processresult(int i
, string s
, object o
, stat stat
) {if (stat
!= null
) {zk
.getdata("/appconf", this, this, "def");}}
}
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.zookeeper
;import java
.util
.concurrent
.countdownlatch
;public class zkutils {static zookeeper zk
;final static string address
= "192.168.172.3:2181,192.168.172.4:2181,192.168.172.5:2181,192.168.172.6:2181/testconf";final static defaultwatcher defaultwatcher
= new defaultwatcher();static countdownlatch c
= new countdownlatch(1);public static zookeeper
getzk() {try {zk
= new zookeeper(address
, 4000, defaultwatcher
);defaultwatcher
.setc(c
);c
.await();} catch (exception e
) {e
.printstacktrace();}return zk
;}
}
总结
以上是凯发k8官方网为你收集整理的zookeeper配置中心的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。