- * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper. - *
- * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for - * org.springframework.xd.dirt.server.singlenode.SingleNodeApplication - * - * @author Patrick Peralta - * @author Mark Fisher - * @author David Turanski - */ -public class EmbeddedZooKeeper implements SmartLifecycle { - - /** - * Logger. - */ - private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class); - - /** - * ZooKeeper client port. This will be determined dynamically upon startup. - */ - private final int clientPort; - - /** - * Whether to auto-start. Default is true. - */ - private boolean autoStartup = true; - - /** - * Lifecycle phase. Default is 0. - */ - private int phase = 0; - - /** - * Thread for running the ZooKeeper server. - */ - private volatile Thread zkServerThread; - - /** - * ZooKeeper server. - */ - private volatile ZooKeeperServerMain zkServer; - - /** - * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. - */ - private ErrorHandler errorHandler; - - private boolean daemon = true; - - /** - * Construct an EmbeddedZooKeeper with a random port. - */ - public EmbeddedZooKeeper() { - clientPort = SocketUtils.findAvailableTcpPort(); - } - - /** - * Construct an EmbeddedZooKeeper with the provided port. - * - * @param clientPort port for ZooKeeper server to bind to - * @param daemon is daemon or not thread. - */ - public EmbeddedZooKeeper(int clientPort, boolean daemon) { - this.clientPort = clientPort; - this.daemon = daemon; - } - - /** - * Returns the port that clients should use to connect to this embedded server. - * - * @return dynamically determined client port - */ - public int getClientPort() { - return this.clientPort; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isAutoStartup() { - return this.autoStartup; - } - - /** - * Specify whether to start automatically. Default is true. - * - * @param autoStartup whether to start automatically - */ - public void setAutoStartup(boolean autoStartup) { - this.autoStartup = autoStartup; - } - - /** - * {@inheritDoc} - */ - @Override - public int getPhase() { - return this.phase; - } - - /** - * Specify the lifecycle phase for the embedded server. - * - * @param phase the lifecycle phase - */ - public void setPhase(int phase) { - this.phase = phase; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isRunning() { - return (zkServerThread != null); - } - - /** - * Start the ZooKeeper server in a background thread. - *
- * Register an error handler via {@link #setErrorHandler} in order to handle
- * any exceptions thrown during startup or execution.
- */
- @Override
- public synchronized void start() {
- if (zkServerThread == null) {
- zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter");
- zkServerThread.setDaemon(daemon);
- zkServerThread.start();
- }
- }
-
- /**
- * Shutdown the ZooKeeper server.
- */
- @Override
- public synchronized void stop() {
- if (zkServerThread != null) {
- // The shutdown method is protected...thus this hack to invoke it.
- // This will log an exception on shutdown; see
- // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details.
- try {
- Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown");
- shutdown.setAccessible(true);
- shutdown.invoke(zkServer);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
-
- // It is expected that the thread will exit after
- // the server is shutdown; this will block until
- // the shutdown is complete.
- try {
- zkServerThread.join(5000);
- zkServerThread = null;
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- logger.warn("Interrupted while waiting for embedded ZooKeeper to exit");
- // abandoning zk thread
- zkServerThread = null;
- }
- }
- }
-
- /**
- * Stop the server if running and invoke the callback when complete.
- */
- @Override
- public void stop(Runnable callback) {
- stop();
- callback.run();
- }
-
- /**
- * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none
- * is provided, only error-level logging will occur.
- *
- * @param errorHandler the {@link ErrorHandler} to be invoked
- */
- public void setErrorHandler(ErrorHandler errorHandler) {
- this.errorHandler = errorHandler;
- }
-
- /**
- * Runnable implementation that starts the ZooKeeper server.
- */
- private class ServerRunnable implements Runnable {
-
- @Override
- public void run() {
- try {
- Properties properties = new Properties();
- File file = new File(System.getProperty("java.io.tmpdir")
- + File.separator + UUID.randomUUID());
- file.deleteOnExit();
- properties.setProperty("dataDir", file.getAbsolutePath());
- properties.setProperty("clientPort", String.valueOf(clientPort));
-
- QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
- quorumPeerConfig.parseProperties(properties);
-
- zkServer = new ZooKeeperServerMain();
- ServerConfig configuration = new ServerConfig();
- configuration.readFrom(quorumPeerConfig);
-
- zkServer.runFromConfig(configuration);
- } catch (Exception e) {
- if (errorHandler != null) {
- errorHandler.handleError(e);
- } else {
- logger.error("Exception running embedded ZooKeeper", e);
- }
- }
- }
- }
-
-}
diff --git a/dubbo-spring-boot-samples/dubbo-registry-zookeeper-samples/provider-sample/src/main/resources/application.properties b/dubbo-spring-boot-samples/dubbo-registry-zookeeper-samples/provider-sample/src/main/resources/application.properties
index 0c9116cdd..516281800 100644
--- a/dubbo-spring-boot-samples/dubbo-registry-zookeeper-samples/provider-sample/src/main/resources/application.properties
+++ b/dubbo-spring-boot-samples/dubbo-registry-zookeeper-samples/provider-sample/src/main/resources/application.properties
@@ -7,15 +7,13 @@ dubbo.scan.base-packages=org.apache.dubbo.spring.boot.demo.provider.service
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
-embedded.zookeeper.port = 2181
-
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
-dubbo.registry.address=zookeeper://127.0.0.1:${embedded.zookeeper.port}
+dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
## DemoService version
diff --git a/dubbo-spring-boot-samples/pom.xml b/dubbo-spring-boot-samples/pom.xml
index 7e79622d6..d2a4ba12a 100644
--- a/dubbo-spring-boot-samples/pom.xml
+++ b/dubbo-spring-boot-samples/pom.xml
@@ -78,5 +78,21 @@
+