codingapi / codingapi/tx-lcn

关于5.0.2版本未修复bug临时解决方案

Open
#454 4 comments 2 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
4.2k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

## TC集群环境下,分布式事务回调用,会路由到非发起方机器上去

[参考](https://github.com/codingapi/tx-lcn/issues/296)

### TM端

同样采用替换源码的方式,复制出源码到同包名(自己新建)下

1. `com.codingapi.txlcn.tm.txmsg.transaction.JoinGroupExecuteService.java`
把rpcClient.getAppName改成rpcClient.getModId
```
transactionManager.join(dtxContext, joinGroupParams.getUnitId(), joinGroupParams.getUnitType(),
rpcClient.getModId(transactionCmd.getRemoteKey()), joinGroupParams.getTransactionState());
```

2. `com.codingapi.txlcn.txmsg.netty.impl.NettyRpcClient.java` 及`com.codingapi.txlcn.txmsg.RpcClient.java`

添加方法

```
@Override
public String getModId(String remoteKey) {
return SocketManager.getInstance().getModuleId(remoteKey);
}
```

3. `com.codingapi.txlcn.txmsg.netty.bean.SocketManager.java`

1) 添加方法
```
/**
* 获取模块的唯一ID
*
* @param remoteKey 远程唯一标识
* @return 模块名称
*/
public String getModuleId(String remoteKey) {
AppInfo appInfo = appNames.get(remoteKey);
return appInfo == null ? null : appInfo.getLabelName();
}

```
2) 修改remoteKeys(5.0.2源码里面是removeKeys,估计是单词错误直接注释掉),根据modId取remoteKeys
```
/**
* 获取模块的远程标识keys
*
* @param modId 模块唯一识别Id
* @return remoteKeys
*/
public List remoteKeys(String modId) {
List allKeys = new ArrayList<>();
for (Channel channel : channels) {
if (modId.equals(getModuleIdFromChannel(channel))) {
allKeys.add(channel.remoteAddress().toString());
}
}
return allKeys;
}

```

3) 新增getModuleIdFromChannel
```
/**
* 获取模块名称
*
* @param channel 管道信息
* @return 模块名称
*/
public String getModuleIdFromChannel(Channel channel) {
String key = channel.remoteAddress().toString();
return getModuleId(key);
}
```

### TC端

在TC端,implement ModIdProvider确保每个tc都有唯一识别id即可。

```
@Component
public class MyModIdProvider implements ModIdProvider {
@Override
public String modId(){
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return address.getHostAddress() + ":" + this.serverPort;
}

}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.