Update sentinel version (#490)
* Update sentinel version
* update demo
diff --git a/dubbo-samples-sentinel/case-configuration.yml b/dubbo-samples-sentinel/case-configuration.yml
index f6e2383..425e53c 100644
--- a/dubbo-samples-sentinel/case-configuration.yml
+++ b/dubbo-samples-sentinel/case-configuration.yml
@@ -14,10 +14,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from: app-external-zookeeper.yml
+from: app-builtin-zookeeper.yml
props:
project_name: dubbo-samples-sentinel
main_class: org.apache.samples.sentinel.FooProviderBootstrap
- dubbo_port: 20880
-
+ zookeeper_port: 2181
+ dubbo_port: 20880
\ No newline at end of file
diff --git a/dubbo-samples-sentinel/pom.xml b/dubbo-samples-sentinel/pom.xml
index 9bbdb40..e2f9760 100644
--- a/dubbo-samples-sentinel/pom.xml
+++ b/dubbo-samples-sentinel/pom.xml
@@ -34,7 +34,7 @@
<dubbo.version>3.0.7</dubbo.version>
<junit.version>4.12</junit.version>
<spring.version>4.3.16.RELEASE</spring.version>
- <sentinel.version>1.6.2</sentinel.version>
+ <sentinel.version>1.8.4</sentinel.version>
</properties>
<dependencyManagement>
@@ -140,6 +140,19 @@
<target>${target.level}</target>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>2.4.1</version>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
</project>
diff --git a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/EmbeddedZooKeeper.java b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/EmbeddedZooKeeper.java
new file mode 100644
index 0000000..f77efee
--- /dev/null
+++ b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/EmbeddedZooKeeper.java
@@ -0,0 +1,255 @@
+/*
+ * 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.samples.sentinel;
+
+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.
+ * <p>
+ * 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
diff --git a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooConsumerBootstrap.java b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooConsumerBootstrap.java
index c91464d..6fe5ce0 100644
--- a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooConsumerBootstrap.java
+++ b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooConsumerBootstrap.java
@@ -43,10 +43,13 @@
try {
String message = service.sayHello("dubbo");
System.out.println("Success: " + message);
- } catch (SentinelRpcException ex) {
- System.out.println("Blocked");
- } catch (Exception ex) {
- ex.printStackTrace();
+ } catch (RuntimeException ex) {
+ if (ex.getMessage().contains("SentinelBlockException: FlowException")) {
+ System.out.println("Blocked");
+ }
+ else {
+ ex.printStackTrace();
+ }
}
}
}
diff --git a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooProviderBootstrap.java b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooProviderBootstrap.java
index 68b8a66..3cd1a56 100644
--- a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooProviderBootstrap.java
+++ b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/FooProviderBootstrap.java
@@ -40,6 +40,7 @@
private static final String RES_KEY = INTERFACE_RES_KEY + ":sayHello(java.lang.String)";
public static void main(String[] args) throws InterruptedException {
+ new EmbeddedZooKeeper(2181, false).start();
// Users don't need to manually call this method.
// Only for eager initialization.
InitExecutor.doInit();
@@ -55,7 +56,7 @@
}
private static void initFlowRule() {
- FlowRule flowRule = new FlowRule(INTERFACE_RES_KEY)
+ FlowRule flowRule = new FlowRule(FooService.class.getName())
.setCount(10)
.setGrade(RuleConstant.FLOW_GRADE_QPS);
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
diff --git a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/provider/ProviderConfiguration.java b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/provider/ProviderConfiguration.java
index 553c88a..cf34349 100644
--- a/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/provider/ProviderConfiguration.java
+++ b/dubbo-samples-sentinel/src/main/java/org/apache/samples/sentinel/provider/ProviderConfiguration.java
@@ -29,7 +29,7 @@
@Configuration
@EnableDubbo
public class ProviderConfiguration {
- @Value("zookeeper://${zookeeper.address:127.0.0.1}:2181")
+ @Value("zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port:2181}")
private String zookeeperAddress;
@Value("${dubbo.port:20880}")
diff --git a/dubbo-samples-sentinel/src/test/java/org/apache/samples/sentinel/FooServiceIT.java b/dubbo-samples-sentinel/src/test/java/org/apache/samples/sentinel/FooServiceIT.java
index 11d19af..dcab781 100644
--- a/dubbo-samples-sentinel/src/test/java/org/apache/samples/sentinel/FooServiceIT.java
+++ b/dubbo-samples-sentinel/src/test/java/org/apache/samples/sentinel/FooServiceIT.java
@@ -17,11 +17,9 @@
package org.apache.samples.sentinel;
-import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
-import org.apache.dubbo.rpc.RpcException;
-
import org.apache.samples.sentinel.consumer.ConsumerConfiguration;
import org.apache.samples.sentinel.consumer.FooServiceConsumer;
+import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -43,17 +41,13 @@
}
}
- @Test(expected = com.alibaba.csp.sentinel.slots.block.flow.FlowException.class)
- public void testFlowControl2() throws Throwable {
+ @Test
+ public void testFlowControl2() {
for (int i = 0; i < 11; i++) {
try {
consumer.sayHello("dubbo");
} catch (Throwable e) {
- if (e.getMessage().contains("com.alibaba.csp.sentinel.slots.block.flow.FlowException")) {
- throw new FlowException(e.getMessage());
- }
- e.printStackTrace();
- throw e.getCause();
+ Assert.assertTrue(e.getMessage().contains("SentinelBlockException: FlowException"));
}
}
}