WillemJiang closed pull request #558: [SCB-314] Support custom rest transport server compress and max header size URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/558
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 f2fc0d77a..c49fc05f0 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 @@ 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,20 @@ public static void run() { 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 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!", + entityCompress.getBody()); + // if server response is compressed, the content-length header will be removed , so can't check this. + // the transfer-encoding header will be missing when the server is setted to not compressed + if (entityCompress.getHeaders().get("transfer-encoding") != null) { + TestMgr.check("chunked", entityCompress.getHeaders().get("transfer-encoding").get(0)); + } //0.5.0 later version metrics integration test try { diff --git a/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml b/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml index 13cc6060e..72d65bf2b 100644 --- a/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml +++ b/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml @@ -35,7 +35,10 @@ cse: interval: 90 watch: false autodiscovery: true - + rest: + client: + connection: + compression: true references: version-rule: 0+ handler: 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 103e37e76..5bbd00700 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 @@ private String _fileUpload(MultipartFile file1, Part file2) { 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 String sayHi(@PathVariable(name = "name") String name) { 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 OutputModelForTestIgnore testModelWithIgnoreField(@RequestBody InputModel 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 d7a69d826..34b582a8e 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-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClient.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClient.java index b326a09d3..1fed06523 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClient.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClient.java @@ -55,6 +55,7 @@ private static HttpClientOptions createHttpClientOptions() { httpClientOptions.setMaxPoolSize(TransportClientConfig.getConnectionMaxPoolSize()); httpClientOptions.setIdleTimeout(TransportClientConfig.getConnectionIdleTimeoutInSeconds()); httpClientOptions.setKeepAlive(TransportClientConfig.getConnectionKeepAlive()); + httpClientOptions.setTryUseCompression(TransportClientConfig.getConnectionCompression()); VertxTLSBuilder.buildHttpClientOptions(SSL_KEY, httpClientOptions); return httpClientOptions; diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportClientConfig.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportClientConfig.java index f46d9b070..980326710 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportClientConfig.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/TransportClientConfig.java @@ -40,4 +40,8 @@ public static int getConnectionIdleTimeoutInSeconds() { public static boolean getConnectionKeepAlive() { return DynamicPropertyFactory.getInstance().getBooleanProperty("cse.rest.client.connection.keepAlive", true).get(); } + + public static boolean getConnectionCompression() { + return DynamicPropertyFactory.getInstance().getBooleanProperty("cse.rest.client.connection.compression", false).get(); + } } diff --git a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestTransportClientConfig.java b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestTransportClientConfig.java index 343d64dc4..bbc2e753a 100644 --- a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestTransportClientConfig.java +++ b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestTransportClientConfig.java @@ -40,4 +40,9 @@ public void getConnectionIdleTimeoutInSeconds() { public void getConnectionKeepAlive() { Assert.assertTrue(TransportClientConfig.getConnectionKeepAlive()); } + + @Test + public void getConnectionCompression() { + Assert.assertFalse(TransportClientConfig.getConnectionCompression()); + } } 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 d011e8a83..73eab866a 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 @@ private HttpServerOptions createDefaultHttpServerOptions() { 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 e723ef093..75bcc98af 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.DynamicPropertyFactory; import com.netflix.config.DynamicStringProperty; +import io.vertx.core.http.HttpServerOptions; + public final class TransportConfig { private TransportConfig() { } @@ -42,4 +44,16 @@ public static int getConnectionIdleTimeoutInSeconds() { .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 514fb1398..e63dcd5b8 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 void testGetThreadCountNormal() { 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"); + } } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services