chore(project): 添加项目配置文件和忽略规则
- 添加 Babel 配置文件支持 ES6+ 语法转换 - 添加 ESLint 忽略规则和配置文件 - 添加 Git 忽略规则文件 - 添加 Travis CI 配置文件 - 添加 1.4.2 版本变更日志文件 - 添加 Helm 图表辅助模板文件 - 添加 Helm 忽略规则文件
This commit is contained in:
44
integration/sofa-rpc/pom.xml
Normal file
44
integration/sofa-rpc/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright 1999-2019 Seata.io Group.
|
||||
~
|
||||
~ Licensed 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>
|
||||
<groupId>io.seata</groupId>
|
||||
<artifactId>seata-parent</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>seata-sofa-rpc</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>seata-sofa-rpc ${project.version}</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>seata-tm</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>sofa-rpc-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed 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 io.seata.integration.sofa.rpc;
|
||||
|
||||
import com.alipay.sofa.rpc.context.RpcInternalContext;
|
||||
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
|
||||
import com.alipay.sofa.rpc.core.request.SofaRequest;
|
||||
import com.alipay.sofa.rpc.core.response.SofaResponse;
|
||||
import com.alipay.sofa.rpc.ext.Extension;
|
||||
import com.alipay.sofa.rpc.filter.AutoActive;
|
||||
import com.alipay.sofa.rpc.filter.Filter;
|
||||
import com.alipay.sofa.rpc.filter.FilterInvoker;
|
||||
import io.seata.core.context.RootContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* TransactionContext on consumer side.
|
||||
*
|
||||
* @author Geng Zhang
|
||||
* @since 0.6.0
|
||||
*/
|
||||
@Extension(value = "transactionContextConsumer")
|
||||
@AutoActive(consumerSide = true)
|
||||
public class TransactionContextConsumerFilter extends Filter {
|
||||
|
||||
/**
|
||||
* Logger for this class
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionContextConsumerFilter.class);
|
||||
|
||||
@Override
|
||||
public SofaResponse invoke(FilterInvoker filterInvoker, SofaRequest sofaRequest) throws SofaRpcException {
|
||||
String xid = RootContext.getXID();
|
||||
String rpcXid = getRpcXid();
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("xid in RootContext[" + xid + "] xid in RpcContext[" + rpcXid + "]");
|
||||
}
|
||||
boolean bind = false;
|
||||
if (xid != null) {
|
||||
sofaRequest.addRequestProp(RootContext.KEY_XID, xid);
|
||||
} else {
|
||||
if (rpcXid != null) {
|
||||
RootContext.bind(rpcXid);
|
||||
bind = true;
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("bind[" + rpcXid + "] to RootContext");
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
return filterInvoker.invoke(sofaRequest);
|
||||
} finally {
|
||||
if (bind) {
|
||||
String unbindXid = RootContext.unbind();
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("unbind[" + unbindXid + "] from RootContext");
|
||||
}
|
||||
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
|
||||
if (LOGGER.isWarnEnabled()) {
|
||||
LOGGER.warn("xid in change during RPC from " + rpcXid + " to " + unbindXid);
|
||||
}
|
||||
if (unbindXid != null) {
|
||||
RootContext.bind(unbindXid);
|
||||
if (LOGGER.isWarnEnabled()) {
|
||||
LOGGER.warn("bind [" + unbindXid + "] back to RootContext");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get rpc xid
|
||||
* @return
|
||||
*/
|
||||
private String getRpcXid() {
|
||||
String rpcXid = (String) RpcInternalContext.getContext().getAttachment(RootContext.KEY_XID);
|
||||
if (rpcXid == null) {
|
||||
rpcXid = (String) RpcInternalContext.getContext().getAttachment(RootContext.KEY_XID.toLowerCase());
|
||||
}
|
||||
return rpcXid;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed 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 io.seata.integration.sofa.rpc;
|
||||
|
||||
import com.alipay.sofa.rpc.context.RpcInternalContext;
|
||||
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
|
||||
import com.alipay.sofa.rpc.core.request.SofaRequest;
|
||||
import com.alipay.sofa.rpc.core.response.SofaResponse;
|
||||
import com.alipay.sofa.rpc.ext.Extension;
|
||||
import com.alipay.sofa.rpc.filter.AutoActive;
|
||||
import com.alipay.sofa.rpc.filter.Filter;
|
||||
import com.alipay.sofa.rpc.filter.FilterInvoker;
|
||||
import io.seata.core.context.RootContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* TransactionContext on provider side.
|
||||
*
|
||||
* @author Geng Zhang
|
||||
* @since 0.6.0
|
||||
*/
|
||||
@Extension(value = "transactionContextProvider")
|
||||
@AutoActive(providerSide = true)
|
||||
public class TransactionContextProviderFilter extends Filter {
|
||||
|
||||
/**
|
||||
* Logger for this class
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionContextProviderFilter.class);
|
||||
|
||||
@Override
|
||||
public SofaResponse invoke(FilterInvoker filterInvoker, SofaRequest sofaRequest) throws SofaRpcException {
|
||||
String xid = RootContext.getXID();
|
||||
String rpcXid = getRpcXid(sofaRequest);
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("xid in RootContext[" + xid + "] xid in RpcContext[" + rpcXid + "]");
|
||||
}
|
||||
boolean bind = false;
|
||||
if (xid != null) {
|
||||
RpcInternalContext.getContext().setAttachment(RootContext.KEY_XID, xid);
|
||||
} else {
|
||||
if (rpcXid != null) {
|
||||
RootContext.bind(rpcXid);
|
||||
bind = true;
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("bind[" + rpcXid + "] to RootContext");
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
return filterInvoker.invoke(sofaRequest);
|
||||
} finally {
|
||||
if (bind) {
|
||||
String unbindXid = RootContext.unbind();
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("unbind[" + unbindXid + "] from RootContext");
|
||||
}
|
||||
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
|
||||
if (LOGGER.isWarnEnabled()) {
|
||||
LOGGER.warn("xid in change during RPC from " + rpcXid + " to " + unbindXid);
|
||||
}
|
||||
if (unbindXid != null) {
|
||||
RootContext.bind(unbindXid);
|
||||
if (LOGGER.isWarnEnabled()) {
|
||||
LOGGER.warn("bind [" + unbindXid + "] back to RootContext");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get rpc xid
|
||||
* @return
|
||||
*/
|
||||
private String getRpcXid(SofaRequest sofaRequest) {
|
||||
String rpcXid = (String) sofaRequest.getRequestProp(RootContext.KEY_XID);
|
||||
if (rpcXid == null) {
|
||||
rpcXid = (String) sofaRequest.getRequestProp(RootContext.KEY_XID.toLowerCase());
|
||||
}
|
||||
return rpcXid;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
io.seata.integration.sofa.rpc.TransactionContextProviderFilter
|
||||
io.seata.integration.sofa.rpc.TransactionContextConsumerFilter
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed 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 io.seata.integration.sofa.rpc;
|
||||
|
||||
/**
|
||||
* @author Geng Zhang
|
||||
*/
|
||||
public interface HelloService {
|
||||
|
||||
String sayHello(String name, int age);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed 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 io.seata.integration.sofa.rpc;
|
||||
|
||||
import io.seata.core.context.RootContext;
|
||||
|
||||
/**
|
||||
* @author Geng Zhang
|
||||
*/
|
||||
public class HelloServiceImpl implements HelloService {
|
||||
|
||||
private String result;
|
||||
|
||||
private String xid;
|
||||
|
||||
public HelloServiceImpl() {
|
||||
|
||||
}
|
||||
|
||||
public HelloServiceImpl(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sayHello(String name, int age) {
|
||||
xid = RootContext.getXID();
|
||||
return result != null ? result : "hello " + name + " from server! age: " + age;
|
||||
}
|
||||
|
||||
public String getXid() {
|
||||
return xid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed 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 io.seata.integration.sofa.rpc;
|
||||
|
||||
import io.seata.core.context.RootContext;
|
||||
|
||||
/**
|
||||
* @author Geng Zhang
|
||||
*/
|
||||
public class HelloServiceProxy implements HelloService {
|
||||
|
||||
private String xid;
|
||||
|
||||
private HelloService proxy;
|
||||
|
||||
public HelloServiceProxy(HelloService proxy) {
|
||||
this.proxy = proxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sayHello(String name, int age) {
|
||||
xid = RootContext.getXID();
|
||||
return proxy.sayHello(name, age);
|
||||
}
|
||||
|
||||
public String getXid() {
|
||||
return xid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed 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 io.seata.integration.sofa.rpc;
|
||||
|
||||
import com.alipay.sofa.rpc.config.ConsumerConfig;
|
||||
import com.alipay.sofa.rpc.config.ProviderConfig;
|
||||
import com.alipay.sofa.rpc.config.ServerConfig;
|
||||
import com.alipay.sofa.rpc.context.RpcInternalContext;
|
||||
import com.alipay.sofa.rpc.context.RpcInvokeContext;
|
||||
import com.alipay.sofa.rpc.context.RpcRunningState;
|
||||
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
|
||||
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
|
||||
import io.seata.core.context.RootContext;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Geng Zhang
|
||||
*/
|
||||
public class TransactionContextFilterTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
HelloServiceImpl helloServiceImpl;
|
||||
HelloService helloServiceRef;
|
||||
HelloServiceProxy helloServiceProxy;
|
||||
HelloService helloService;
|
||||
|
||||
// mock A -> B -> C
|
||||
|
||||
{ // C
|
||||
ServerConfig serverConfig1 = new ServerConfig()
|
||||
.setStopTimeout(0).setPort(22222)
|
||||
.setQueues(5).setCoreThreads(1).setMaxThreads(1);
|
||||
helloServiceImpl = new HelloServiceImpl();
|
||||
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
|
||||
.setInterfaceId(HelloService.class.getName())
|
||||
.setRef(helloServiceImpl)
|
||||
.setServer(serverConfig1)
|
||||
.setUniqueId("x1")
|
||||
.setRegister(false);
|
||||
providerConfig.export();
|
||||
}
|
||||
{ // B
|
||||
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
|
||||
.setInterfaceId(HelloService.class.getName())
|
||||
.setTimeout(1000)
|
||||
.setDirectUrl("bolt://127.0.0.1:22222")
|
||||
.setUniqueId("x1")
|
||||
.setRegister(false);
|
||||
helloServiceRef = consumerConfig.refer();
|
||||
|
||||
ServerConfig serverConfig2 = new ServerConfig()
|
||||
.setStopTimeout(0).setPort(22223)
|
||||
.setQueues(5).setCoreThreads(1).setMaxThreads(1);
|
||||
helloServiceProxy = new HelloServiceProxy(helloServiceRef);
|
||||
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
|
||||
.setInterfaceId(HelloService.class.getName())
|
||||
.setRef(helloServiceProxy)
|
||||
.setServer(serverConfig2)
|
||||
.setUniqueId("x2")
|
||||
.setRegister(false);
|
||||
providerConfig.export();
|
||||
}
|
||||
{ // A
|
||||
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
|
||||
.setInterfaceId(HelloService.class.getName())
|
||||
.setTimeout(1000)
|
||||
.setDirectUrl("bolt://127.0.0.1:22223")
|
||||
.setUniqueId("x2")
|
||||
.setRegister(false);
|
||||
helloService = consumerConfig.refer();
|
||||
}
|
||||
|
||||
try {
|
||||
helloService.sayHello("xxx", 22);
|
||||
// check C
|
||||
Assertions.assertNull(helloServiceImpl.getXid());
|
||||
// check B
|
||||
Assertions.assertNull(helloServiceProxy.getXid());
|
||||
} catch (Exception e) {
|
||||
Assertions.assertTrue(e instanceof SofaRpcException);
|
||||
} finally {
|
||||
Assertions.assertNull(RootContext.unbind());
|
||||
}
|
||||
|
||||
RootContext.bind("xidddd");
|
||||
try {
|
||||
helloService.sayHello("xxx", 22);
|
||||
// check C
|
||||
Assertions.assertEquals(helloServiceImpl.getXid(), "xidddd");
|
||||
// check B
|
||||
Assertions.assertEquals(helloServiceProxy.getXid(), "xidddd");
|
||||
} catch (Exception e) {
|
||||
Assertions.assertTrue(e instanceof SofaRpcException);
|
||||
} finally {
|
||||
Assertions.assertEquals("xidddd", RootContext.unbind());
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void adBeforeClass() {
|
||||
RpcRunningState.setUnitTestMode(true);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void adAfterClass() {
|
||||
RpcRuntimeContext.destroy();
|
||||
RpcInternalContext.removeContext();
|
||||
RpcInvokeContext.removeContext();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user