我现在有一个SpringMvc的web项目。我现在的需求是监听zookeeper的节点变化。我需要怎么实现呢???
你可以在配置文件里直接注册zk啊,然后在程序里面获取连接。
我的这个是dubbo和zk,配置
<dubbo:application name="${dubbo.service.name}" owner="Telematcis" organization="adt"/>
<!-- 使用zookeeper注册中心 -->
<dubbo:registry protocol="zookeeper" address="${zk.server}" timeout="30000"/>
<!--uncomment this if you want to test dubbo's monitor-->
<!--<dubbo:monitor protocol="registry"/>-->
<!-- 使用dubbo协议 -->
<!-- optimizer="com.adt.front.yang.rpc.protocol.SerializationOptimizerImpl"
extension="com.adt.framework.monitor.cat.dubbospi.DubboCatCrossFilter"
/>-->
<!-- 设定dubbo默认参数 -->
<dubbo:provider timeout="50000" retries="1" />
<dubbo:consumer timeout="50000" retries="1" />
这里配置监听的RPC暴露的接口
<dubbo:reference id="customerService"
interface="com.adt.service.yang.rpc.CustomerService" check="false" version="0.0.0.1"/>
public class HelloZK
{
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(HelloZK.class);
private static final String CONNECTSTRING = "192.168.67.167:2181";
private static final String PATH = "/qqqq";
private static final int SESSION_TIMEOUT = 50*1000;
public ZooKeeper startZK() throws IOException
{
return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent event)
{
}
});
}
public void stopZK(ZooKeeper zk) throws InterruptedException
{
if(zk != null)
{
zk.close();
}
}
public void createZNode(ZooKeeper zk,String path,String nodeValue) throws KeeperException, InterruptedException
{
zk.create(path,nodeValue.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
}
public String getZNode(ZooKeeper zk,String path) throws KeeperException, InterruptedException
{
byte[] byteArray = zk.getData(path, false, new Stat());
return new String(byteArray);
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException
{
HelloZK hello = new HelloZK();
ZooKeeper zk = hello.startZK();
Stat stat = zk.exists(PATH, false);
if(stat == null)
{
hello.createZNode(zk, PATH, "zk1014");
String result = hello.getZNode(zk, PATH);
System.out.println("**********result: "+result);
}else{
System.out.println("***********znode has already ok***********");
}
hello.stopZK(zk);
}
}
估计很难实现,没用过,不好意思
回复繁华穿越现实: 一个demo,java客户端操作zk的