[seata]求助大佬: 集成seata,分布式事务未回滚

2024-02-02 627 views
4

Seata版本1.3.0

SpringBoot版本 2.3.0.RELEASE

Seata依赖

`

io.seata
        <artifactId>seata-all</artifactId>
        <version>1.7.0</version>
    </dependency>

    <!--If your project base on `Spring Boot`, you can directly use the following dependencies-->
    <!--Notice: `seata-spring-boot-starter` has already included `seata-all` dependency-->
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-all</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-spring-boot-starter</artifactId>
        <version>1.7.0</version>
    </dependency>

    <!--      seata序列化    -->
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-serializer-kryo</artifactId>
        <version>1.7.0</version>
    </dependency>`

Server1 Seata 配置如下:

seata.client.undo.log-serialization=kryo seata.data-source-proxy-mode=AT seata.application-id=gallop-admin seata.service.grouplist.default=192.168.xx.xx:8091 seata.service.vgroup-mapping.demo-gallop-admin-group=default seata.tx-service-group=demo-gallop-admin-group

Server2 Seata 配置如下:

seata.client.undo.log-serialization=kryo seata.data-source-proxy-mode=AT seata.application-id=gallop-remote seata.service.grouplist.default=192.168.xx.xx:8091 seata.service.vgroup-mappingdemo-gallop-remote-group=default seata.tx-service-group=demo-gallop-remote-group

代码如下:

` @Resource private RemoteCommandClient remoteCommandClient;

@GlobalTransactional(rollbackFor = Exception.class)
@Override
public ResponseResult addOrUpdateUserTest(User user) {
    userMapper.insert(user);
    ResponseResult result = remoteCommandClient.updateCMDState("f514b2c693d5495384ad5f61191f34f2","我来了");
    userMapper.insert(user);
    return null;
}

@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public ResponseResult updateCMDState(String commandId, String commandResponse) {
    RemoteCommandsHistory history = new RemoteCommandsHistory();
    history.setCommandId(commandId);
    history.setCommandResponse(commandResponse);
    history.setCommandResponseTime(LocalDateTime.now());
    return remoteCommandsHistoryMapper.updateById(history) == 0 ? ResponseResult.ok(ResponseCode.FAIL, 
    ResponseMessage.FAIL) : ResponseResult.ok(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
}

`

运行结果: 运行到第二个userMapper.insert会出现主键冲突异常 此时第一个userMapper.insert回滚 remoteCommandClient.updateCMDState未回滚

seata日志如下:

seata-log

求大佬指点!!!

回答

2

第二个服务没有注册分支上来,感觉可以检查下XID的传递,看使用的什么rpc框架

8

我在第二个服务的方法上面加上@GlobalTransactional,也会出现事务执行的XID,vgroup等信息,但是和第一个服务不是同一个XID, 感觉还是那里配置不对

7

比如你是feign调用,springcloud-alibaba会自动在http请求头里把上游的XID传递到下游去, 你这边这个XID应该没传递。 如果是不支持的rpc框架,就需要自己把XID传递下去。