[ https://issues.apache.org/jira/browse/SCB-482?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16471386#comment-16471386 ]
ASF GitHub Bot commented on SCB-482: ------------------------------------ liubao68 closed pull request #648: [SCB-482] Http2 support for java-chassis URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/648 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-pojo/pojo-server/src/main/resources/microservice.yaml b/demo/demo-pojo/pojo-server/src/main/resources/microservice.yaml index 9ca4af41b..e70518320 100644 --- a/demo/demo-pojo/pojo-server/src/main/resources/microservice.yaml +++ b/demo/demo-pojo/pojo-server/src/main/resources/microservice.yaml @@ -24,7 +24,7 @@ servicecomb: registry: address: http://127.0.0.1:30100 rest: - address: 0.0.0.0:8080 + address: 0.0.0.0:8080?protocol=http2 highway: address: 0.0.0.0:7070 #executors: diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/URIEndpointObject.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/URIEndpointObject.java index 8ec116dd9..4c46e14f4 100644 --- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/URIEndpointObject.java +++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/URIEndpointObject.java @@ -33,8 +33,14 @@ public class URIEndpointObject extends IpPort { private static final String SSL_ENABLED_KEY = "sslEnabled"; + private static final String PROTOCOL_KEY = "protocol"; + + private static final String HTTP2 = "http2"; + private boolean sslEnabled; + private boolean http2Enabled; + private Map<String, List<String>> querys; public URIEndpointObject(String endpoint) { @@ -47,6 +53,10 @@ public URIEndpointObject(String endpoint) { setPort(uri.getPort()); querys = splitQuery(uri); sslEnabled = Boolean.parseBoolean(getFirst(SSL_ENABLED_KEY)); + String httpversion = getFirst(PROTOCOL_KEY); + if (httpversion != null && httpversion.equals(HTTP2)) { + http2Enabled = true; + } } public static Map<String, List<String>> splitQuery(URI uri) { @@ -65,6 +75,10 @@ public boolean isSslEnabled() { return sslEnabled; } + public boolean isHttp2Enabled() { + return http2Enabled; + } + public List<String> getQuery(String key) { return querys.get(key); } diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestURIEndpointObject.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestURIEndpointObject.java index 9590687ce..5f7f4d418 100644 --- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestURIEndpointObject.java +++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestURIEndpointObject.java @@ -41,6 +41,13 @@ public void testRestEndpointObject() { Assert.assertEquals(8080, obj.getPort()); Assert.assertTrue(obj.isSslEnabled()); Assert.assertNull(obj.getFirst("notExist")); + + obj = new URIEndpointObject("http://127.0.2.0:8080?sslEnabled=true&protocol=http2"); + Assert.assertEquals("127.0.2.0", obj.getHostOrIp()); + Assert.assertEquals(8080, obj.getPort()); + Assert.assertTrue(obj.isSslEnabled()); + Assert.assertTrue(obj.isHttp2Enabled()); + Assert.assertNull(obj.getFirst("notExist")); } @Test(expected = IllegalArgumentException.class) 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 8c4381417..7b04ecb93 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 @@ -21,6 +21,7 @@ import org.apache.servicecomb.common.rest.filter.HttpClientFilter; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.foundation.common.net.URIEndpointObject; import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; import org.apache.servicecomb.foundation.vertx.VertxTLSBuilder; import org.apache.servicecomb.foundation.vertx.VertxUtils; @@ -36,6 +37,7 @@ import io.vertx.core.DeploymentOptions; import io.vertx.core.Vertx; import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.HttpVersion; public class RestTransportClient { private static final Logger LOGGER = LoggerFactory.getLogger(RestTransportClient.class); @@ -44,8 +46,11 @@ protected ClientPoolManager<HttpClientWithContext> clientMgr; + private ClientPoolManager<HttpClientWithContext> clientMgrHttp2; + private List<HttpClientFilter> httpClientFilters; + public ClientPoolManager<HttpClientWithContext> getClientMgr() { return clientMgr; } @@ -56,9 +61,20 @@ public void init(Vertx vertx) throws Exception { HttpClientOptions httpClientOptions = createHttpClientOptions(); clientMgr = new ClientPoolManager<>(vertx, new HttpClientPoolFactory(httpClientOptions)); + + HttpClientOptions httpClientOptionshttp2 = createHttpClientOptions(); + httpClientOptionshttp2.setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2); + httpClientOptionshttp2.setHttp2ClearTextUpgrade(false); + + clientMgrHttp2 = new ClientPoolManager<>(vertx, new HttpClientPoolFactory(httpClientOptionshttp2)); + DeploymentOptions deployOptions = VertxUtils.createClientDeployOptions(clientMgr, TransportClientConfig.getThreadCount()); VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions); + + DeploymentOptions deployOptionshttp2 = VertxUtils.createClientDeployOptions(clientMgrHttp2, + TransportClientConfig.getThreadCount()); + VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptionshttp2); } private static HttpClientOptions createHttpClientOptions() { @@ -73,8 +89,18 @@ private static HttpClientOptions createHttpClientOptions() { } public void send(Invocation invocation, AsyncResponse asyncResp) { - HttpClientWithContext httpClientWithContext = findHttpClientPool(invocation); + URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress(); + HttpClientWithContext httpClientWithContext; + + if (endpoint.isHttp2Enabled()) { + httpClientWithContext = findHttp2ClientPool(invocation); + + } else { + httpClientWithContext = findHttpClientPool(invocation); + } + RestClientInvocation restClientInvocation = new RestClientInvocation(httpClientWithContext, httpClientFilters); + try { restClientInvocation.invoke(invocation, asyncResp); } catch (Throwable e) { @@ -86,4 +112,8 @@ public void send(Invocation invocation, AsyncResponse asyncResp) { protected HttpClientWithContext findHttpClientPool(Invocation invocation) { return clientMgr.findClientPool(invocation.isSync()); } + + public HttpClientWithContext findHttp2ClientPool(Invocation invocation) { + return clientMgrHttp2.findClientPool(invocation.isSync()); + } } diff --git a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestRestTransportClient.java b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestRestTransportClient.java index d133105e2..c008adfed 100644 --- a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestRestTransportClient.java +++ b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/TestRestTransportClient.java @@ -17,10 +17,14 @@ package org.apache.servicecomb.transport.rest.client; +import java.lang.reflect.Field; + import org.apache.servicecomb.common.rest.RestConst; import org.apache.servicecomb.common.rest.definition.RestOperationMeta; +import org.apache.servicecomb.core.Endpoint; import org.apache.servicecomb.core.Invocation; import org.apache.servicecomb.core.definition.OperationMeta; +import org.apache.servicecomb.foundation.common.net.URIEndpointObject; import org.apache.servicecomb.foundation.vertx.VertxUtils; import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager; import org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext; @@ -35,6 +39,7 @@ import io.vertx.core.DeploymentOptions; import io.vertx.core.Vertx; import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.impl.HttpClientImpl; import mockit.Deencapsulation; import mockit.Mock; import mockit.MockUp; @@ -52,6 +57,17 @@ RestOperationMeta swaggerRestOperation = Mockito.mock(RestOperationMeta.class); + URIEndpointObject uriEndpointObject = Mockito.mock(URIEndpointObject.class); + + Endpoint endPoint = Mockito.mock(Endpoint.class); + + HttpClientWithContext httpClientWithContext = Mockito.mock(HttpClientWithContext.class); + + HttpClientImpl httpClient = Mockito.mock(HttpClientImpl.class); + + HttpClientOptions httpClientOptions = Mockito.mock(HttpClientOptions.class); + + @Before public void setUp() throws Exception { instance = new RestTransportClient(); @@ -97,6 +113,43 @@ public void testRestTransportClientException() { Assert.assertFalse(status); } + + @Test + public void testRestTransportClientHttp2(@Mocked Vertx vertx, @Mocked VertxUtils utils) + throws Exception { + boolean status = true; + Mockito.when(invocation.getEndpoint()).thenReturn(endPoint); + Mockito.when(invocation.isSync()).thenReturn(true); + Mockito.when(endPoint.getAddress()).thenReturn(uriEndpointObject); + Mockito.when(uriEndpointObject.isHttp2Enabled()).thenReturn(true); + Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta); + Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(operationMeta); + Mockito.when(httpClientWithContext.getHttpClient()).thenReturn(httpClient); + Mockito.when(httpClient.getOptions()).thenReturn(httpClientOptions); + + instance.init(vertx); + Field clientMgrHttp2Field = instance.getClass().getDeclaredField("clientMgrHttp2"); + clientMgrHttp2Field.setAccessible(true); + + + ClientPoolManager<HttpClientWithContext> client = new ClientPoolManager<HttpClientWithContext>(vertx, null) { + @Mock + public HttpClientWithContext findClientPool(boolean sync) { + return httpClientWithContext; + } + }; + + clientMgrHttp2Field.set(instance, client); + + try { + instance.send(invocation, asyncResp); + } catch (Exception e) { + status = false; + } + + Assert.assertTrue(status); + } + @Test public void testCreateHttpClientOptions() { HttpClientOptions obj = (HttpClientOptions) Deencapsulation.invoke(instance, "createHttpClientOptions"); 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 3eb679c56..a3bd7e823 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 @@ -133,7 +133,9 @@ private HttpServerOptions createDefaultHttpServerOptions() { serverOptions.setIdleTimeout(TransportConfig.getConnectionIdleTimeoutInSeconds()); serverOptions.setCompressionSupported(TransportConfig.getCompressed()); serverOptions.setMaxHeaderSize(TransportConfig.getMaxHeaderSize()); - + if (endpointObject.isHttp2Enabled()) { + serverOptions.setUseAlpn(true); + } if (endpointObject.isSslEnabled()) { SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(SSL_KEY, null); diff --git a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java index ebd1c3ee4..146c5616e 100644 --- a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java +++ b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java @@ -109,6 +109,38 @@ public void testRestServerVerticleWithRouterSSL(@Mocked Transport transport, @Mo server.start(startFuture); } + @Test + public void testRestServerVerticleWithHttp2(@Mocked Transport transport, @Mocked Vertx vertx, + @Mocked Context context, + @Mocked JsonObject jsonObject, @Mocked Future<Void> startFuture) throws Exception { + URIEndpointObject endpointObject = new URIEndpointObject("http://127.0.0.1:8080?protocol=http2"); + new Expectations() { + { + transport.parseAddress("http://127.0.0.1:8080?protocol=http2"); + result = endpointObject; + } + }; + Endpoint endpiont = new Endpoint(transport, "http://127.0.0.1:8080?protocol=http2"); + + new Expectations() { + { + context.config(); + result = jsonObject; + jsonObject.getValue(AbstractTransport.ENDPOINT_KEY); + result = endpiont; + } + }; + RestServerVerticle server = new RestServerVerticle(); + boolean status = false; + try { + server.init(vertx, context); + server.start(startFuture); + } catch (Exception e) { + status = true; + } + Assert.assertFalse(status); + } + @Test public void testStartFutureAddressEmpty() { boolean status = false; ---------------------------------------------------------------- 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 > Http2 support for java chassis > ------------------------------ > > Key: SCB-482 > URL: https://issues.apache.org/jira/browse/SCB-482 > Project: Apache ServiceComb > Issue Type: New Feature > Components: Java-Chassis > Reporter: Rajadeepan > Priority: Minor > > Http2 support for java chassis. > Only Application for Rest Vertex and not applicable for servlet and highway. -- This message was sent by Atlassian JIRA (v7.6.3#76005)