Skip to content

Instantly share code, notes, and snippets.

@izebit
Created May 24, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izebit/1f267294823bd6f294a764830a04cba1 to your computer and use it in GitHub Desktop.
Save izebit/1f267294823bd6f294a764830a04cba1 to your computer and use it in GitHub Desktop.
zookeeper example - client and server
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.3-beta</version>
</dependency>
package ru.izebit;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.server.NIOServerCnxnFactory;
import org.apache.zookeeper.server.ServerCnxnFactory;
import org.apache.zookeeper.server.ZooKeeperServer;
import java.io.File;
import java.net.InetSocketAddress;
/**
* @author <a href="izebit@gmail.com">Artem Konovalov</a> <br/>
* Date: 27/03/2018/.
*/
public class ZookeeperExample {
public static void main(String[] args) throws Exception {
ZooKeeperServer server = zookeeper();
ZkClient client1 = new ZkClient("127.0.0.1:2199");
ZkClient client2 = new ZkClient("127.0.0.1:2199");
client1.createEphemeral("/test");
client2.createEphemeral("/test/example");
System.out.println("shutdown");
server.shutdown(true);
System.exit(0);
}
private static ZooKeeperServer zookeeper() throws Exception {
int clientPort = 2199; // not standard
int numConnections = 5000;
int tickTime = 2000;
String dataDirectory = System.getProperty("java.io.tmpdir");
File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
ServerCnxnFactory factory = new NIOServerCnxnFactory();
factory.configure(new InetSocketAddress(clientPort), numConnections);
factory.startup(server);
return server;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment