You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/02/14 01:24:48 UTC
[incubator-servicecomb-java-chassis] 01/02: [SCB-314] Support
custom rest transport server compress and max header size
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit f9dd383f37564da7bd8e230fead7fde3c27e8b38
Author: jeho0815 <je...@email.com>
AuthorDate: Mon Feb 12 15:13:13 2018 +0800
[SCB-314] Support custom rest transport server compress and max header size
---
.../demo/springmvc/client/SpringmvcClient.java | 12 ++++++++++++
.../demo/springmvc/server/CodeFirstSpringmvc.java | 16 ++++++++++++++--
.../src/main/resources/microservice.yaml | 2 ++
.../transport/rest/vertx/RestServerVerticle.java | 2 ++
.../transport/rest/vertx/TransportConfig.java | 14 ++++++++++++++
.../transport/rest/vertx/TestTransportConfig.java | 10 ++++++++++
6 files changed, 54 insertions(+), 2 deletions(-)
diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
index f2fc0d7..b3e0f85 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.demo.springmvc.client;
import java.util.HashMap;
import java.util.Map;
+
import org.apache.servicecomb.core.CseContext;
import org.apache.servicecomb.demo.DemoConst;
import org.apache.servicecomb.demo.TestMgr;
@@ -85,6 +86,17 @@ public class SpringmvcClient {
testController();
}
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("Accept-Encoding", "gzip");
+ HttpEntity<String> entity = new HttpEntity<>(headers);
+ ResponseEntity<String> entityCompress =
+ restTemplate.exchange(prefix
+ + "/codeFirstSpringmvc/sayhi/compressed/{name}/v2", HttpMethod.GET, entity, String.class, "Test");
+ TestMgr.check(
+ "Test sayhi compressed:This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This [...]
+ entityCompress.getBody());
+ TestMgr.check("gzip", entityCompress.getHeaders().get("content-encoding"));
+ TestMgr.check("75", entityCompress.getHeaders().get("content-length"));
//0.5.0 later version metrics integration test
try {
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
index 103e37e..5bbd007 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
@@ -85,7 +85,7 @@ public class CodeFirstSpringmvc {
String content1 = IOUtils.toString(is1);
String content2 = IOUtils.toString(is2);
return String.format("%s:%s:%s\n"
- + "%s:%s:%s",
+ + "%s:%s:%s",
file1.getOriginalFilename(),
file1.getContentType(),
content1,
@@ -220,6 +220,18 @@ public class CodeFirstSpringmvc {
return name + " sayhi";
}
+ @RequestMapping(path = "/sayhi/compressed/{name}/v2", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
+ public String sayHiForCompressed(@PathVariable(name = "name") String name) {
+ String bigText =
+ "This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,"
+ + "This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,"
+ + "This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,"
+ + "This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,"
+ + "This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,"
+ + "This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text,This is a big text!";
+ return name + " sayhi compressed:" + bigText;
+ }
+
@RequestMapping(path = "/sayhi/{name}/v2", method = RequestMethod.PUT)
public String sayHi2(@PathVariable(name = "name") String name) {
return name + " sayhi 2";
@@ -298,7 +310,7 @@ public class CodeFirstSpringmvc {
return new OutputModelForTestIgnore("output_id", input.getInputId(), input.getContent(), input.getInputObject(),
input.getInputJsonObject(), input.getInputIgnoreInterface(),
new Person("outputSomeone"), new JsonObject("{\"OutputJsonKey\" : \"OutputJsonValue\"}"), () -> {
- });
+ });
}
@SuppressWarnings("unchecked")
diff --git a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
index d7a69d8..34b582a 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
@@ -46,6 +46,8 @@ cse:
autodiscovery: true
rest:
address: 0.0.0.0:8080?sslEnabled=true
+ server:
+ compression: true
highway:
address: 0.0.0.0:7070?sslEnabled=true
handler:
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 d011e8a..73eab86 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
@@ -124,6 +124,8 @@ public class RestServerVerticle extends AbstractVerticle {
HttpServerOptions serverOptions = new HttpServerOptions();
serverOptions.setUsePooledBuffers(true);
serverOptions.setIdleTimeout(TransportConfig.getConnectionIdleTimeoutInSeconds());
+ serverOptions.setCompressionSupported(TransportConfig.getCompressed());
+ serverOptions.setMaxHeaderSize(TransportConfig.getMaxHeaderSize());
if (endpointObject.isSslEnabled()) {
SSLOptionFactory factory =
diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/TransportConfig.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/TransportConfig.java
index e723ef0..75bcc98 100644
--- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/TransportConfig.java
+++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/TransportConfig.java
@@ -21,6 +21,8 @@ import com.netflix.config.DynamicIntProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
+import io.vertx.core.http.HttpServerOptions;
+
public final class TransportConfig {
private TransportConfig() {
}
@@ -42,4 +44,16 @@ public final class TransportConfig {
.getIntProperty("cse.rest.server.connection.idleTimeoutInSeconds", 60)
.get();
}
+
+ public static boolean getCompressed() {
+ return DynamicPropertyFactory.getInstance()
+ .getBooleanProperty("cse.rest.server.compression", false)
+ .get();
+ }
+
+ public static int getMaxHeaderSize() {
+ return DynamicPropertyFactory.getInstance()
+ .getIntProperty("cse.rest.server.maxHeaderSize", HttpServerOptions.DEFAULT_MAX_HEADER_SIZE)
+ .get();
+ }
}
diff --git a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestTransportConfig.java b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestTransportConfig.java
index 514fb13..e63dcd5 100644
--- a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestTransportConfig.java
+++ b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestTransportConfig.java
@@ -63,4 +63,14 @@ public class TestTransportConfig {
Assert.assertEquals(10, TransportConfig.getThreadCount());
config.clearProperty("cse.rest.server.thread-count");
}
+
+ @Test
+ public void testGetCompressedAndHeaderSize() {
+ config.addProperty("cse.rest.server.compression", true);
+ Assert.assertEquals(true, TransportConfig.getCompressed());
+ config.addProperty("cse.rest.server.maxHeaderSize", 2048);
+ Assert.assertEquals(2048, TransportConfig.getMaxHeaderSize());
+ config.clearProperty("cse.rest.server.compression");
+ config.clearProperty("cse.rest.server.maxHeaderSize");
+ }
}
--
To stop receiving notification emails like this one, please contact
ningjiang@apache.org.