Created
May 24, 2018 12:21
-
-
Save izebit/1f267294823bd6f294a764830a04cba1 to your computer and use it in GitHub Desktop.
zookeeper example - client and server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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