liubao68 closed pull request #885: [SCB-859] Generate traceid
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/885
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/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
index a467f0bf1..65ef363d8 100644
---
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
+++
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
@@ -118,7 +118,7 @@ protected void scheduleInvocation() {
return;
}
- invocation.onStart();
+ invocation.onStart(requestEx);
OperationMeta operationMeta = restOperationMeta.getOperationMeta();
operationMeta.getExecutor().execute(() -> {
diff --git
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/tracing/TracingFilter.java
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/tracing/TracingFilter.java
deleted file mode 100644
index 94b2fc515..000000000
---
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/tracing/TracingFilter.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * 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.common.rest.filter.tracing;
-
-import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
-import org.apache.servicecomb.core.Const;
-import org.apache.servicecomb.core.Invocation;
-import org.apache.servicecomb.core.tracing.BraveTraceIdGenerator;
-import org.apache.servicecomb.core.tracing.TraceIdGenerator;
-import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
-import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
-import org.apache.servicecomb.swagger.invocation.Response;
-import org.springframework.util.StringUtils;
-
-/**
- * Ensure the invocation contains traceId
- */
-public class TracingFilter implements HttpServerFilter {
- private TraceIdGenerator traceIdGenerator = getTraceIdGenerator();
-
- @Override
- public int getOrder() {
- return 0;
- }
-
- /**
- * Ensure the invocation contains traceId
- * @return {@code null}
- */
- @Override
- public Response afterReceiveRequest(Invocation invocation,
HttpServletRequestEx requestEx) {
- if (!StringUtils.isEmpty(invocation.getContext(Const.TRACE_ID_NAME))) {
- // if invocation context contains traceId, nothing needed to do
- return null;
- }
-
- String traceId = requestEx.getHeader(Const.TRACE_ID_NAME);
- if (!StringUtils.isEmpty(traceId)) {
- // if request header contains traceId, move traceId into invocation
context
- invocation.addContext(Const.TRACE_ID_NAME, traceId);
- return null;
- }
-
- // if traceId not found, generate a traceId
- invocation.addContext(Const.TRACE_ID_NAME,
traceIdGenerator.generateStringId());
-
- return null;
- }
-
- /**
- * nothing to do
- */
- @Override
- public void beforeSendResponse(Invocation invocation, HttpServletResponseEx
responseEx) {
- }
-
- protected TraceIdGenerator getTraceIdGenerator() {
- return BraveTraceIdGenerator.INSTANCE;
- }
-}
diff --git
a/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
b/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
index 91e25afe4..13ee39be8 100644
---
a/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
+++
b/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
@@ -16,4 +16,3 @@
#
org.apache.servicecomb.common.rest.filter.inner.ServerRestArgsFilter
-org.apache.servicecomb.common.rest.filter.tracing.TracingFilter
\ No newline at end of file
diff --git
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
index 2b0fffae2..dcc6110f8 100644
---
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
+++
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
@@ -102,6 +102,13 @@
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ class AbstractHttpServletRequestForTest extends AbstractHttpServletRequest {
+ @Override
+ public String getHeader(String name) {
+ return null;
+ }
+ }
+
class AbstractRestInvocationForTest extends AbstractRestInvocation {
@Override
protected OperationLocator locateOperation(ServicePathManager
servicePathManager) {
@@ -730,8 +737,7 @@ protected OperationLocator
locateOperation(ServicePathManager servicePathManager
@Test
public void scheduleInvocationException(@Mocked OperationMeta operationMeta)
{
Executor executor = new ReactiveExecutor();
- requestEx = new AbstractHttpServletRequest() {
- };
+ requestEx = new AbstractHttpServletRequestForTest();
requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
new Expectations() {
{
@@ -778,8 +784,7 @@ public void scheduleInvocationTimeout(@Mocked OperationMeta
operationMeta) {
}
};
- requestEx = new AbstractHttpServletRequest() {
- };
+ requestEx = new AbstractHttpServletRequestForTest();
restInvocation = new AbstractRestInvocationForTest() {
@Override
@@ -818,15 +823,16 @@ public void onStart(InvocationStartEvent event) {
EventManager.register(subscriber);
Executor executor = new ReactiveExecutor();
- requestEx = new AbstractHttpServletRequest() {
- };
+ requestEx = new AbstractHttpServletRequestForTest();
requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
- new Expectations() {
+ new Expectations(requestEx) {
{
restOperation.getOperationMeta();
result = operationMeta;
operationMeta.getExecutor();
result = executor;
+ requestEx.getHeader(Const.TRACE_ID_NAME);
+ result = "tid";
}
};
@@ -846,6 +852,7 @@ protected void runOnExecutor() {
Assert.assertTrue(result.value);
Assert.assertEquals(time, invocation.getStartTime());
Assert.assertSame(invocation, eventHolder.value.getInvocation());
+ Assert.assertEquals("tid", invocation.getTraceId());
}
@Test
diff --git
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/tracing/TracingFilterTest.java
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/tracing/TracingFilterTest.java
deleted file mode 100644
index 141acf85e..000000000
---
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/tracing/TracingFilterTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * 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.common.rest.filter.tracing;
-
-import org.apache.servicecomb.core.Const;
-import org.apache.servicecomb.core.Invocation;
-import org.apache.servicecomb.core.tracing.TraceIdGenerator;
-import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
-import org.junit.Test;
-import org.mockito.Mockito;
-
-public class TracingFilterTest {
- private static final TestTracingFilter FILTER = new TestTracingFilter();
-
- @Test
- public void testAfterReceiveRequestOnInvocationContainsTraceId() {
- Invocation invocation = Mockito.mock(Invocation.class);
- String traceId = "traceIdTest";
- HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
-
-
Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(traceId);
-
- FILTER.afterReceiveRequest(invocation, requestEx);
-
- Mockito.verify(requestEx, Mockito.times(0)).getHeader(Const.TRACE_ID_NAME);
- }
-
- @Test
- public void testAfterReceiveRequestOnHeaderContainsTraceId() {
- Invocation invocation = Mockito.mock(Invocation.class);
- String traceId = "traceIdTest";
- HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
-
- Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(null);
- Mockito.when(requestEx.getHeader(Const.TRACE_ID_NAME)).thenReturn(traceId);
-
- FILTER.afterReceiveRequest(invocation, requestEx);
-
- Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME, traceId);
- }
-
- @Test
- public void testAfterReceiveRequestOnGenerateTraceId() {
- Invocation invocation = Mockito.mock(Invocation.class);
- HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
-
- Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(null);
- Mockito.when(requestEx.getHeader(Const.TRACE_ID_NAME)).thenReturn(null);
-
- FILTER.afterReceiveRequest(invocation, requestEx);
-
- Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME,
TestTracingFilter.TRACE_ID);
- }
-
- static class TestTracingFilter extends TracingFilter {
-
- static final String TRACE_ID = "" + Long.MAX_VALUE;
-
- @Override
- protected TraceIdGenerator getTraceIdGenerator() {
- return () -> TRACE_ID;
- }
- }
-}
diff --git a/core/src/main/java/org/apache/servicecomb/core/Invocation.java
b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
index 6b5be130a..ceb5f928e 100644
--- a/core/src/main/java/org/apache/servicecomb/core/Invocation.java
+++ b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
@@ -17,22 +17,33 @@
package org.apache.servicecomb.core;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
+import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.core.definition.SchemaMeta;
import org.apache.servicecomb.core.event.InvocationFinishEvent;
import org.apache.servicecomb.core.event.InvocationStartEvent;
import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
+import org.apache.servicecomb.core.tracing.TraceIdGenerator;
import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.swagger.invocation.AsyncResponse;
import org.apache.servicecomb.swagger.invocation.InvocationType;
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
public class Invocation extends SwaggerInvocation {
+ private static final Collection<TraceIdGenerator> TRACE_ID_GENERATORS =
loadTraceIdGenerators();
+
+ static Collection<TraceIdGenerator> loadTraceIdGenerators() {
+ return SPIServiceUtils.getPriorityHighestServices(generator ->
generator.getName(), TraceIdGenerator.class);
+ }
+
private ReferenceConfig referenceConfig;
// 本次调用对应的schemaMeta
@@ -63,6 +74,20 @@
private boolean sync = true;
+ private HttpServletRequestEx requestEx;
+
+ public HttpServletRequestEx getRequestEx() {
+ return requestEx;
+ }
+
+ public String getTraceId() {
+ return getContext(Const.TRACE_ID_NAME);
+ }
+
+ public String getTraceId(String traceIdName) {
+ return getContext(traceIdName);
+ }
+
public long getStartTime() {
return startTime;
}
@@ -188,11 +213,46 @@ public String getMicroserviceQualifiedName() {
return operationMeta.getMicroserviceQualifiedName();
}
+ protected void initTraceId() {
+ for (TraceIdGenerator traceIdGenerator : TRACE_ID_GENERATORS) {
+ initTraceId(traceIdGenerator);
+ }
+ }
+
+ protected void initTraceId(TraceIdGenerator traceIdGenerator) {
+ if
(!StringUtils.isEmpty(getTraceId(traceIdGenerator.getTraceIdKeyName()))) {
+ // if invocation context contains traceId, nothing needed to do
+ return;
+ }
+
+ if (requestEx == null) {
+ // it's a new consumer invocation, must generate a traceId
+ addContext(traceIdGenerator.getTraceIdKeyName(),
traceIdGenerator.generate());
+ return;
+ }
+
+ String traceId = requestEx.getHeader(traceIdGenerator.getTraceIdKeyName());
+ if (!StringUtils.isEmpty(traceId)) {
+ // if request header contains traceId, save traceId into invocation
context
+ addContext(traceIdGenerator.getTraceIdKeyName(), traceId);
+ return;
+ }
+
+ // if traceId not found, generate a traceId
+ addContext(traceIdGenerator.getTraceIdKeyName(),
traceIdGenerator.generate());
+ }
+
public void onStart() {
this.startTime = System.nanoTime();
+ initTraceId();
EventManager.post(new InvocationStartEvent(this));
}
+ public void onStart(HttpServletRequestEx requestEx) {
+ this.requestEx = requestEx;
+ onStart();
+ }
+
public void onStartExecute() {
this.startExecutionTime = System.nanoTime();
}
diff --git
a/core/src/main/java/org/apache/servicecomb/core/filter/EndpointDiscoveryFilter.java
b/core/src/main/java/org/apache/servicecomb/core/filter/EndpointDiscoveryFilter.java
index 349f68af4..d05c59d6d 100644
---
a/core/src/main/java/org/apache/servicecomb/core/filter/EndpointDiscoveryFilter.java
+++
b/core/src/main/java/org/apache/servicecomb/core/filter/EndpointDiscoveryFilter.java
@@ -25,8 +25,12 @@
import
org.apache.servicecomb.serviceregistry.discovery.AbstractEndpointDiscoveryFilter;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class EndpointDiscoveryFilter extends AbstractEndpointDiscoveryFilter {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(EndpointDiscoveryFilter.class);
+
@Override
public int getOrder() {
return (int) Short.MAX_VALUE;
@@ -42,6 +46,7 @@ protected String findTransportName(DiscoveryContext context,
DiscoveryTreeNode p
protected Object createEndpoint(String transportName, String endpoint,
MicroserviceInstance instance) {
Transport transport =
CseContext.getInstance().getTransportManager().findTransport(transportName);
if (transport == null) {
+ LOGGER.info("not deployed transport {}, ignore {}.", transportName,
endpoint);
return null;
}
diff --git
a/core/src/main/java/org/apache/servicecomb/core/tracing/BraveTraceIdGenerator.java
b/core/src/main/java/org/apache/servicecomb/core/tracing/BraveTraceIdGenerator.java
index c740b7905..7a2eee0ea 100644
---
a/core/src/main/java/org/apache/servicecomb/core/tracing/BraveTraceIdGenerator.java
+++
b/core/src/main/java/org/apache/servicecomb/core/tracing/BraveTraceIdGenerator.java
@@ -17,16 +17,18 @@
package org.apache.servicecomb.core.tracing;
+import org.apache.servicecomb.core.Const;
+
import brave.internal.Platform;
public class BraveTraceIdGenerator implements TraceIdGenerator {
- public static final BraveTraceIdGenerator INSTANCE = new
BraveTraceIdGenerator();
-
@Override
- public String generateStringId() {
- return Long.toHexString(Platform.get().nextTraceIdHigh());
+ public String getTraceIdKeyName() {
+ return Const.TRACE_ID_NAME;
}
- private BraveTraceIdGenerator() {
+ @Override
+ public String generate() {
+ return Long.toHexString(Platform.get().nextTraceIdHigh());
}
}
diff --git
a/core/src/main/java/org/apache/servicecomb/core/tracing/TraceIdGenerator.java
b/core/src/main/java/org/apache/servicecomb/core/tracing/TraceIdGenerator.java
index 94bd9f779..7f807be7a 100644
---
a/core/src/main/java/org/apache/servicecomb/core/tracing/TraceIdGenerator.java
+++
b/core/src/main/java/org/apache/servicecomb/core/tracing/TraceIdGenerator.java
@@ -18,5 +18,29 @@
package org.apache.servicecomb.core.tracing;
public interface TraceIdGenerator {
- String generateStringId();
+ default int getOrder() {
+ return 1000;
+ }
+
+ /**
+ * <pre>
+ * for generators have the same name, will only use the minimum order
instance
+ * not use getTraceIdKeyName to control this logic, because most customers
not want to generate multiple traceIds
+ * </pre>
+ * @return generator name
+ */
+ default String getName() {
+ return "default";
+ }
+
+ /**
+ *
+ * @return trance id key name
+ * <pre>
+ * default value is X-B3-TraceId to work with zipkin
+ * </pre>
+ */
+ String getTraceIdKeyName();
+
+ String generate();
}
diff --git
a/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.tracing.TraceIdGenerator
b/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.tracing.TraceIdGenerator
new file mode 100644
index 000000000..3a57a7db6
--- /dev/null
+++
b/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.tracing.TraceIdGenerator
@@ -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.tracing.BraveTraceIdGenerator
\ No newline at end of file
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
b/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
index 0efc78370..c0e98b749 100644
--- a/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
+++ b/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
@@ -16,14 +16,21 @@
*/
package org.apache.servicecomb.core;
+import java.util.Arrays;
+
import javax.xml.ws.Holder;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.core.event.InvocationFinishEvent;
import org.apache.servicecomb.core.event.InvocationStartEvent;
import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
+import org.apache.servicecomb.core.tracing.BraveTraceIdGenerator;
+import org.apache.servicecomb.core.tracing.TraceIdGenerator;
import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.swagger.invocation.Response;
+import org.hamcrest.Matchers;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@@ -32,6 +39,7 @@
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
+import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
@@ -144,4 +152,90 @@ public void localContext(@Mocked ReferenceConfig
referenceConfig) {
Assert.assertSame(invocation.getHandlerContext(),
invocation.getLocalContext());
Assert.assertEquals(1, (int) invocation.getLocalContext("k"));
}
+
+ @Test
+ public void traceId_fromContext(@Mocked ReferenceConfig referenceConfig) {
+ Invocation invocation = new Invocation(referenceConfig, operationMeta,
swaggerArguments);
+ invocation.addContext(Const.TRACE_ID_NAME, "abc");
+
+ invocation.onStart();
+
+ Assert.assertEquals("abc", invocation.getTraceId());
+ Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
+ }
+
+ @Test
+ public void traceId_consumerCreateTraceId(@Mocked ReferenceConfig
referenceConfig) {
+ TraceIdGenerator generator =
SPIServiceUtils.getTargetService(TraceIdGenerator.class,
BraveTraceIdGenerator.class);
+ new Expectations(generator) {
+ {
+ generator.generate();
+ result = "abc";
+ }
+ };
+ Invocation invocation = new Invocation(referenceConfig, operationMeta,
swaggerArguments);
+
+ invocation.onStart();
+
+ Assert.assertEquals("abc", invocation.getTraceId());
+ Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
+ }
+
+ @Test
+ public void traceId_fromRequest(@Mocked Endpoint endpoint, @Mocked
HttpServletRequestEx requestEx) {
+ new Expectations() {
+ {
+ requestEx.getHeader(Const.TRACE_ID_NAME);
+ result = "abc";
+ }
+ };
+ Invocation invocation = new Invocation(endpoint, operationMeta,
swaggerArguments);
+
+ invocation.onStart(requestEx);
+
+ Assert.assertEquals("abc", invocation.getTraceId());
+ Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
+ }
+
+ @Test
+ public void traceId_producerCreateTraceId(@Mocked Endpoint endpoint, @Mocked
HttpServletRequestEx requestEx) {
+ TraceIdGenerator generator =
SPIServiceUtils.getTargetService(TraceIdGenerator.class,
BraveTraceIdGenerator.class);
+ new Expectations(generator) {
+ {
+ generator.generate();
+ result = "abc";
+ }
+ };
+ Invocation invocation = new Invocation(endpoint, operationMeta,
swaggerArguments);
+
+ invocation.onStart(requestEx);
+
+ Assert.assertEquals("abc", invocation.getTraceId());
+ Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
+ }
+
+ @Test
+ public void traceIdGeneratorInit(@Mocked TraceIdGenerator gen1, @Mocked
TraceIdGenerator gen2,
+ @Mocked TraceIdGenerator gen3, @Mocked TraceIdGenerator gen4) {
+ new Expectations(SPIServiceUtils.class) {
+ {
+ gen1.getName();
+ result = "zipkin";
+
+ gen3.getName();
+ result = "apm";
+
+ gen2.getName();
+ result = "zipkin";
+
+ gen4.getName();
+ result = "apm";
+
+ SPIServiceUtils.getOrLoadSortedService(TraceIdGenerator.class);
+ result = Arrays.asList(gen1, gen3, gen2, gen4);
+ }
+ };
+
+ Assert.assertThat(Invocation.loadTraceIdGenerators(),
Matchers.contains(gen1, gen3));
+ }
}
diff --git
a/core/src/test/java/org/apache/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java
b/core/src/test/java/org/apache/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java
index d9c0bea8d..8963cd292 100644
---
a/core/src/test/java/org/apache/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java
+++
b/core/src/test/java/org/apache/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java
@@ -26,10 +26,10 @@
@Test
public void generateStringId() {
- TraceIdGenerator traceIdGenerator = BraveTraceIdGenerator.INSTANCE;
- assertNotEquals(traceIdGenerator.generateStringId(),
traceIdGenerator.generateStringId());
+ TraceIdGenerator traceIdGenerator = new BraveTraceIdGenerator();
+ assertNotEquals(traceIdGenerator.generate(), traceIdGenerator.generate());
- String traceId = traceIdGenerator.generateStringId();
+ String traceId = traceIdGenerator.generate();
try {
Long.parseLong(traceId, 16);
} catch (NumberFormatException e) {
diff --git a/demo/demo-schema/src/main/resources/config/log4j.properties
b/demo/demo-schema/src/main/resources/config/log4j.properties
index fe3ad5a25..cad696acc 100644
--- a/demo/demo-schema/src/main/resources/config/log4j.properties
+++ b/demo/demo-schema/src/main/resources/config/log4j.properties
@@ -15,7 +15,7 @@
# limitations under the License.
#
-paas.logs.dir=../logs/
+paas.logs.dir=../target/logs/
paas.logs.file=cse.log
log4j.rootLogger=INFO,paas,stdout
diff --git
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/SPIServiceUtils.java
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/SPIServiceUtils.java
index 9cbfc86db..ea8a0bc12 100644
---
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/SPIServiceUtils.java
+++
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/SPIServiceUtils.java
@@ -20,12 +20,15 @@
import java.lang.reflect.Method;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
@@ -129,6 +132,18 @@ private SPIServiceUtils() {
return services.get(0);
}
+ public static <T> Collection<T> getPriorityHighestServices(Function<T,
String> keyFunc, Class<T> serviceType) {
+ List<T> services = getOrLoadSortedService(serviceType);
+ if (services.isEmpty()) {
+ LOGGER.info("Can not find SPI service for {}", serviceType.getName());
+ return null;
+ }
+
+ Map<String, T> map = new HashMap<>();
+ services.forEach(instance -> map.putIfAbsent(keyFunc.apply(instance),
instance));
+ return map.values();
+ }
+
@SuppressWarnings("unchecked")
public static <T, IMPL> IMPL getTargetService(Class<T> serviceType,
Class<IMPL> implType) {
List<T> services = getOrLoadSortedService(serviceType);
diff --git
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestSPIServiceUtils.java
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestSPIServiceUtils.java
index 2bb136bd3..6b26ff9e8 100644
---
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestSPIServiceUtils.java
+++
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestSPIServiceUtils.java
@@ -93,4 +93,65 @@ public void testSort(@Mocked Ordered o1, @Mocked Ordered o2)
{
public void getPriorityHighestService_null() {
Assert.assertNull(SPIServiceUtils.getPriorityHighestService(SPIServiceDef0.class));
}
+
+ interface PriorityIntf {
+ String getName();
+
+ int getOrder();
+ }
+
+ public class PriorityImpl implements PriorityIntf {
+ private final String name;
+
+ private final int order;
+
+ public PriorityImpl(String name, int order) {
+ this.name = name;
+ this.order = order;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public int getOrder() {
+ return order;
+ }
+
+ @Override
+ public String toString() {
+ return "PriorityImpl{" +
+ "name='" + name + '\'' +
+ ", order=" + order +
+ '}';
+ }
+ }
+
+ @Test
+ public void getPriorityHighestServices() {
+ Map<String, PriorityIntf> instances = new LinkedHashMap<>();
+ instances.putIfAbsent("1", new PriorityImpl("n1", 0));
+ instances.putIfAbsent("2", new PriorityImpl("n1", -1));
+ instances.putIfAbsent("3", new PriorityImpl("n1", 1));
+ instances.putIfAbsent("4", new PriorityImpl("n2", 0));
+ instances.putIfAbsent("5", new PriorityImpl("n2", -1));
+ instances.putIfAbsent("6", new PriorityImpl("n2", 1));
+
+ ServiceLoader<PriorityIntf> serviceLoader =
ServiceLoader.load(PriorityIntf.class);
+ Deencapsulation.setField(serviceLoader, "providers", instances);
+ new Expectations(ServiceLoader.class) {
+ {
+ ServiceLoader.load(PriorityIntf.class);
+ result = serviceLoader;
+ }
+ };
+
+ Assert.assertThat(SPIServiceUtils.getPriorityHighestServices(inst ->
inst.getName(), PriorityIntf.class),
+ Matchers.containsInAnyOrder(instances.get("2"), instances.get("5")));
+
+ Map<Class<?>, List<Object>> cache =
Deencapsulation.getField(SPIServiceUtils.class, "cache");
+ cache.clear();
+ }
}
diff --git
a/foundations/foundation-test-scaffolding/src/main/resources/config/log4j.properties
b/foundations/foundation-test-scaffolding/src/main/resources/config/log4j.properties
new file mode 100644
index 000000000..a6911b4f6
--- /dev/null
+++
b/foundations/foundation-test-scaffolding/src/main/resources/config/log4j.properties
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+paas.logs.dir=target/
\ No newline at end of file
diff --git
a/handlers/handler-publickey-auth/src/test/java/org/apache/servicecomb/authentication/TestAccessController.java
b/handlers/handler-publickey-auth/src/test/java/org/apache/servicecomb/authentication/TestAccessController.java
index e493e7703..892aceaac 100644
---
a/handlers/handler-publickey-auth/src/test/java/org/apache/servicecomb/authentication/TestAccessController.java
+++
b/handlers/handler-publickey-auth/src/test/java/org/apache/servicecomb/authentication/TestAccessController.java
@@ -20,20 +20,13 @@
import java.util.Map;
import org.apache.servicecomb.authentication.provider.AccessController;
-import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
public class TestAccessController {
- @Before
- public void setUp() throws Exception {
- Log4jUtils.init();
- }
-
@After
public void tearDown() {
ArchaiusUtils.resetConfig();
diff --git a/integration-tests/it-common/src/main/resources/logback.xml
b/integration-tests/it-common/src/main/resources/logback.xml
index d753514a0..625669066 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 514b870dc..10868018c 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.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 @@ private static void testStandalone() throws Throwable {
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 000000000..4f3f42b6b
--- /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 000000000..7c79a2d4b
--- /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 000000000..bfb25287e
--- /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 000000000..72b210429
--- /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 fc9a2b2fc..d681525c8 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 @@
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 @@
private static AtomicInteger runCount = new AtomicInteger();
+ private static String transport;
+
static {
jUnitCore.addListener(new RunListener() {
@Override
@@ -63,6 +67,20 @@ public static int getRunCount() {
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 static void run(Class<?>... classes) {
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 000000000..37aa9cc3a
--- /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 000000000..914464aa9
--- /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 b57a63094..b1ca0db82 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 000000000..9b5ba8af3
--- /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();
+ }
+}
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services