[
https://issues.apache.org/jira/browse/SCB-245?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16462493#comment-16462493
]
ASF GitHub Bot commented on SCB-245:
------------------------------------
WillemJiang closed pull request #180: [SCB-245] add dubbo support for omega,
pass omega context from consumer…
URL: https://github.com/apache/incubator-servicecomb-saga/pull/180
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/omega/omega-transport/omega-transport-dubbo/pom.xml
b/omega/omega-transport/omega-transport-dubbo/pom.xml
new file mode 100644
index 00000000..48071cf8
--- /dev/null
+++ b/omega/omega-transport/omega-transport-dubbo/pom.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ ~
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>omega-transport</artifactId>
+ <groupId>org.apache.servicecomb.saga</groupId>
+ <version>0.2.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>omega-transport-dubbo</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>com.alibaba</groupId>
+ <artifactId>dubbo</artifactId>
+ <version>2.6.1</version>
+ </dependency>
+ </dependencies>
+
+
+</project>
\ No newline at end of file
diff --git
a/omega/omega-transport/omega-transport-dubbo/src/main/java/org/apache/servicecomb/saga/omega/transport/dubbo/SagaDubboConsumerFilter.java
b/omega/omega-transport/omega-transport-dubbo/src/main/java/org/apache/servicecomb/saga/omega/transport/dubbo/SagaDubboConsumerFilter.java
new file mode 100644
index 00000000..40605e81
--- /dev/null
+++
b/omega/omega-transport/omega-transport-dubbo/src/main/java/org/apache/servicecomb/saga/omega/transport/dubbo/SagaDubboConsumerFilter.java
@@ -0,0 +1,62 @@
+/*
+ * 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.saga.omega.transport.dubbo;
+
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.common.extension.Activate;
+import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
+import com.alibaba.dubbo.rpc.Filter;
+import com.alibaba.dubbo.rpc.Invocation;
+import com.alibaba.dubbo.rpc.Invoker;
+import com.alibaba.dubbo.rpc.Result;
+import com.alibaba.dubbo.rpc.RpcException;
+
+import java.lang.invoke.MethodHandles;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static
org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static
org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+
+/**
+ * add saga transaction id to dubbo invocation
+* @date 03/05/2018 10:44 AM
+*/
+@Activate(group = {Constants.CONSUMER})
+public class SagaDubboConsumerFilter implements Filter {
+ private static final Logger LOG =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ public Result invoke(Invoker<?> invoker, Invocation invocation) throws
RpcException {
+ OmegaContext omegaContext = (OmegaContext) (new
SpringExtensionFactory()).getExtension(OmegaContext.class, "omegaContext");
+ if(omegaContext != null){
+ invocation.getAttachments().put(GLOBAL_TX_ID_KEY,
omegaContext.globalTxId());
+ invocation.getAttachments().put(LOCAL_TX_ID_KEY,
omegaContext.localTxId());
+ }
+ if (omegaContext != null && omegaContext.globalTxId() != null) {
+ LOG.info("Added {} {} and {} {} to dubbo invocation", new
Object[]{GLOBAL_TX_ID_KEY, omegaContext.globalTxId(),
+ LOCAL_TX_ID_KEY, omegaContext.localTxId()});
+ }
+
+ if (invoker != null) {
+ return invoker.invoke(invocation);
+ }
+ return null;
+ }
+}
diff --git
a/omega/omega-transport/omega-transport-dubbo/src/main/java/org/apache/servicecomb/saga/omega/transport/dubbo/SagaDubboProviderFilter.java
b/omega/omega-transport/omega-transport-dubbo/src/main/java/org/apache/servicecomb/saga/omega/transport/dubbo/SagaDubboProviderFilter.java
new file mode 100644
index 00000000..a4268802
--- /dev/null
+++
b/omega/omega-transport/omega-transport-dubbo/src/main/java/org/apache/servicecomb/saga/omega/transport/dubbo/SagaDubboProviderFilter.java
@@ -0,0 +1,61 @@
+/*
+ * 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.saga.omega.transport.dubbo;
+
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.common.extension.Activate;
+import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
+import com.alibaba.dubbo.rpc.*;
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.invoke.MethodHandles;
+
+import static
org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static
org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+
+/**
+ * get saga transaction id from dubbo invocation and set into omega context
+* @date 03/05/2018 10:44 AM
+*/
+@Activate(group = Constants.PROVIDER)
+public class SagaDubboProviderFilter implements Filter {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ @Override
+ public Result invoke(Invoker<?> invoker, Invocation invocation) throws
RpcException {
+ OmegaContext omegaContext = new
SpringExtensionFactory().getExtension(OmegaContext.class, "omegaContext");
+ String globalTxId = invocation.getAttachment(GLOBAL_TX_ID_KEY);
+ if (globalTxId == null) {
+ LOG.info("no such omega context global id: {}", GLOBAL_TX_ID_KEY);
+ }else{
+ omegaContext.setGlobalTxId(globalTxId);
+
omegaContext.setLocalTxId(invocation.getAttachment(LOCAL_TX_ID_KEY));
+ LOG.info("Added {} {} and {} {} to omegaContext", new
Object[]{GLOBAL_TX_ID_KEY, omegaContext.globalTxId(),
+ LOCAL_TX_ID_KEY, omegaContext.localTxId()});
+ }
+ invocation.getAttachments().put(GLOBAL_TX_ID_KEY,null);
+ invocation.getAttachments().put(LOCAL_TX_ID_KEY,null);
+
+ if(invoker != null){
+ return invoker.invoke(invocation);
+ }
+ return null;
+ }
+}
diff --git
a/omega/omega-transport/omega-transport-dubbo/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
b/omega/omega-transport/omega-transport-dubbo/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
new file mode 100644
index 00000000..f60a7433
--- /dev/null
+++
b/omega/omega-transport/omega-transport-dubbo/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
@@ -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.
+## ---------------------------------------------------------------------------
+sagaDubboConsumerFilter=org.apache.servicecomb.saga.omega.transport.dubbo.SagaDubboConsumerFilter
+sageDubboProviderFilter=org.apache.servicecomb.saga.omega.transport.dubbo.SagaDubboProviderFilter
\ No newline at end of file
diff --git
a/omega/omega-transport/omega-transport-dubbo/src/test/java/SagaDubboConsumerFilterTest.java
b/omega/omega-transport/omega-transport-dubbo/src/test/java/SagaDubboConsumerFilterTest.java
new file mode 100644
index 00000000..e601edac
--- /dev/null
+++
b/omega/omega-transport/omega-transport-dubbo/src/test/java/SagaDubboConsumerFilterTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+
+import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
+import com.alibaba.dubbo.rpc.Invocation;
+import org.apache.servicecomb.saga.omega.context.IdGenerator;
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import
org.apache.servicecomb.saga.omega.transport.dubbo.SagaDubboConsumerFilter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.context.ApplicationContext;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SagaDubboConsumerFilterTest {
+
+ private static final String globalTxId = UUID.randomUUID().toString();
+ private static final String localTxId = UUID.randomUUID().toString();
+ @SuppressWarnings("unchecked")
+ private final IdGenerator<String> idGenerator = mock(IdGenerator.class);
+
+ private final OmegaContext omegaContext = new OmegaContext(() -> "ignored");
+ private final Invocation invocation = mock(Invocation.class);
+ private final ApplicationContext applicationContext =
mock(ApplicationContext.class);
+ private final SagaDubboConsumerFilter filter = new SagaDubboConsumerFilter();
+
+ @Before
+ public void setUp() {
+ when(idGenerator.nextId()).thenReturn(globalTxId, localTxId);
+ when(applicationContext.containsBean("omegaContext")).thenReturn(true);
+ when(applicationContext.getBean("omegaContext")).thenReturn(omegaContext);
+ SpringExtensionFactory.addApplicationContext(applicationContext);
+ }
+
+ @After
+ public void setDown(){
+ SpringExtensionFactory.removeApplicationContext(applicationContext);
+ }
+
+ @Test
+ public void keepHeaderUnchangedIfContextAbsent() throws Exception {
+
when(invocation.getAttachment(OmegaContext.GLOBAL_TX_ID_KEY)).thenReturn(null);
+
when(invocation.getAttachment(OmegaContext.LOCAL_TX_ID_KEY)).thenReturn(null);
+
+ filter.invoke(null, invocation);
+
+ assertThat(invocation.getAttachments().isEmpty(), is(true));
+ }
+
+ @Test
+ public void interceptTransactionIdInHeaderIfContextPresent() throws
Exception {
+ omegaContext.setGlobalTxId(globalTxId);
+ omegaContext.setLocalTxId(localTxId);
+
+ Map<String, String> attachMents = new HashMap<>();
+ when(invocation.getAttachments()).thenReturn(attachMents);
+
+ filter.invoke(null, invocation);
+
+ assertThat(invocation.getAttachments().get(OmegaContext.GLOBAL_TX_ID_KEY),
is(globalTxId));
+ assertThat(invocation.getAttachments().get(OmegaContext.LOCAL_TX_ID_KEY),
is(localTxId));
+ }
+}
diff --git
a/omega/omega-transport/omega-transport-dubbo/src/test/java/SagaDubboProviderFilterTest.java
b/omega/omega-transport/omega-transport-dubbo/src/test/java/SagaDubboProviderFilterTest.java
new file mode 100644
index 00000000..0a5f576a
--- /dev/null
+++
b/omega/omega-transport/omega-transport-dubbo/src/test/java/SagaDubboProviderFilterTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.
+ */
+
+import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
+import com.alibaba.dubbo.rpc.Invocation;
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import
org.apache.servicecomb.saga.omega.transport.dubbo.SagaDubboProviderFilter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.context.ApplicationContext;
+
+import java.util.UUID;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SagaDubboProviderFilterTest {
+
+ private static final String globalTxId = UUID.randomUUID().toString();
+ private static final String localTxId = UUID.randomUUID().toString();
+ private final OmegaContext omegaContext = new OmegaContext(() -> "ignored");
+ private final Invocation invocation = mock(Invocation.class);
+ private final ApplicationContext applicationContext =
mock(ApplicationContext.class);
+
+ private final SagaDubboProviderFilter filter = new SagaDubboProviderFilter();
+
+ @Before
+ public void setUp() {
+ omegaContext.clear();
+ ApplicationContext applicationContext = mock(ApplicationContext.class);
+ when(applicationContext.containsBean("omegaContext")).thenReturn(true);
+ when(applicationContext.getBean("omegaContext")).thenReturn(omegaContext);
+ SpringExtensionFactory.addApplicationContext(applicationContext);
+ }
+
+ @After
+ public void setDown(){
+ SpringExtensionFactory.removeApplicationContext(applicationContext);
+ }
+
+ @Test
+ public void setUpOmegaContextInTransactionRequest() throws Exception {
+
when(invocation.getAttachment(OmegaContext.GLOBAL_TX_ID_KEY)).thenReturn(globalTxId);
+
when(invocation.getAttachment(OmegaContext.LOCAL_TX_ID_KEY)).thenReturn(localTxId);
+
+ filter.invoke(null, invocation);
+
+ assertThat(omegaContext.globalTxId(), is(globalTxId));
+ assertThat(omegaContext.localTxId(), is(localTxId));
+ }
+
+ @Test
+ public void doNothingInNonTransactionRequest() throws Exception {
+
when(invocation.getAttachment(OmegaContext.GLOBAL_TX_ID_KEY)).thenReturn(null);
+
when(invocation.getAttachment(OmegaContext.LOCAL_TX_ID_KEY)).thenReturn(null);
+
+ filter.invoke(null, invocation);
+
+ assertThat(omegaContext.globalTxId(), is(nullValue()));
+ assertThat(omegaContext.localTxId(), is(nullValue()));
+ }
+
+}
diff --git a/omega/omega-transport/pom.xml b/omega/omega-transport/pom.xml
index 8d5751ef..055a4611 100644
--- a/omega/omega-transport/pom.xml
+++ b/omega/omega-transport/pom.xml
@@ -34,6 +34,7 @@
<modules>
<module>omega-transport-resttemplate</module>
<module>omega-transport-servicecomb</module>
+ <module>omega-transport-dubbo</module>
</modules>
<dependencies>
----------------------------------------------------------------
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]
> [pack] support to intercept dubbo's requests
> --------------------------------------------
>
> Key: SCB-245
> URL: https://issues.apache.org/jira/browse/SCB-245
> Project: Apache ServiceComb
> Issue Type: Improvement
> Components: Saga
> Affects Versions: saga-0.2.0
> Reporter: Eric Lee
> Priority: Minor
> Fix For: saga-0.2.0
>
>
> as a sdk, I want to be able to intercept the incoming and outgoing requests
> of apps developed by dubbo and inject the transaction ids in its header.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)