下面列出了怎么用org.apache.zookeeper.server.quorum.QuorumPeerMain的API类实例代码及写法,或者点击链接到github查看源代码。
public static String startZooQ(boolean clean) {
ZkS.zkSystemProps();
String connectionString = format("localhost:2181,localhost:2182,localhost:2183");
String [] args1 = {"./../zk/zk1/zoo.cfg"};
String [] args2 = {"./../zk/zk2/zoo.cfg"};
String [] args3 = {"./../zk/zk3/zoo.cfg"};
// Port 2180
//ZkS.runZookeeperServer("./../zk/single/zoo.cfg");
//System.out.println("Server1 ...");
async(()->QuorumPeerMain.main(args1));
//System.out.println("Server2 ...");
async(()->QuorumPeerMain.main(args2));
//System.out.println("Server3 ...");
async(()->QuorumPeerMain.main(args3));
return connectionString;
}
protected static void start(String[] args, String defaultStatsPort) throws Exception {
// Register basic JVM metrics
DefaultExports.initialize();
// Start Jetty to serve stats
int port = Integer.parseInt(System.getProperties().getProperty("stats_server_port", defaultStatsPort));
log.info("Starting ZK stats HTTP server at port {}", port);
InetSocketAddress httpEndpoint = InetSocketAddress.createUnresolved("0.0.0.0", port);
Server server = new Server(httpEndpoint);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
try {
server.start();
} catch (Exception e) {
log.error("Failed to start HTTP server at port {}. Use \"-Dstats_server_port=1234\" to change port number",
port, e);
throw e;
}
// Start the regular ZooKeeper server
QuorumPeerMain.main(args);
}
/**
* Runs a ZooKeeper {@link QuorumPeer} if further peers are configured or a single
* {@link ZooKeeperServer} if no further peers are configured.
*
* @param zkConfigFile ZooKeeper config file 'zoo.cfg'
* @param peerId ID for the 'myid' file
*/
public static void runFlinkZkQuorumPeer(String zkConfigFile, int peerId) throws Exception {
Properties zkProps = new Properties();
try (InputStream inStream = new FileInputStream(new File(zkConfigFile))) {
zkProps.load(inStream);
}
LOG.info("Configuration: " + zkProps);
// Set defaults for required properties
setRequiredProperties(zkProps);
// Write peer id to myid file
writeMyIdToDataDir(zkProps, peerId);
// The myid file needs to be written before creating the instance. Otherwise, this
// will fail.
QuorumPeerConfig conf = new QuorumPeerConfig();
conf.parseProperties(zkProps);
if (conf.isDistributed()) {
// Run quorum peer
LOG.info("Running distributed ZooKeeper quorum peer (total peers: {}).",
conf.getServers().size());
QuorumPeerMain qp = new QuorumPeerMain();
qp.runFromConfig(conf);
}
else {
// Run standalone
LOG.info("Running standalone ZooKeeper quorum peer.");
ZooKeeperServerMain zk = new ZooKeeperServerMain();
ServerConfig sc = new ServerConfig();
sc.readFrom(conf);
zk.runFromConfig(sc);
}
}
/**
* Runs a ZooKeeper {@link QuorumPeer} if further peers are configured or a single
* {@link ZooKeeperServer} if no further peers are configured.
*
* @param zkConfigFile ZooKeeper config file 'zoo.cfg'
* @param peerId ID for the 'myid' file
*/
public static void runFlinkZkQuorumPeer(String zkConfigFile, int peerId) throws Exception {
Properties zkProps = new Properties();
try (InputStream inStream = new FileInputStream(new File(zkConfigFile))) {
zkProps.load(inStream);
}
LOG.info("Configuration: " + zkProps);
// Set defaults for required properties
setRequiredProperties(zkProps);
// Write peer id to myid file
writeMyIdToDataDir(zkProps, peerId);
// The myid file needs to be written before creating the instance. Otherwise, this
// will fail.
QuorumPeerConfig conf = new QuorumPeerConfig();
conf.parseProperties(zkProps);
if (conf.isDistributed()) {
// Run quorum peer
LOG.info("Running distributed ZooKeeper quorum peer (total peers: {}).",
conf.getServers().size());
QuorumPeerMain qp = new QuorumPeerMain();
qp.runFromConfig(conf);
}
else {
// Run standalone
LOG.info("Running standalone ZooKeeper quorum peer.");
ZooKeeperServerMain zk = new ZooKeeperServerMain();
ServerConfig sc = new ServerConfig();
sc.readFrom(conf);
zk.runFromConfig(sc);
}
}
/**
* 通过官方的QuorumPeerMain启动类启动真集群模式
* 会执行quorumPeer.join();
* 需要在不同的服务器上执行
* @param zkConfig 配置对象
* @throws ConfigException 配置异常
* @throws IOException IO异常
*/
public void startCluster(ZkConfig zkConfig) throws ConfigException, IOException {
Properties zkProp = zkConfig.toProp();
QuorumPeerConfig config = new QuorumPeerConfig();
config.parseProperties(zkProp);
QuorumPeerMain main = new QuorumPeerMain();
main.runFromConfig(config);
}
/** One external zoo keeper */
public static String startZooQ_single(boolean clean) {
ZkS.zkSystemProps();
String connectionString = format("localhost:2180");
if ( clean )
FileOps.clearDirectory("./../zk/single/zk-data/version-2");
String [] args1 = {"./../zk/single/zoo.cfg"};
async(()->QuorumPeerMain.main(args1));
return connectionString;
}
@Override
public void run() {
String[] args = new String[2];
args[0] = port;
args[1] = "./zk_test_data/zkdata" + this.getId();
QuorumPeerMain.main(args);
}
@Override
public void run() {
String[] args = new String[2];
args[0] = port;
args[1] = "./zk_test_data/zkdata" + this.getId();
QuorumPeerMain.main(args);
}
@Override
public void run() {
String[] args = new String[2];
args[0] = port;
args[1] = "./zk_test_data/zkdata" + this.getId();
QuorumPeerMain.main(args);
}
private static void runZKServer(QuorumPeerConfig zkConfig)
throws IOException, AdminServer.AdminServerException {
if (zkConfig.isDistributed()) {
QuorumPeerMain qp = new QuorumPeerMain();
qp.runFromConfig(zkConfig);
} else {
ZooKeeperServerMain zk = new ZooKeeperServerMain();
ServerConfig serverConfig = new ServerConfig();
serverConfig.readFrom(zkConfig);
zk.runFromConfig(serverConfig);
}
}
public void start() {
if (zkRun == null) return;
if (System.getProperty(ZK_WHITELIST_PROPERTY) == null) {
System.setProperty(ZK_WHITELIST_PROPERTY, "ruok, mntr, conf");
}
zkThread = new Thread() {
@Override
public void run() {
try {
if (zkProps.getServers().size() > 1) {
QuorumPeerMain zkServer = new QuorumPeerMain();
zkServer.runFromConfig(zkProps);
} else {
ServerConfig sc = new ServerConfig();
sc.readFrom(zkProps);
ZooKeeperServerMain zkServer = new ZooKeeperServerMain();
zkServer.runFromConfig(sc);
}
log.info("ZooKeeper Server exited.");
} catch (Exception e) {
log.error("ZooKeeper Server ERROR", e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
}
}
};
if (zkProps.getServers().size() > 1) {
if (log.isInfoEnabled()) {
log.info("STARTING EMBEDDED ENSEMBLE ZOOKEEPER SERVER at port {}", zkProps.getClientPortAddress().getPort());
}
} else {
if (log.isInfoEnabled()) {
log.info("STARTING EMBEDDED STANDALONE ZOOKEEPER SERVER at port {}", zkProps.getClientPortAddress().getPort());
}
}
log.warn("Embedded Zookeeper is not recommended in production environments. See Reference Guide for details.");
zkThread.setDaemon(true);
zkThread.start();
try {
Thread.sleep(500); // pause for ZooKeeper to start
} catch (Exception e) {
log.error("STARTING ZOOKEEPER", e);
}
}
public static void quorumServer(String confFile) {
// No join.
async(() -> QuorumPeerMain.main(new String[] {confFile}) );
}