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 2020/06/12 01:29:19 UTC
[servicecomb-java-chassis] branch master updated: [SCB-1998] make
invoke special endpoint simpler
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 e54e0ea [SCB-1998] make invoke special endpoint simpler
e54e0ea is described below
commit e54e0eae0629d7b5e66b534bd94aea002836ebeb
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Jun 11 15:58:19 2020 +0800
[SCB-1998] make invoke special endpoint simpler
---
.../handler/impl/SimpleLoadBalanceHandler.java | 5 ++
.../endpoint/EndpointContextRegister.java | 29 +++++++
.../core/invocation/endpoint/EndpointMapper.java | 39 +++++++++
.../invocation/endpoint/EndpointMapperFactory.java | 33 ++++++++
.../core/invocation/endpoint}/EndpointUtils.java | 27 +++++-
...cecomb.swagger.generator.SwaggerContextRegister | 18 ++++
...s.consumer.ConsumerContextArgumentMapperFactory | 18 ++++
.../handler/impl/TestSimpleLoadBalanceHandler.java | 2 +
.../core/invocation/endpoint/EndpointTest.java | 96 ++++++++++++++++++++++
.../invocation/endpoint}/EndpointUtilsTest.java | 4 +-
.../swagger/generator/SwaggerGeneratorFeature.java | 8 +-
11 files changed, 274 insertions(+), 5 deletions(-)
diff --git a/core/src/main/java/org/apache/servicecomb/core/handler/impl/SimpleLoadBalanceHandler.java b/core/src/main/java/org/apache/servicecomb/core/handler/impl/SimpleLoadBalanceHandler.java
index b301ba1..a3d4205 100644
--- a/core/src/main/java/org/apache/servicecomb/core/handler/impl/SimpleLoadBalanceHandler.java
+++ b/core/src/main/java/org/apache/servicecomb/core/handler/impl/SimpleLoadBalanceHandler.java
@@ -54,6 +54,11 @@ public class SimpleLoadBalanceHandler implements Handler {
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
+ if (invocation.getEndpoint() != null) {
+ invocation.next(asyncResp);
+ return;
+ }
+
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache endpointsVersionedCache = discoveryTree.discovery(context,
diff --git a/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointContextRegister.java b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointContextRegister.java
new file mode 100644
index 0000000..ff0da7e
--- /dev/null
+++ b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointContextRegister.java
@@ -0,0 +1,29 @@
+/*
+ * 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.core.invocation.endpoint;
+
+import java.lang.reflect.Type;
+
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.swagger.generator.SwaggerContextRegister;
+
+public class EndpointContextRegister implements SwaggerContextRegister {
+ @Override
+ public Type getContextType() {
+ return Endpoint.class;
+ }
+}
diff --git a/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointMapper.java b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointMapper.java
new file mode 100644
index 0000000..a792b9c
--- /dev/null
+++ b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointMapper.java
@@ -0,0 +1,39 @@
+/*
+ * 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.core.invocation.endpoint;
+
+import java.util.Map;
+
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
+import org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentMapper;
+
+public class EndpointMapper extends ConsumerArgumentMapper {
+ private final String invocationArgumentName;
+
+ public EndpointMapper(String invocationArgumentName) {
+ this.invocationArgumentName = invocationArgumentName;
+ }
+
+ @Override
+ public void invocationArgumentToSwaggerArguments(SwaggerInvocation swaggerInvocation,
+ Map<String, Object> swaggerArguments, Map<String, Object> invocationArguments) {
+ Invocation invocation = (Invocation) swaggerInvocation;
+ invocation.setEndpoint((Endpoint) invocationArguments.get(invocationArgumentName));
+ }
+}
diff --git a/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointMapperFactory.java b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointMapperFactory.java
new file mode 100644
index 0000000..ab1cebd
--- /dev/null
+++ b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointMapperFactory.java
@@ -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.
+ */
+package org.apache.servicecomb.core.invocation.endpoint;
+
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.swagger.invocation.arguments.ArgumentMapper;
+import org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory;
+
+public class EndpointMapperFactory implements ConsumerContextArgumentMapperFactory {
+ @Override
+ public Class<?> getContextClass() {
+ return Endpoint.class;
+ }
+
+ @Override
+ public ArgumentMapper create(String invocationArgumentName, String swaggerArgumentName) {
+ return new EndpointMapper(invocationArgumentName);
+ }
+}
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/EndpointUtils.java b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointUtils.java
similarity index 78%
rename from foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/EndpointUtils.java
rename to core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointUtils.java
index 46294e4..3bdf880 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/EndpointUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/invocation/endpoint/EndpointUtils.java
@@ -14,14 +14,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.servicecomb.foundation.common.net;
+package org.apache.servicecomb.core.invocation.endpoint;
import static com.google.common.collect.ImmutableMap.of;
+import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.core.Transport;
+import org.apache.servicecomb.core.exception.Exceptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* <pre>
@@ -36,6 +43,8 @@ import org.apache.http.client.utils.URIBuilder;
* </pre>
**/
public final class EndpointUtils {
+ private static final Logger LOGGER = LoggerFactory.getLogger(EndpointUtils.class);
+
private static final String HTTP = "http";
private static final String HTTPS = "https";
@@ -74,6 +83,22 @@ public final class EndpointUtils {
private EndpointUtils() {
}
+ /**
+ *
+ * @param uriEndpoint eg: rest://xxx?sslEnabled=true
+ * @return Endpoint object
+ */
+ public static Endpoint parse(String uriEndpoint) {
+ URI uri = URI.create(uriEndpoint);
+ Transport transport = SCBEngine.getInstance().getTransportManager().findTransport(uri.getScheme());
+ if (transport == null) {
+ LOGGER.error("not deployed transport, uri={}.", uriEndpoint);
+ throw Exceptions.genericConsumer("the endpoint's trnasport is not found.");
+ }
+
+ return new Endpoint(transport, uriEndpoint);
+ }
+
public static String formatFromUri(String inputUri) {
try {
return doFormatFromUri(inputUri);
diff --git a/core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.generator.SwaggerContextRegister b/core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.generator.SwaggerContextRegister
new file mode 100644
index 0000000..7fb23d9
--- /dev/null
+++ b/core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.generator.SwaggerContextRegister
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.core.invocation.endpoint.EndpointContextRegister
\ No newline at end of file
diff --git a/core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory b/core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory
new file mode 100644
index 0000000..094ac2c
--- /dev/null
+++ b/core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.core.invocation.endpoint.EndpointMapperFactory
\ No newline at end of file
diff --git a/core/src/test/java/org/apache/servicecomb/core/handler/impl/TestSimpleLoadBalanceHandler.java b/core/src/test/java/org/apache/servicecomb/core/handler/impl/TestSimpleLoadBalanceHandler.java
index 65bff50..bcd2eec 100644
--- a/core/src/test/java/org/apache/servicecomb/core/handler/impl/TestSimpleLoadBalanceHandler.java
+++ b/core/src/test/java/org/apache/servicecomb/core/handler/impl/TestSimpleLoadBalanceHandler.java
@@ -68,6 +68,8 @@ public class TestSimpleLoadBalanceHandler {
result = Collections.emptyList();
invocation.getConfigTransportName();
result = "";
+ invocation.getEndpoint();
+ result = null;
}
};
diff --git a/core/src/test/java/org/apache/servicecomb/core/invocation/endpoint/EndpointTest.java b/core/src/test/java/org/apache/servicecomb/core/invocation/endpoint/EndpointTest.java
new file mode 100644
index 0000000..aa9c3a8
--- /dev/null
+++ b/core/src/test/java/org/apache/servicecomb/core/invocation/endpoint/EndpointTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.core.invocation.endpoint;
+
+import static com.google.common.collect.ImmutableMap.of;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Map;
+
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.Transport;
+import org.apache.servicecomb.foundation.common.Holder;
+import org.apache.servicecomb.swagger.SwaggerUtils;
+import org.apache.servicecomb.swagger.engine.SwaggerConsumer;
+import org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation;
+import org.apache.servicecomb.swagger.engine.SwaggerEnvironment;
+import org.apache.servicecomb.swagger.generator.SwaggerGenerator;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import io.swagger.models.Swagger;
+
+public class EndpointTest {
+ public interface TestSchema {
+ void say(Endpoint endpoint);
+ }
+
+ @Test
+ void should_ignore_endpoint_when_generate_swagger() {
+ SwaggerGenerator generator = SwaggerGenerator.create(TestSchema.class);
+ generator.getSwaggerGeneratorFeature()
+ .setExtJavaInterfaceInVendor(false)
+ .setExtJavaClassInVendor(false);
+ Swagger swagger = generator.generate();
+
+ assertThat(SwaggerUtils.swaggerToString(swagger))
+ .isEqualTo("---\n"
+ + "swagger: \"2.0\"\n"
+ + "info:\n"
+ + " version: \"1.0.0\"\n"
+ + " title: \"swagger definition for org.apache.servicecomb.core.invocation.endpoint.EndpointTest$TestSchema\"\n"
+ + "basePath: \"/TestSchema\"\n"
+ + "consumes:\n"
+ + "- \"application/json\"\n"
+ + "produces:\n"
+ + "- \"application/json\"\n"
+ + "paths:\n"
+ + " /say:\n"
+ + " post:\n"
+ + " operationId: \"say\"\n"
+ + " parameters: []\n"
+ + " responses:\n"
+ + " \"200\":\n"
+ + " description: \"response of 200\"\n");
+ }
+
+ @Test
+ void should_set_endpoint_to_invocation_when_map_arguments() {
+ SwaggerEnvironment environment = new SwaggerEnvironment();
+ SwaggerConsumer consumer = environment
+ .createConsumer(TestSchema.class, SwaggerGenerator.generate(TestSchema.class));
+ SwaggerConsumerOperation operation = consumer.findOperation("say");
+
+ Endpoint endpoint = new Endpoint(Mockito.mock(Transport.class), null);
+ Invocation invocation = Mockito.mock(Invocation.class);
+ Holder<Object> holder = new Holder<>();
+ Mockito
+ .doAnswer(invocationOnMock -> {
+ holder.value = invocationOnMock.getArguments()[0];
+ return null;
+ })
+ .when(invocation)
+ .setEndpoint(Mockito.any());
+
+ Map<String, Object> argsMap = of("endpoint", endpoint);
+ operation.getArgumentsMapper().invocationArgumentToSwaggerArguments(invocation, argsMap);
+
+ assertThat(holder.value).isSameAs(endpoint);
+ }
+}
\ No newline at end of file
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/EndpointUtilsTest.java b/core/src/test/java/org/apache/servicecomb/core/invocation/endpoint/EndpointUtilsTest.java
similarity index 95%
rename from foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/EndpointUtilsTest.java
rename to core/src/test/java/org/apache/servicecomb/core/invocation/endpoint/EndpointUtilsTest.java
index 52dbef4..2338cca 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/EndpointUtilsTest.java
+++ b/core/src/test/java/org/apache/servicecomb/core/invocation/endpoint/EndpointUtilsTest.java
@@ -15,9 +15,9 @@
* limitations under the License.
*/
-package org.apache.servicecomb.foundation.common.net;
+package org.apache.servicecomb.core.invocation.endpoint;
-import static org.apache.servicecomb.foundation.common.net.EndpointUtils.formatFromUri;
+import static org.apache.servicecomb.core.invocation.endpoint.EndpointUtils.formatFromUri;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Nested;
diff --git a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/SwaggerGeneratorFeature.java b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/SwaggerGeneratorFeature.java
index c7dbe0e..e84a57f 100644
--- a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/SwaggerGeneratorFeature.java
+++ b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/SwaggerGeneratorFeature.java
@@ -48,15 +48,19 @@ public class SwaggerGeneratorFeature {
return extJavaClassInVendor;
}
- public void setExtJavaClassInVendor(boolean extJavaClassInVendor) {
+ public SwaggerGeneratorFeature setExtJavaClassInVendor(boolean extJavaClassInVendor) {
this.extJavaClassInVendor = extJavaClassInVendor;
+
+ return this;
}
public boolean isExtJavaInterfaceInVendor() {
return extJavaInterfaceInVendor;
}
- public void setExtJavaInterfaceInVendor(boolean extJavaInterfaceInVendor) {
+ public SwaggerGeneratorFeature setExtJavaInterfaceInVendor(boolean extJavaInterfaceInVendor) {
this.extJavaInterfaceInVendor = extJavaInterfaceInVendor;
+
+ return this;
}
}