Skip to content

Instantly share code, notes, and snippets.

@Arbow
Created September 26, 2012 10:26
Show Gist options
  • Save Arbow/3787229 to your computer and use it in GitHub Desktop.
Save Arbow/3787229 to your computer and use it in GitHub Desktop.
Start zookeeper cluster from program
private static void startClusterServer(String name, int id, int port) {
String dataDir = "./zk/" + name;
File dataDirectory = new File(dataDir);
if (!dataDirectory.exists())
dataDirectory.mkdirs();
File myIdFile = new File(dataDirectory, "myid");
if (!myIdFile.exists()) {
IO.write(String.valueOf(id).getBytes(), myIdFile);
}
Properties prop = new Properties();
prop.put("tickTime", 2000);
prop.put("initLimit", 10);
prop.put("syncLimit", 5);
prop.put("dataDir", dataDir);
prop.put("dataLogDir", dataDir);
prop.put("clientPort", port);
prop.put("server.1", "localhost:2888:3888");
prop.put("server.2", "localhost:2889:3889");
try {
QuorumPeerConfig config = new QuorumPeerConfig();
config.parseProperties(prop);
QuorumPeerMain main = new QuorumPeerMain();
main.runFromConfig(config);
} catch(Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment