reference = new ReferenceConfig<>();
+ reference.setUrl("dubbo://" + providerAddress + ":20880/" + DirectService.class.getName());
+ reference.setVersion("1.0.0-daily");
+ reference.setGroup("test3");
+ reference.setApplication(application);
+ reference.setInterface(DirectService.class.getName());
+ DirectService service = reference.get();
+ String result = service.sayHello("api");
+ TestCase.assertTrue(result.startsWith("Hello api"));
+ }
+}
From 047f2490912c5b0ec5783c2d22da782aa621a41f Mon Sep 17 00:00:00 2001
From: Wesley <2915519956@qq.com>
Date: Mon, 6 Mar 2023 22:27:14 +0800
Subject: [PATCH 08/12] update: update case-configuration.yml
---
.../case-configuration.yml | 27 ++++++++++---------
.../apache/dubbo/samples/direct/api/Task.java | 2 +-
2 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/2-advanced/dubbo-samples-direct/case-configuration.yml b/2-advanced/dubbo-samples-direct/case-configuration.yml
index e6d08a7d01..b82861167a 100644
--- a/2-advanced/dubbo-samples-direct/case-configuration.yml
+++ b/2-advanced/dubbo-samples-direct/case-configuration.yml
@@ -15,31 +15,32 @@
# limitations under the License.
services:
- zookeeper:
- image: zookeeper:latest
- provider:
+ direct-provider:
type: app
basedir: dubbo-samples-direct-provider
mainClass: org.apache.dubbo.samples.direct.DirectProviderApplication
- systemProps:
- - zookeeper.address=zookeeper
- waitPortsBeforeRun:
- - zookeeper:2181
- checkPorts:
- - 20880
- checkLog: "Current Spring Boot Application is await..."
+# systemProps:
+# - zookeeper.address=zookeeper
+# waitPortsBeforeRun:
+# - zookeeper:2181
+# checkPorts:
+# - 20880
+# checkLog: "Current Spring Boot Application is await..."
- test:
+ direct-consumer:
type: test
basedir: dubbo-samples-direct-consumer
tests:
- "**/*IT.class"
systemProps:
- zookeeper.address=zookeeper
+ - zookeeper.port=2181
+ - dubbo.address=direct-provider
+ - dubbo.port=20880
waitPortsBeforeRun:
- zookeeper:2181
- - provider:20880
+ - direct-provider:20880
depends_on:
- - provider
+ - direct-provider
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java
index 2cceb26952..837af315fb 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java
@@ -28,7 +28,7 @@
public class Task implements CommandLineRunner {
@DubboReference(interfaceClass = DirectService.class,
check = false,
- group = "test",
+ group = "*",
version = "1.0.0-daily")
private DirectService directService;
From c974b4168b7be050d88f2f09402d72c914ac7097 Mon Sep 17 00:00:00 2001
From: Wesley <2915519956@qq.com>
Date: Mon, 6 Mar 2023 22:44:53 +0800
Subject: [PATCH 09/12] update: update case-configuration.yml
---
2-advanced/dubbo-samples-direct/case-configuration.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/2-advanced/dubbo-samples-direct/case-configuration.yml b/2-advanced/dubbo-samples-direct/case-configuration.yml
index b82861167a..78cb951002 100644
--- a/2-advanced/dubbo-samples-direct/case-configuration.yml
+++ b/2-advanced/dubbo-samples-direct/case-configuration.yml
@@ -19,7 +19,7 @@ services:
direct-provider:
type: app
basedir: dubbo-samples-direct-provider
- mainClass: org.apache.dubbo.samples.direct.DirectProviderApplication
+ mainClass: org.apache.dubbo.samples.direct.api.DirectProviderApplication
# systemProps:
# - zookeeper.address=zookeeper
# waitPortsBeforeRun:
From 28f4274aa956973a14b4836b91f3a2654fa93825 Mon Sep 17 00:00:00 2001
From: Wesley <2915519956@qq.com>
Date: Mon, 6 Mar 2023 22:53:15 +0800
Subject: [PATCH 10/12] update: update case-configuration.yml
---
2-advanced/dubbo-samples-direct/case-configuration.yml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/2-advanced/dubbo-samples-direct/case-configuration.yml b/2-advanced/dubbo-samples-direct/case-configuration.yml
index 78cb951002..26ffbd0f5a 100644
--- a/2-advanced/dubbo-samples-direct/case-configuration.yml
+++ b/2-advanced/dubbo-samples-direct/case-configuration.yml
@@ -34,12 +34,12 @@ services:
tests:
- "**/*IT.class"
systemProps:
- - zookeeper.address=zookeeper
+ - zookeeper.address=direct-provider
- zookeeper.port=2181
- dubbo.address=direct-provider
- dubbo.port=20880
waitPortsBeforeRun:
- - zookeeper:2181
+ - cache-provider:2181
- direct-provider:20880
depends_on:
- direct-provider
From de489c0c7df3fa31f5a1e89ccb0dd5b7579fc290 Mon Sep 17 00:00:00 2001
From: Wesley <2915519956@qq.com>
Date: Tue, 7 Mar 2023 00:21:05 +0800
Subject: [PATCH 11/12] update: update case-configuration.yml
---
2-advanced/dubbo-samples-direct/case-configuration.yml | 5 ++---
.../samples/direct/{api => }/DirectConsumerApplication.java | 2 +-
.../org/apache/dubbo/samples/direct/{api => }/Task.java | 6 +++---
.../dubbo/samples/direct/{api => }/DirectServiceIT.java | 5 ++++-
.../samples/direct/{api => }/DirectProviderApplication.java | 2 +-
.../samples/direct/{api => }/impl/DirectServiceImpl.java | 3 +--
.../samples/direct/{api => }/impl/DirectServiceImpl2.java | 2 +-
.../samples/direct/{api => }/impl/DirectServiceImpl3.java | 2 +-
8 files changed, 14 insertions(+), 13 deletions(-)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/{api => }/DirectConsumerApplication.java (96%)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/{api => }/Task.java (91%)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/{api => }/DirectServiceIT.java (97%)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/{api => }/DirectProviderApplication.java (96%)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/{api => }/impl/DirectServiceImpl.java (96%)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/{api => }/impl/DirectServiceImpl2.java (96%)
rename 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/{api => }/impl/DirectServiceImpl3.java (96%)
diff --git a/2-advanced/dubbo-samples-direct/case-configuration.yml b/2-advanced/dubbo-samples-direct/case-configuration.yml
index 26ffbd0f5a..cbd378da4c 100644
--- a/2-advanced/dubbo-samples-direct/case-configuration.yml
+++ b/2-advanced/dubbo-samples-direct/case-configuration.yml
@@ -19,7 +19,7 @@ services:
direct-provider:
type: app
basedir: dubbo-samples-direct-provider
- mainClass: org.apache.dubbo.samples.direct.api.DirectProviderApplication
+ mainClass: org.apache.dubbo.samples.direct.DirectProviderApplication
# systemProps:
# - zookeeper.address=zookeeper
# waitPortsBeforeRun:
@@ -27,7 +27,6 @@ services:
# checkPorts:
# - 20880
# checkLog: "Current Spring Boot Application is await..."
-
direct-consumer:
type: test
basedir: dubbo-samples-direct-consumer
@@ -39,7 +38,7 @@ services:
- dubbo.address=direct-provider
- dubbo.port=20880
waitPortsBeforeRun:
- - cache-provider:2181
+ - direct-provider:2181
- direct-provider:20880
depends_on:
- direct-provider
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/DirectConsumerApplication.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/DirectConsumerApplication.java
similarity index 96%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/DirectConsumerApplication.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/DirectConsumerApplication.java
index ed32a1cda0..6d17428545 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/DirectConsumerApplication.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/DirectConsumerApplication.java
@@ -16,7 +16,7 @@
*/
-package org.apache.dubbo.samples.direct.api;
+package org.apache.dubbo.samples.direct;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/Task.java
similarity index 91%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/Task.java
index 837af315fb..b909465337 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/api/Task.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/main/java/org/apache/dubbo/samples/direct/Task.java
@@ -15,10 +15,10 @@
* limitations under the License.
*/
-package org.apache.dubbo.samples.direct.api;
+package org.apache.dubbo.samples.direct;
import org.apache.dubbo.config.annotation.DubboReference;
-import org.apache.dubbo.config.annotation.Method;
+import org.apache.dubbo.samples.direct.api.DirectService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@@ -28,7 +28,7 @@
public class Task implements CommandLineRunner {
@DubboReference(interfaceClass = DirectService.class,
check = false,
- group = "*",
+ group = "test",
version = "1.0.0-daily")
private DirectService directService;
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/api/DirectServiceIT.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/DirectServiceIT.java
similarity index 97%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/api/DirectServiceIT.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/DirectServiceIT.java
index b844b3cc7e..0f93040042 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/api/DirectServiceIT.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-consumer/src/test/java/org/apache/dubbo/samples/direct/DirectServiceIT.java
@@ -15,13 +15,14 @@
* limitations under the License.
*/
-package org.apache.dubbo.samples.direct.api;
+package org.apache.dubbo.samples.direct;
import junit.framework.TestCase;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.service.GenericService;
+import org.apache.dubbo.samples.direct.api.DirectService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,11 +44,13 @@ public class DirectServiceIT {
@Test
public void testXml() throws Exception {
+
Assert.assertTrue(directService.sayHello("dubbo").startsWith("Hello dubbo"));
}
@Test
public void testGeneric() throws Exception {
+
ApplicationConfig application = new ApplicationConfig();
application.setName("direct-consumer");
System.out.println(application.getClass());
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/DirectProviderApplication.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java
similarity index 96%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/DirectProviderApplication.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java
index 6f006c2d1b..92a12ba7fb 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/DirectProviderApplication.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.dubbo.samples.direct.api;
+package org.apache.dubbo.samples.direct;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl.java
similarity index 96%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl.java
index 9b9bacbdb0..eee4d8a028 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl.java
@@ -15,13 +15,12 @@
* limitations under the License.
*/
-package org.apache.dubbo.samples.direct.api.impl;
+package org.apache.dubbo.samples.direct.impl;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.samples.direct.api.DirectService;
-
import java.text.SimpleDateFormat;
import java.util.Date;
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl2.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl2.java
similarity index 96%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl2.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl2.java
index f3bdda5b6b..959f25af84 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl2.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl2.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.dubbo.samples.direct.api.impl;
+package org.apache.dubbo.samples.direct.impl;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl3.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl3.java
similarity index 96%
rename from 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl3.java
rename to 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl3.java
index 617d96d9ad..e5e26166b2 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/api/impl/DirectServiceImpl3.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/impl/DirectServiceImpl3.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.dubbo.samples.direct.api.impl;
+package org.apache.dubbo.samples.direct.impl;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
From 311fc91065f263c382877756d26856e1f219af1d Mon Sep 17 00:00:00 2001
From: Wesley <2915519956@qq.com>
Date: Tue, 7 Mar 2023 09:47:40 +0800
Subject: [PATCH 12/12] add:add EmbeddedZookeeper
---
.../direct/DirectProviderApplication.java | 2 +
.../samples/direct/EmbeddedZooKeeper.java | 225 ++++++++++++++++++
2 files changed, 227 insertions(+)
create mode 100644 2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/EmbeddedZooKeeper.java
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java
index 92a12ba7fb..02f56f6d0a 100644
--- a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/DirectProviderApplication.java
@@ -24,7 +24,9 @@
@SpringBootApplication
@EnableDubbo
public class DirectProviderApplication {
+
public static void main(String[] args) {
+ new EmbeddedZooKeeper(2181,false).start();
SpringApplication.run(DirectProviderApplication.class, args);
}
}
diff --git a/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/EmbeddedZooKeeper.java b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/EmbeddedZooKeeper.java
new file mode 100644
index 0000000000..e050bb6757
--- /dev/null
+++ b/2-advanced/dubbo-samples-direct/dubbo-samples-direct-provider/src/main/java/org/apache/dubbo/samples/direct/EmbeddedZooKeeper.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.samples.direct;
+
+import org.apache.zookeeper.server.ServerConfig;
+import org.apache.zookeeper.server.ZooKeeperServerMain;
+import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.SmartLifecycle;
+import org.springframework.util.ErrorHandler;
+import org.springframework.util.SocketUtils;
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.Properties;
+import java.util.UUID;
+/**
+ * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java
+ *
+ * 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
+ * {@link 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
+ */
+ 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;
+ }
+ /**
+ * 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 boolean isAutoStartup() {
+ return this.autoStartup;
+ }
+ /**
+ * Specify the lifecycle phase for the embedded server.
+ *
+ * @param phase the lifecycle phase
+ */
+ public void setPhase(int phase) {
+ this.phase = phase;
+ }
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int getPhase() {
+ return this.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);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file