You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/08/07 12:48:57 UTC

[incubator-servicecomb-java-chassis] 01/04: SCB-688 vertx rest server not accept too many connection

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit b973c2b34ba483aa282912804c919178547c4af1
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Thu Aug 2 13:57:11 2018 +0800

    SCB-688 vertx rest server not accept too many connection
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 coverage-reports/pom.xml                           | 30 ++++++++++++++++++++
 .../{ClientClosedEvent.java => ClientEvent.java}   | 18 ++++++++++--
 .../{ClientConnectedEvent.java => EventType.java}  | 27 +++---------------
 .../{ClientClosedEvent.java => ServerType.java}    | 25 +++-------------
 .../foundation/vertx/server/TcpServer.java         |  7 +++--
 .../vertx/server/TcpServerConnection.java          |  6 ++--
 .../demo/pojo/test/ConnectionEventWatcher.java     | 15 ++++------
 integration-tests/springmvc-tests/pom.xml          |  1 +
 .../springmvc/tests}/ConnectionEventWatcher.java   | 17 +++++------
 .../{ => springmvc-tests-connection-limit}/pom.xml | 24 ++++++----------
 .../tests/RawSpringMvcIntegrationTest.java         | 27 ++++++++++++++++--
 .../demo/springmvc/tests/SpringMvcTestMain.java}   | 20 ++++---------
 .../src/test/resources/microservice.yaml           | 33 ++++++++++++++++++++++
 .../tests/RawSpringMvcIntegrationTest.java         |  7 +++++
 ...SimplifiedMappingAnnotationIntegrationTest.java |  7 +++++
 .../transport/rest/vertx/RestServerVerticle.java   | 33 ++++++++++++++++++++++
 .../transport/rest/vertx/VertxRestTransport.java   |  8 ++++++
 17 files changed, 205 insertions(+), 100 deletions(-)

diff --git a/coverage-reports/pom.xml b/coverage-reports/pom.xml
index ecbc01c..6a174b3 100644
--- a/coverage-reports/pom.xml
+++ b/coverage-reports/pom.xml
@@ -268,6 +268,31 @@
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>pojo-test</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>spring-jaxrs-tests</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>spring-pojo-tests</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>spring-pojo-connection-limit-test</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>spring-zuul-tracing-tests</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.tests</groupId>
       <artifactId>springmvc-tests-general</artifactId>
       <version>1.1.0-SNAPSHOT</version>
     </dependency>
@@ -283,6 +308,11 @@
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>springmvc-tests-connection-limit</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.tests</groupId>
       <artifactId>springmvc-tests-simplified-mapping-with-springboot</artifactId>
       <version>1.1.0-SNAPSHOT</version>
     </dependency>
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientClosedEvent.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientEvent.java
similarity index 75%
copy from foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientClosedEvent.java
copy to foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientEvent.java
index 68fa037..a8d8ea7 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientClosedEvent.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientEvent.java
@@ -20,21 +20,35 @@ package org.apache.servicecomb.foundation.vertx;
 /**
  * Notice: this event will raised in vertx eventloop thread, so do not run any block code
  */
-public class ClientClosedEvent {
+public class ClientEvent {
   private final String address;
 
+  private final EventType eventType;
+
+  private final ServerType serverType;
+
   private final int totalConnectedCount;
 
   public String getAddress() {
     return address;
   }
 
+  public EventType getEventType() {
+    return eventType;
+  }
+
+  public ServerType getServerType() {
+    return serverType;
+  }
+
   public int getTotalConnectedCount() {
     return totalConnectedCount;
   }
 
-  public ClientClosedEvent(String address, int totalConnectedCount) {
+  public ClientEvent(String address, EventType eventType, ServerType serverType, int totalConnectedCount) {
     this.address = address;
+    this.eventType = eventType;
+    this.serverType = serverType;
     this.totalConnectedCount = totalConnectedCount;
   }
 }
\ No newline at end of file
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientConnectedEvent.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/EventType.java
similarity index 60%
rename from foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientConnectedEvent.java
rename to foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/EventType.java
index 09e752e..9c46986 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientConnectedEvent.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/EventType.java
@@ -17,26 +17,7 @@
 
 package org.apache.servicecomb.foundation.vertx;
 
-import io.vertx.core.net.NetSocket;
-
-/**
- * Notice: this event will raised in vertx eventloop thread, so do not run any block code
- */
-public class ClientConnectedEvent {
-  private final NetSocket netSocket;
-
-  private final int totalConnectedCount;
-
-  public NetSocket getNetSocket() {
-    return netSocket;
-  }
-
-  public int getTotalConnectedCount() {
-    return totalConnectedCount;
-  }
-
-  public ClientConnectedEvent(NetSocket netSocket, int totalConnectedCount) {
-    this.netSocket = netSocket;
-    this.totalConnectedCount = totalConnectedCount;
-  }
-}
\ No newline at end of file
+public enum EventType {
+  Connected,
+  Closed
+}
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientClosedEvent.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ServerType.java
similarity index 63%
rename from foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientClosedEvent.java
rename to foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ServerType.java
index 68fa037..b054a75 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ClientClosedEvent.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/ServerType.java
@@ -17,24 +17,7 @@
 
 package org.apache.servicecomb.foundation.vertx;
 
-/**
- * Notice: this event will raised in vertx eventloop thread, so do not run any block code
- */
-public class ClientClosedEvent {
-  private final String address;
-
-  private final int totalConnectedCount;
-
-  public String getAddress() {
-    return address;
-  }
-
-  public int getTotalConnectedCount() {
-    return totalConnectedCount;
-  }
-
-  public ClientClosedEvent(String address, int totalConnectedCount) {
-    this.address = address;
-    this.totalConnectedCount = totalConnectedCount;
-  }
-}
\ No newline at end of file
+public enum ServerType {
+  Highway,
+  Rest
+}
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServer.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServer.java
index a2db206..1c6671d 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServer.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServer.java
@@ -26,7 +26,9 @@ import org.apache.servicecomb.foundation.ssl.SSLCustom;
 import org.apache.servicecomb.foundation.ssl.SSLOption;
 import org.apache.servicecomb.foundation.ssl.SSLOptionFactory;
 import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
-import org.apache.servicecomb.foundation.vertx.ClientConnectedEvent;
+import org.apache.servicecomb.foundation.vertx.ClientEvent;
+import org.apache.servicecomb.foundation.vertx.EventType;
+import org.apache.servicecomb.foundation.vertx.ServerType;
 import org.apache.servicecomb.foundation.vertx.VertxTLSBuilder;
 
 import com.netflix.config.DynamicPropertyFactory;
@@ -76,7 +78,8 @@ public class TcpServer {
 
       TcpServerConnection connection = createTcpServerConnection();
       connection.init(netSocket, connectedCounter);
-      EventManager.post(new ClientConnectedEvent(netSocket, connectedCount));
+      EventManager.post(new ClientEvent(netSocket.remoteAddress().toString(),
+          EventType.Connected, ServerType.Highway, connectedCount));
     });
 
     InetSocketAddress socketAddress = endpointObject.getSocketAddress();
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServerConnection.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServerConnection.java
index 5c47100..1fe0e06 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServerConnection.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServerConnection.java
@@ -19,7 +19,9 @@ package org.apache.servicecomb.foundation.vertx.server;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.servicecomb.foundation.common.event.EventManager;
-import org.apache.servicecomb.foundation.vertx.ClientClosedEvent;
+import org.apache.servicecomb.foundation.vertx.ClientEvent;
+import org.apache.servicecomb.foundation.vertx.EventType;
+import org.apache.servicecomb.foundation.vertx.ServerType;
 import org.apache.servicecomb.foundation.vertx.tcp.TcpConnection;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,7 +54,7 @@ public class TcpServerConnection extends TcpConnection {
           Thread.currentThread().getName());
 
       int connectedCount = connectedCounter.decrementAndGet();
-      EventManager.post(new ClientClosedEvent(remoteAddress, connectedCount));
+      EventManager.post(new ClientEvent(remoteAddress, EventType.Closed, ServerType.Highway, connectedCount));
     });
 
     netSocket.handler(splitter);
diff --git a/integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java b/integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java
index 68e58cc..0becc8c 100644
--- a/integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java
+++ b/integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java
@@ -20,8 +20,8 @@ package org.apache.servicecomb.demo.pojo.test;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.servicecomb.foundation.vertx.ClientClosedEvent;
-import org.apache.servicecomb.foundation.vertx.ClientConnectedEvent;
+import org.apache.servicecomb.foundation.vertx.ClientEvent;
+import org.apache.servicecomb.foundation.vertx.ServerType;
 
 import com.google.common.eventbus.Subscribe;
 
@@ -33,12 +33,9 @@ public class ConnectionEventWatcher {
   }
 
   @Subscribe
-  public void onConnected(ClientConnectedEvent event) {
-    counters.add(event.getTotalConnectedCount());
-  }
-
-  @Subscribe
-  public void onClosed(ClientClosedEvent event) {
-    counters.add(event.getTotalConnectedCount());
+  public void getEvent(ClientEvent event) {
+    if (ServerType.Highway.equals(event.getServerType())) {
+      counters.add(event.getTotalConnectedCount());
+    }
   }
 }
diff --git a/integration-tests/springmvc-tests/pom.xml b/integration-tests/springmvc-tests/pom.xml
index 732f77a..cd8908f 100644
--- a/integration-tests/springmvc-tests/pom.xml
+++ b/integration-tests/springmvc-tests/pom.xml
@@ -36,6 +36,7 @@
     <module>springmvc-tests-simplified-mapping</module>
     <module>springmvc-tests-general-with-springboot</module>
     <module>springmvc-tests-simplified-mapping-with-springboot</module>
+    <module>springmvc-tests-connection-limit</module>
   </modules>
 
   <dependencies>
diff --git a/integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java b/integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/ConnectionEventWatcher.java
similarity index 72%
copy from integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java
copy to integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/ConnectionEventWatcher.java
index 68e58cc..e3f61d8 100644
--- a/integration-tests/spring-pojo-tests/src/test/java/org/apache/servicecomb/demo/pojo/test/ConnectionEventWatcher.java
+++ b/integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/ConnectionEventWatcher.java
@@ -15,13 +15,13 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.demo.pojo.test;
+package org.apache.servicecomb.demo.springmvc.tests;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.servicecomb.foundation.vertx.ClientClosedEvent;
-import org.apache.servicecomb.foundation.vertx.ClientConnectedEvent;
+import org.apache.servicecomb.foundation.vertx.ClientEvent;
+import org.apache.servicecomb.foundation.vertx.ServerType;
 
 import com.google.common.eventbus.Subscribe;
 
@@ -33,12 +33,9 @@ public class ConnectionEventWatcher {
   }
 
   @Subscribe
-  public void onConnected(ClientConnectedEvent event) {
-    counters.add(event.getTotalConnectedCount());
-  }
-
-  @Subscribe
-  public void onClosed(ClientClosedEvent event) {
-    counters.add(event.getTotalConnectedCount());
+  public void getEvent(ClientEvent event) {
+    if (ServerType.Rest.equals(event.getServerType())) {
+      counters.add(event.getTotalConnectedCount());
+    }
   }
 }
diff --git a/integration-tests/springmvc-tests/pom.xml b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/pom.xml
similarity index 68%
copy from integration-tests/springmvc-tests/pom.xml
copy to integration-tests/springmvc-tests/springmvc-tests-connection-limit/pom.xml
index 732f77a..a0760fd 100644
--- a/integration-tests/springmvc-tests/pom.xml
+++ b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/pom.xml
@@ -20,28 +20,22 @@
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <parent>
-    <artifactId>integration-tests</artifactId>
+    <artifactId>springmvc-tests</artifactId>
     <groupId>org.apache.servicecomb.tests</groupId>
     <version>1.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
-  <artifactId>springmvc-tests</artifactId>
-  <packaging>pom</packaging>
-  <name>Java Chassis::Integration Tests::Spring MVC</name>
-
-  <modules>
-    <module>springmvc-tests-common</module>
-    <module>springmvc-tests-general</module>
-    <module>springmvc-tests-simplified-mapping</module>
-    <module>springmvc-tests-general-with-springboot</module>
-    <module>springmvc-tests-simplified-mapping-with-springboot</module>
-  </modules>
+  <artifactId>springmvc-tests-connection-limit</artifactId>
+  <name>Java Chassis::Integration Tests::Spring MVC Connection Limit</name>
 
   <dependencies>
     <dependency>
-      <groupId>org.apache.servicecomb.demo</groupId>
-      <artifactId>demo-schema</artifactId>
+      <groupId>org.apache.servicecomb.tests</groupId>
+      <artifactId>springmvc-tests-common</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+      <type>test-jar</type>
     </dependency>
   </dependencies>
-</project>
+
+</project>
\ No newline at end of file
diff --git a/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
similarity index 54%
copy from integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
copy to integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
index 17fec53..e4cd60d 100644
--- a/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
+++ b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
@@ -17,19 +17,42 @@
 
 package org.apache.servicecomb.demo.springmvc.tests;
 
+import static org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl.LOCAL_REGISTRY_FILE_KEY;
+import static org.junit.Assert.fail;
+
 import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
 import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.BeforeClass;
+import org.junit.Test;
+import org.springframework.web.client.RestTemplate;
+
+public class RawSpringMvcIntegrationTest {
+
+  private RestTemplate restTemplate = RestTemplateBuilder.create();
 
-public class RawSpringMvcIntegrationTest extends SpringMvcIntegrationTestBase {
+  private final String baseUrl = "http://127.0.0.1:8080/";
+
+  private final String controllerUrl = baseUrl + "springmvc/controller/";
 
   @BeforeClass
   public static void init() throws Exception {
     System.setProperty("servicecomb.uploads.directory", "/tmp");
-    setUpLocalRegistry();
+    System.setProperty(LOCAL_REGISTRY_FILE_KEY, "notExistJustForceLocal");
     SpringMvcTestMain.main(new String[0]);
   }
 
+  @Test
+  public void ensureServerWorksFine() {
+    try {
+      String result = restTemplate.getForObject(controllerUrl + "sayhi?name=world", String.class);
+      fail("connection limit failed");
+    } catch (Exception ex) {
+      Assert.assertEquals("java.net.SocketException: Unexpected end of file from server", ex.getCause().toString());
+    }
+  }
+
   @AfterClass
   public static void shutdown() {
     SCBEngine.getInstance().destroy();
diff --git a/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcTestMain.java
similarity index 64%
copy from integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
copy to integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcTestMain.java
index 17fec53..99d9370 100644
--- a/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
+++ b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcTestMain.java
@@ -17,21 +17,13 @@
 
 package org.apache.servicecomb.demo.springmvc.tests;
 
-import org.apache.servicecomb.core.SCBEngine;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
+import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
 
-public class RawSpringMvcIntegrationTest extends SpringMvcIntegrationTestBase {
+public class SpringMvcTestMain {
 
-  @BeforeClass
-  public static void init() throws Exception {
-    System.setProperty("servicecomb.uploads.directory", "/tmp");
-    setUpLocalRegistry();
-    SpringMvcTestMain.main(new String[0]);
-  }
-
-  @AfterClass
-  public static void shutdown() {
-    SCBEngine.getInstance().destroy();
+  public static void main(String[] args) throws Exception {
+    Log4jUtils.init();
+    BeanUtils.init();
   }
 }
diff --git a/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/resources/microservice.yaml b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/resources/microservice.yaml
new file mode 100644
index 0000000..97340cf
--- /dev/null
+++ b/integration-tests/springmvc-tests/springmvc-tests-connection-limit/src/test/resources/microservice.yaml
@@ -0,0 +1,33 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You 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.
+## ---------------------------------------------------------------------------
+
+APPLICATION_ID: springmvctest-it
+service_description:
+  name: springmvc-tests
+  version: 0.0.2
+servicecomb:
+  service:
+    registry:
+      address: http://127.0.0.1:30100
+  rest:
+    address: 0.0.0.0:8080
+    server:
+      connection-limit: 0
+  handler:
+    chain:
+      Provider:
+        default: bizkeeper-provider
diff --git a/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java b/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
index 17fec53..14d81da 100644
--- a/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
+++ b/integration-tests/springmvc-tests/springmvc-tests-general/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java
@@ -18,20 +18,27 @@
 package org.apache.servicecomb.demo.springmvc.tests;
 
 import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.BeforeClass;
 
 public class RawSpringMvcIntegrationTest extends SpringMvcIntegrationTestBase {
 
+  private static final ConnectionEventWatcher watcher = new ConnectionEventWatcher();
+
   @BeforeClass
   public static void init() throws Exception {
     System.setProperty("servicecomb.uploads.directory", "/tmp");
     setUpLocalRegistry();
+    EventManager.register(watcher);
     SpringMvcTestMain.main(new String[0]);
   }
 
   @AfterClass
   public static void shutdown() {
     SCBEngine.getInstance().destroy();
+    Assert.assertArrayEquals("check connection count change", new Integer[] {1, 2, 1, 0},
+        watcher.getCounters().toArray());
   }
 }
diff --git a/integration-tests/springmvc-tests/springmvc-tests-simplified-mapping/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java b/integration-tests/springmvc-tests/springmvc-tests-simplified-mapping/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java
index 709152c..32cfb03 100644
--- a/integration-tests/springmvc-tests/springmvc-tests-simplified-mapping/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java
+++ b/integration-tests/springmvc-tests/springmvc-tests-simplified-mapping/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java
@@ -18,21 +18,28 @@
 package org.apache.servicecomb.demo.springmvc.tests;
 
 import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.BeforeClass;
 
 public class RawSpringMvcSimplifiedMappingAnnotationIntegrationTest extends SpringMvcIntegrationTestBase {
 
+  private static final ConnectionEventWatcher watcher = new ConnectionEventWatcher();
+
   @BeforeClass
   public static void init() throws Exception {
     System.setProperty("spring.profiles.active", "SimplifiedMapping");
     System.setProperty("servicecomb.uploads.directory", "/tmp");
     setUpLocalRegistry();
+    EventManager.register(watcher);
     SpringMvcTestMain.main(new String[0]);
   }
 
   @AfterClass
   public static void shutdown() {
     SCBEngine.getInstance().destroy();
+    Assert.assertArrayEquals("check connection count change", new Integer[] {1, 2, 1, 0},
+        watcher.getCounters().toArray());
   }
 }
diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
index 3780347..25b94d3 100644
--- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
+++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
@@ -19,20 +19,28 @@ package org.apache.servicecomb.transport.rest.vertx;
 
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.servicecomb.core.Endpoint;
 import org.apache.servicecomb.core.transport.AbstractTransport;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.ssl.SSLCustom;
 import org.apache.servicecomb.foundation.ssl.SSLOption;
 import org.apache.servicecomb.foundation.ssl.SSLOptionFactory;
+import org.apache.servicecomb.foundation.vertx.ClientEvent;
+import org.apache.servicecomb.foundation.vertx.EventType;
+import org.apache.servicecomb.foundation.vertx.ServerType;
 import org.apache.servicecomb.foundation.vertx.VertxTLSBuilder;
 import org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogConfiguration;
 import org.apache.servicecomb.transport.rest.vertx.accesslog.impl.AccessLogHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.netflix.config.DynamicPropertyFactory;
+
 import io.vertx.core.AbstractVerticle;
 import io.vertx.core.Context;
 import io.vertx.core.Future;
@@ -52,6 +60,16 @@ public class RestServerVerticle extends AbstractVerticle {
 
   private URIEndpointObject endpointObject;
 
+  private final AtomicInteger connectedCounter;
+
+  public RestServerVerticle() {
+    this(((VertxRestTransport) BeanUtils.getBean("vertxRestTransport")).getConnectedCounter());
+  }
+
+  public RestServerVerticle(AtomicInteger connectedCounter) {
+    this.connectedCounter = connectedCounter;
+  }
+
   @Override
   public void init(Vertx vertx, Context context) {
     super.init(vertx, context);
@@ -75,6 +93,21 @@ public class RestServerVerticle extends AbstractVerticle {
       initDispatcher(mainRouter);
       HttpServer httpServer = createHttpServer();
       httpServer.requestHandler(mainRouter::accept);
+      httpServer.connectionHandler(connection -> {
+        int connectedCount = connectedCounter.incrementAndGet();
+        int connectionLimit = DynamicPropertyFactory.getInstance()
+            .getIntProperty("servicecomb.rest.server.connection-limit", Integer.MAX_VALUE).get();
+        if (connectedCount > connectionLimit) {
+          connectedCounter.decrementAndGet();
+          connection.close();
+        } else {
+          EventManager.post(new ClientEvent(connection.remoteAddress().toString(),
+              EventType.Connected, ServerType.Rest, connectedCount));
+          connection.closeHandler(event -> EventManager.post(new ClientEvent(connection.remoteAddress().toString(),
+              EventType.Closed, ServerType.Rest, connectedCounter.decrementAndGet())));
+        }
+      });
+
       startListen(httpServer, startFuture);
     } catch (Throwable e) {
       // vert.x got some states that not print error and execute call back in VertexUtils.blockDeploy, we add a log our self.
diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java
index 8ec17c1..9a306b6 100644
--- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java
+++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java
@@ -17,6 +17,8 @@
 
 package org.apache.servicecomb.transport.rest.vertx;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.apache.servicecomb.core.Const;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.transport.AbstractTransport;
@@ -39,6 +41,12 @@ public class VertxRestTransport extends AbstractTransport {
 
   private RestTransportClient restClient;
 
+  private final AtomicInteger connectedCounter = new AtomicInteger(0);
+
+  public AtomicInteger getConnectedCounter() {
+    return connectedCounter;
+  }
+
   @Override
   public String getName() {
     return Const.RESTFUL;