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/incubator-servicecomb-java-chassis.git
commit a671fcd96a5f7e88aed41a93b501d79391c8275f Author: wujimin <[email protected]> AuthorDate: Thu Aug 30 02:27:02 2018 +0800 [SCB-859] add traceId integration test --- .../it-common/src/main/resources/logback.xml | 2 + .../org/apache/servicecomb/it/ConsumerMain.java | 4 ++ .../java/org/apache/servicecomb/it/Consumers.java | 80 +++++++++++++++++++++ .../it/extend/engine/GateRestTemplate.java | 82 ++++++++++++++++++++++ .../it/extend/engine/ITSCBRestTemplate.java | 46 ++++++++++++ .../it/extend/engine/ITUriTemplateHandler.java | 44 ++++++++++++ .../apache/servicecomb/it/junit/ITJUnitUtils.java | 36 ++++++++++ .../apache/servicecomb/it/testcase/TestTrace.java | 64 +++++++++++++++++ .../servicecomb/it/testcase/TestTraceEdge.java | 41 +++++++++++ .../it-edge/src/main/resources/microservice.yaml | 4 +- .../apache/servicecomb/it/schema/TraceSchema.java | 47 +++++++++++++ 11 files changed, 448 insertions(+), 2 deletions(-) diff --git a/integration-tests/it-common/src/main/resources/logback.xml b/integration-tests/it-common/src/main/resources/logback.xml index d753514..6256690 100644 --- a/integration-tests/it-common/src/main/resources/logback.xml +++ b/integration-tests/it-common/src/main/resources/logback.xml @@ -23,6 +23,8 @@ <pattern>%d [%level] [%thread] - %msg (%F:%L\)%n</pattern> </encoder> </appender> + <logger name="org.apache.servicecomb.core.definition.schema.ProducerSchemaFactory" level="OFF"> + </logger> <root level="INFO"> <appender-ref ref="STDOUT"/> </root> diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java index 514b870..1086801 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java @@ -24,6 +24,8 @@ import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.it.deploy.Deploys; import org.apache.servicecomb.it.junit.ITJUnitUtils; +import org.apache.servicecomb.it.testcase.TestTrace; +import org.apache.servicecomb.it.testcase.TestTraceEdge; import org.apache.servicecomb.it.testcase.base.TestDataTypeJaxrs; import org.apache.servicecomb.it.testcase.base.TestDataTypePojo; import org.apache.servicecomb.it.testcase.base.TestDataTypeSpringmvc; @@ -105,6 +107,8 @@ public class ConsumerMain { ITJUnitUtils.addParent("standalone"); testDataType(); + ITJUnitUtils.runWithHighwayAndRest(TestTrace.class); + ITJUnitUtils.run(TestTraceEdge.class); ITJUnitUtils.getParents().pop(); deploys.getBaseProducer().stop(); diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java new file mode 100644 index 0000000..4f3f42b --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java @@ -0,0 +1,80 @@ +/* + * 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.it; + +import org.apache.servicecomb.it.extend.engine.GateRestTemplate; +import org.apache.servicecomb.it.extend.engine.ITInvoker; +import org.apache.servicecomb.it.extend.engine.ITSCBRestTemplate; +import org.springframework.web.client.RestTemplate; + +public class Consumers<INTF> { + private String producerName = "it-producer"; + + private String schemaId; + + private Class<INTF> intfCls; + + private ITSCBRestTemplate scbRestTemplate; + + private RestTemplate edgeRestTemplate; + + private RestTemplate zuulRestTemplate; + + private INTF intf; + + private String transport; + + public Consumers(String schemaId, Class<INTF> intfCls) { + this.schemaId = schemaId; + this.intfCls = intfCls; + + scbRestTemplate = new ITSCBRestTemplate(producerName, schemaId); + edgeRestTemplate = GateRestTemplate.createEdgeRestTemplate(producerName, schemaId); + zuulRestTemplate = null;// GateRestTemplate.createZuulRestTemplate(producerName, schemaId); + } + + public void init(String transport) { + this.transport = transport; + intf = ITInvoker.createProxy(producerName, schemaId, transport, intfCls); + + scbRestTemplate.setTransport(transport); + } + + public String getSchemaId() { + return schemaId; + } + + public INTF getIntf() { + return intf; + } + + public RestTemplate getSCBRestTemplate() { + return scbRestTemplate; + } + + public RestTemplate getEdgeRestTemplate() { + return edgeRestTemplate; + } + + public RestTemplate getZuulRestTemplate() { + return zuulRestTemplate; + } + + public String getTransport() { + return transport; + } +} \ No newline at end of file diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/GateRestTemplate.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/GateRestTemplate.java new file mode 100644 index 0000000..7c79a2d --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/GateRestTemplate.java @@ -0,0 +1,82 @@ +/* + * 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.it.extend.engine; + +import java.util.Arrays; + +import org.apache.servicecomb.core.definition.MicroserviceVersionMeta; +import org.apache.servicecomb.core.definition.SchemaMeta; +import org.apache.servicecomb.foundation.common.net.URIEndpointObject; +import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; +import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule; +import org.apache.servicecomb.serviceregistry.definition.DefinitionConst; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +public class GateRestTemplate extends RestTemplate { + public static RestTemplate createEdgeRestTemplate(String schemaId) { + return createEdgeRestTemplate("it-producer", schemaId); + } + + public static RestTemplate createEdgeRestTemplate(String producerName, String schemaId) { + return new GateRestTemplate("it-edge", schemaId); + } + + public static RestTemplate createZuulRestTemplate(String schemaId) { + return createZuulRestTemplate("it-producer", schemaId); + } + + public static RestTemplate createZuulRestTemplate(String producerName, String schemaId) { + return new GateRestTemplate("it-zuul", schemaId); + } + + public GateRestTemplate(String gateName, String schemaId) { + this(gateName, "it-producer", schemaId); + } + + public GateRestTemplate(String gateName, String producerName, String schemaId) { + MicroserviceVersionRule microserviceVersionRule = RegistryUtils.getServiceRegistry().getAppManager() + .getOrCreateMicroserviceVersionRule(RegistryUtils.getAppId(), gateName, + DefinitionConst.VERSION_RULE_ALL); + MicroserviceInstance microserviceInstance = microserviceVersionRule.getInstances().values().stream().findFirst() + .get(); + URIEndpointObject edgeAddress = new URIEndpointObject(microserviceInstance.getEndpoints().get(0)); + + String urlSchema = "http"; + if (edgeAddress.isSslEnabled()) { + urlSchema = "https"; + } + + microserviceVersionRule = RegistryUtils.getServiceRegistry().getAppManager() + .getOrCreateMicroserviceVersionRule(RegistryUtils.getAppId(), producerName, + DefinitionConst.VERSION_RULE_ALL); + MicroserviceVersionMeta microserviceVersionMeta = microserviceVersionRule.getLatestMicroserviceVersion(); + SchemaMeta schemaMeta = microserviceVersionMeta.getMicroserviceMeta().ensureFindSchemaMeta(schemaId); + String urlPrefix = String + .format("%s://%s:%d/rest/%s%s", urlSchema, edgeAddress.getHostOrIp(), edgeAddress.getPort(), producerName, + schemaMeta.getSwagger().getBasePath()); + + setUriTemplateHandler(new ITUriTemplateHandler(urlPrefix)); + + setMessageConverters(Arrays.asList( + new MappingJackson2HttpMessageConverter(), + new StringHttpMessageConverter() + )); + } +} diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java new file mode 100644 index 0000000..bfb2528 --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java @@ -0,0 +1,46 @@ +/* + * 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.it.extend.engine; + +import org.apache.servicecomb.core.definition.MicroserviceVersionMeta; +import org.apache.servicecomb.core.definition.SchemaMeta; +import org.apache.servicecomb.provider.springmvc.reference.CseRestTemplate; +import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule; +import org.apache.servicecomb.serviceregistry.definition.DefinitionConst; + +public class ITSCBRestTemplate extends CseRestTemplate { + public ITSCBRestTemplate(String schemaId) { + this("it-producer", schemaId); + } + + public ITSCBRestTemplate(String producerName, String schemaId) { + MicroserviceVersionRule microserviceVersionRule = RegistryUtils.getServiceRegistry().getAppManager() + .getOrCreateMicroserviceVersionRule(RegistryUtils.getAppId(), producerName, + DefinitionConst.VERSION_RULE_ALL); + MicroserviceVersionMeta microserviceVersionMeta = microserviceVersionRule.getLatestMicroserviceVersion(); + SchemaMeta schemaMeta = microserviceVersionMeta.getMicroserviceMeta().ensureFindSchemaMeta(schemaId); + String urlPrefix = String.format("cse://%s/%s", producerName, schemaMeta.getSwagger().getBasePath()); + + setUriTemplateHandler(new ITUriTemplateHandler(urlPrefix)); + setRequestFactory(new ITClientHttpRequestFactory()); + } + + public void setTransport(String transport) { + ((ITClientHttpRequestFactory) getRequestFactory()).setTransport(transport); + } +} diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITUriTemplateHandler.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITUriTemplateHandler.java new file mode 100644 index 0000000..72b2104 --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITUriTemplateHandler.java @@ -0,0 +1,44 @@ +/* + * 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.it.extend.engine; + +import java.net.URI; +import java.util.Map; + +import org.springframework.web.util.DefaultUriTemplateHandler; + +public class ITUriTemplateHandler extends DefaultUriTemplateHandler { + private String urlPrefix; + + public ITUriTemplateHandler(String urlPrefix) { + this.urlPrefix = urlPrefix; + } + + @Override + protected URI expandInternal(String uriTemplate, Object... uriVariables) { + return super.expandInternal(changeUrl(uriTemplate), uriVariables); + } + + @Override + protected URI expandInternal(String uriTemplate, Map<String, ?> uriVariables) { + return super.expandInternal(changeUrl(uriTemplate), uriVariables); + } + + private String changeUrl(String uriTemplate) { + return urlPrefix + uriTemplate; + } +} diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/junit/ITJUnitUtils.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/junit/ITJUnitUtils.java index fc9a2b2..d681525 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/junit/ITJUnitUtils.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/junit/ITJUnitUtils.java @@ -18,10 +18,12 @@ package org.apache.servicecomb.it.junit; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Stack; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.servicecomb.core.Const; import org.apache.servicecomb.foundation.common.utils.JvmUtils; import org.junit.runner.JUnitCore; import org.junit.runner.Result; @@ -41,6 +43,8 @@ public final class ITJUnitUtils { private static AtomicInteger runCount = new AtomicInteger(); + private static String transport; + static { jUnitCore.addListener(new RunListener() { @Override @@ -63,6 +67,20 @@ public final class ITJUnitUtils { return parents; } + public static void pushTransport(String transport) { + ITJUnitUtils.transport = transport; + addParent(transport); + } + + public static String getTransport() { + return transport; + } + + public static void popTransport() { + ITJUnitUtils.transport = null; + popParent(); + } + public static void addParent(String name) { parents.add(name); } @@ -99,4 +117,22 @@ public final class ITJUnitUtils { throw new IllegalStateException("failed to find all classes in package " + packageName, e); } } + + public static void runWithHighwayAndRest(Class<?> classes) { + runWithTransports(Arrays.asList(Const.HIGHWAY, Const.RESTFUL), classes); + } + + public static void runWithRest(Class<?> classes) { + runWithTransports(Arrays.asList(Const.RESTFUL), classes); + } + + public static void runWithTransports(List<String> transports, Class<?> classes) { + for (String transport : transports) { + ITJUnitUtils.pushTransport(transport); + + ITJUnitUtils.run(classes); + + ITJUnitUtils.popTransport(); + } + } } diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestTrace.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestTrace.java new file mode 100644 index 0000000..37aa9cc --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestTrace.java @@ -0,0 +1,64 @@ +/* + * 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.it.testcase; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import org.apache.servicecomb.core.Const; +import org.apache.servicecomb.it.Consumers; +import org.apache.servicecomb.it.junit.ITJUnitUtils; +import org.apache.servicecomb.swagger.invocation.context.ContextUtils; +import org.apache.servicecomb.swagger.invocation.context.InvocationContext; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestTrace { + interface TraceSchemaIntf { + CompletableFuture<String> echoProxy(); + } + + static Consumers<TraceSchemaIntf> consumers = new Consumers<>("trace", TraceSchemaIntf.class); + + @BeforeClass + public static void classSetup() { + consumers.init(ITJUnitUtils.getTransport()); + + InvocationContext context = new InvocationContext(); + context.addContext(Const.TRACE_ID_NAME, "testId"); + ContextUtils.setInvocationContext(context); + } + + @AfterClass + public static void classTeardown() { + ContextUtils.removeInvocationContext(); + } + + @Test + public void echo_intf() throws ExecutionException, InterruptedException { + String traceId = consumers.getIntf().echoProxy().get(); + Assert.assertEquals("testId", traceId); + } + + @Test + public void echo_rt() { + String traceId = consumers.getSCBRestTemplate().getForObject("/echo-proxy", String.class); + Assert.assertEquals("testId", traceId); + } +} diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestTraceEdge.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestTraceEdge.java new file mode 100644 index 0000000..914464a --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestTraceEdge.java @@ -0,0 +1,41 @@ +/* + * 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.it.testcase; + +import org.apache.servicecomb.core.Const; +import org.apache.servicecomb.it.extend.engine.GateRestTemplate; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.web.client.RestTemplate; + +public class TestTraceEdge { + static RestTemplate rt = GateRestTemplate.createEdgeRestTemplate("trace"); + + @Test + public void echo() { + HttpHeaders headers = new HttpHeaders(); + headers.add(Const.TRACE_ID_NAME, "testId"); + + HttpEntity<String> requestEntity = new HttpEntity<>(null, headers); + + String traceId = rt.exchange("/echo-proxy", HttpMethod.GET, requestEntity, String.class).getBody(); + Assert.assertEquals("testId", traceId); + } +} diff --git a/integration-tests/it-edge/src/main/resources/microservice.yaml b/integration-tests/it-edge/src/main/resources/microservice.yaml index b57a630..b1ca0db 100644 --- a/integration-tests/it-edge/src/main/resources/microservice.yaml +++ b/integration-tests/it-edge/src/main/resources/microservice.yaml @@ -22,7 +22,7 @@ servicecomb: handler: chain: Consumer: - default: auth,loadbalance + default: loadbalance service: it-auth: loadbalance http: @@ -32,7 +32,7 @@ servicecomb: enabled: true prefix: rest withVersion: true - prefixSegmentCount: 1 + prefixSegmentCount: 2 url: enabled: true mappings: diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/TraceSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/TraceSchema.java new file mode 100644 index 0000000..9b5ba8a --- /dev/null +++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/TraceSchema.java @@ -0,0 +1,47 @@ +/* + * 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.it.schema; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import org.apache.servicecomb.core.Const; +import org.apache.servicecomb.provider.pojo.Invoker; +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.apache.servicecomb.swagger.invocation.context.InvocationContext; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@RestSchema(schemaId = "trace") +@RequestMapping(path = "/v1/trace") +public class TraceSchema { + interface TraceSchemaIntf { + CompletableFuture<String> echo(); + } + + TraceSchemaIntf intf = Invoker.createProxy("it-producer", "trace", TraceSchemaIntf.class); + + @GetMapping(path = "echo") + public String echo(InvocationContext context) { + return context.getContext(Const.TRACE_ID_NAME); + } + + @GetMapping(path = "echo-proxy") + public String echoProxy() throws ExecutionException, InterruptedException { + return intf.echo().get(); + } +}
