private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException { 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 static void main(String[] args) throws Exception { if (args == null || args.length != 1) { throw new IllegalArgumentException(""); } int i = Integer.valueOf(args[0]); QuorumPeerMain.main(new String[]{SingleServerTest.class.getResource("/zoo" + i + ".cfg").getPath()}); System.in.read(); }
protected static void start(String[] args, String defaultStatsPort) throws Exception { // Register basic JVM metrics DefaultExports.initialize(); // load aspectj-weaver agent for instrumentation AgentLoader.loadAgentClass(Agent.class.getName(), null); // 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); }
private void startZookeeper() { startInNewThread(() -> { try { String zookeeperConfig = TestServerManager.class.getClassLoader().getResource("zookeeper.properties").getPath(); logger.debug("Starting Zookeeper server using config:" + zookeeperConfig); QuorumPeerMain.main(new String[]{zookeeperConfig}); } catch (RuntimeException ex) { logger.error("Failed to start zookeeper", ex); throw ex; } }, "Zookeeper"); logger.debug("Waiting until zookeper is started"); new ZkClient("localhost:2181").waitUntilConnected(); logger.debug("Zookeeper started"); }
/** * 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); } }
public void start() { if (zkRun == null) return; 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) { log.info("STARTING EMBEDDED ENSEMBLE ZOOKEEPER SERVER at port " + zkProps.getClientPortAddress().getPort()); } else { log.info("STARTING EMBEDDED STANDALONE ZOOKEEPER SERVER at port " + zkProps.getClientPortAddress().getPort()); } 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 main(String[] args) { org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger(); rootLogger.setLevel(Level.INFO); // Define log pattern layout PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd hh:mm:ss}:%p %t %c{n} - %m%n"); // Add console appender to root logger rootLogger.addAppender(new ConsoleAppender(layout)); QuorumPeerMain.main(args); }
/** * Start embed zookeeper server in a daemon thread. */ public static int startEmbedZooKeeper() throws IOException { String zooKeeperWorkingDir = getZooKeeperWorkingDir(); boolean isSuccessful = createFolder(zooKeeperWorkingDir); if(!isSuccessful) { zooKeeperWorkingDir = getZooKeeperWorkingDir("zookeeper_" + System.currentTimeMillis()); createFolder(zooKeeperWorkingDir); } final String confName = zooKeeperWorkingDir + File.separator + "zoo.cfg"; int validZkPort = getValidZooKeeperPort(); prepZooKeeperConf(zooKeeperWorkingDir, confName, validZkPort + ""); Thread thread = new Thread(new Runnable() { @Override public void run() { QuorumPeerMain.main(new String[] { confName }); } }, "Embed ZooKeeper"); thread.setDaemon(true); thread.start(); final String cleanZkFolder = zooKeeperWorkingDir; // used local data should be cleaned. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { try { FileUtils.deleteDirectory(new File(cleanZkFolder)); } catch (IOException ignore) { } } })); return validZkPort; }
public void start() { if (zkRun == null) return; 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 (Throwable e) { log.error("ZooKeeper Server ERROR", e); throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } } }; if (zkProps.getServers().size() > 1) { log.info("STARTING EMBEDDED ENSEMBLE ZOOKEEPER SERVER at port " + zkProps.getClientPortAddress().getPort()); } else { log.info("STARTING EMBEDDED STANDALONE ZOOKEEPER SERVER at port " + zkProps.getClientPortAddress().getPort()); } zkThread.setDaemon(true); zkThread.start(); try { Thread.sleep(500); // pause for ZooKeeper to start } catch (Exception e) { log.error("STARTING ZOOKEEPER", e); } }
@Override public void run() { QuorumPeerMain.main(new String[]{MultiServerTest.class.getResource(config).getPath()}); }
/** * Start embed zookeeper server in a child process. * * @return null if start child process failed, non empty string if valid zookeeper server in child. */ public static String startChildZooKeeperProcess(String zkJavaOpts) throws IOException { // 1. prepare working dir String zooKeeperWorkingDir = getZooKeeperWorkingDir(); boolean isSuccessful = createFolder(zooKeeperWorkingDir); if(!isSuccessful) { zooKeeperWorkingDir = getZooKeeperWorkingDir("zookeeper_" + System.currentTimeMillis()); createFolder(zooKeeperWorkingDir); } // 2. prepare conf file final String confName = zooKeeperWorkingDir + File.separator + "zoo.cfg"; int validZkPort = getValidZooKeeperPort(); prepZooKeeperConf(zooKeeperWorkingDir, confName, validZkPort + ""); // 3. prepare process buider command ProcessBuilder processBuilder = new ProcessBuilder(); List<String> commandList = new ArrayList<String>(); String javaHome = System.getProperty("java.home"); if(javaHome == null) { throw new IllegalArgumentException("java.home is not set!"); } commandList.add(javaHome + "/bin/java"); String[] zkJavaOptsArray = zkJavaOpts.split(" "); if(zkJavaOptsArray != null) { commandList.addAll(Arrays.asList(zkJavaOptsArray)); } commandList.add("-cp"); commandList.add(findContainingJar(Log4jLoggerAdapter.class) + ":" + findContainingJar(Logger.class) + ":" + findContainingJar(org.apache.log4j.Logger.class) + ":" + findContainingJar(ZooKeeperUtils.class) + ":" + findContainingJar(QuorumPeerMain.class)); commandList.add(ZooKeeperMain.class.getName()); commandList.add(confName); processBuilder.command(commandList); File execDirectory = new File(zooKeeperWorkingDir); processBuilder.directory(execDirectory); processBuilder.redirectErrorStream(true); LOG.info("onlineZooKeeperServers: Attempting to start ZooKeeper server with command {} in directory {}.", commandList, execDirectory.toString()); // 4. start process Process zkProcess = null; StreamCollector zkProcessCollector; synchronized(ZooKeeperUtils.class) { zkProcess = processBuilder.start(); zkProcessCollector = new StreamCollector(zkProcess.getInputStream()); zkProcessCollector.start(); } Runtime.getRuntime().addShutdownHook( new Thread(new ZooKeeperShutdownHook(zkProcess, zkProcessCollector, zooKeeperWorkingDir))); LOG.info("onlineZooKeeperServers: Shutdown hook added."); // 5. check and wait for server just started. String hostname = getLocalHostName(); if(isServerAlive(hostname, validZkPort)) { return hostname + ":" + validZkPort; } else { return null; } }
/** * 通过官方的QuorumPeerMain启动类启动真集群模式 * 会执行quorumPeer.join(); * 需要在不同的服务器上执行 * @param config * @throws IOException * @throws AdminServer.AdminServerException */ public void startCluster(QuorumPeerConfig config) throws IOException, AdminServer.AdminServerException { QuorumPeerMain main = new QuorumPeerMain(); main.runFromConfig(config); }