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/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new bed01ee [SCB-2150] allowed modify host before create
HttpClientRequest (#2107)
bed01ee is described below
commit bed01ee93e0f6139acaefb58b0b0bef62bd754c8
Author: wujimin <[email protected]>
AuthorDate: Tue Dec 8 11:28:17 2020 +0800
[SCB-2150] allowed modify host before create HttpClientRequest (#2107)
---
.../rest/client/HttpClientRequestFactory.java | 48 ++++++++++++++++++++++
.../client/RestClientTransportContextFactory.java | 12 +++++-
.../RestClientTransportContextFactoryTest.java | 14 +++++++
3 files changed, 72 insertions(+), 2 deletions(-)
diff --git
a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/HttpClientRequestFactory.java
b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/HttpClientRequestFactory.java
new file mode 100644
index 0000000..2e4283d
--- /dev/null
+++
b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/HttpClientRequestFactory.java
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+package org.apache.servicecomb.transport.rest.client;
+
+import org.apache.servicecomb.core.Invocation;
+
+import io.vertx.core.http.HttpClient;
+import io.vertx.core.http.HttpClientRequest;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.http.RequestOptions;
+
+/**
+ * some service has special domain name rule, eg: k8s<br>
+ * assume k8s domain name is https://k8s.com:1234, and clusterId is my-id<br>
+ * then must send request to https://my-id.k8s.com:1234<br>
+ * <br>
+ * this interface allowed to modify host by invocation argument, eg:<br>
+ * <pre>
+ * {@code
+ * HttpClientRequest create(Invocation invocation, HttpClient httpClient,
HttpMethod method, RequestOptions options) {
+ * if ("k8s".equals(invocation.getMicroserviceName())) {
+ * options.setHost(invocation.getSwaggerArgument("clusterId") + "." +
options.getHost());
+ * }
+ *
+ * return httpClient.request(method, options);
+ * }
+ * }
+ * </pre>
+ */
+public interface HttpClientRequestFactory {
+ HttpClientRequestFactory DEFAULT = (invocation, httpClient, method, options)
-> httpClient.request(method, options);
+
+ HttpClientRequest create(Invocation invocation, HttpClient httpClient,
HttpMethod method, RequestOptions options);
+}
\ No newline at end of file
diff --git
a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactory.java
b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactory.java
index f8ba5b4..8e7162e 100644
---
a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactory.java
+++
b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactory.java
@@ -41,12 +41,20 @@ import io.vertx.core.http.RequestOptions;
public class RestClientTransportContextFactory {
private BoundaryFactory boundaryFactory = BoundaryFactory.DEFAULT;
+ private HttpClientRequestFactory httpClientRequestFactory =
HttpClientRequestFactory.DEFAULT;
+
@Autowired(required = false)
public RestClientTransportContextFactory setBoundaryFactory(BoundaryFactory
boundaryFactory) {
this.boundaryFactory = boundaryFactory;
return this;
}
+ @Autowired(required = false)
+ public RestClientTransportContextFactory
setHttpClientRequestFactory(HttpClientRequestFactory factory) {
+ this.httpClientRequestFactory = factory;
+ return this;
+ }
+
public RestClientTransportContext create(Invocation invocation) {
try {
return doCreate(invocation);
@@ -66,7 +74,7 @@ public class RestClientTransportContextFactory {
httpClientRequest,
boundaryFactory);
}
-
+
protected HttpClientWithContext findHttpClientPool(Invocation invocation) {
URIEndpointObject endpoint = (URIEndpointObject)
invocation.getEndpoint().getAddress();
if (endpoint.isHttp2Enabled()) {
@@ -85,7 +93,7 @@ public class RestClientTransportContextFactory {
.setSsl(endpoint.isSslEnabled())
.setURI(createRequestPath(invocation, restOperationMeta));
HttpMethod method = HttpMethod.valueOf(restOperationMeta.getHttpMethod());
- return httpClient.request(method, requestOptions);
+ return httpClientRequestFactory.create(invocation, httpClient, method,
requestOptions);
}
protected String createRequestPath(Invocation invocation, RestOperationMeta
restOperationMeta) throws Exception {
diff --git
a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactoryTest.java
b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactoryTest.java
index e2c3c64..ad2c9fd 100644
---
a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactoryTest.java
+++
b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/RestClientTransportContextFactoryTest.java
@@ -96,4 +96,18 @@ class RestClientTransportContextFactoryTest extends
RestClientTestBase {
assertThat(transportContext.getLocalAddress()).isEqualTo("not connected");
}
+
+ @Test
+ void should_allowed_modify_host() {
+ factory.setHttpClientRequestFactory((invocation, httpClient, method,
options) -> {
+ options.setHost(invocation.getSwaggerArgument("clusterId") + "." +
options.getHost());
+ return httpClient.request(method, options);
+ });
+
+ init(ImmutableMap.of("clusterId", "my-id"), true);
+
+ assertThat(absoluteURI()).isEqualTo("https://my-id.localhost:1234/query");
+
+ factory.setHttpClientRequestFactory(HttpClientRequestFactory.DEFAULT);
+ }
}
\ No newline at end of file