很多使用Zookeeper的情景是需要我们嵌入Zookeeper作为自己的分布式应用系统的一部分来提供分布式服务,此时我们需要通过程序的方式来启动Zookeeper。此时可以通过Zookeeper API的ZooKeeperServerMain类来启动Zookeeper服务。

下面是一个单机模式下启动Zookeeper服务的例子

package my.zookeeperstudy.server;
 
import org.apache.zookeeper.*;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
 
public class StandaloneZKServer {
 
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("tickTime", "2000");
        props.setProperty("dataDir", new File(System.getProperty("java.io.tmpdir"), "zookeeper").getAbsolutePath());
        props.setProperty("clientPort", "2181");
        props.setProperty("initLimit", "10");
        props.setProperty("syncLimit", "5");
 
        QuorumPeerConfig quorumConfig = new QuorumPeerConfig();
        try {
            quorumConfig.parseProperties(props);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
 
        final ZooKeeperServerMain zkServer = new ZooKeeperServerMain();
        final ServerConfig config = new ServerConfig();
        config.readFrom(quorumConfig);
        zkServer.runFromConfig(config);
    }
 
}

客户端例子如下

package my.zookeeperstudy.server;
 
import org.apache.zookeeper.*;
import java.util.List;
 
public class Client {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("localhost:2181", 10000,
                new Watcher() {
                    public void process(WatchedEvent event) {
                        System.out.println("event: " + event.getType());
                    }
                });
 
        System.out.println(zk.getState());
 
        zk.create("/myApps", "myAppsData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.create("/myApps/App1", "App1Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.create("/myApps/App2", "App2Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.create("/myApps/App3", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.setData("/myApps/App3","App3Data".getBytes(), -1);
 
        System.out.println(zk.exists("/myApps", true));
        System.out.println(new String(zk.getData("/myApps", true, null)));
 
        List<String> children = zk.getChildren("/myApps", true);
        for (String child : children) {
            System.out.println(new String(zk.getData("/myApps/" + child, true, null)));
            zk.delete("/myApps/" + child,-1);
        }
 
        zk.delete("/myApps",-1);
 
        zk.close();
    }
}


测试
首先启动StandaloneZKServer类来启动Zookeeper服务,然后运行Client类来连接Zookeeper并操作数据。


原文链接:Zookeeper实战之嵌入式运行Zookeeper单机模式

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐