puzhibing
2026-04-10 ed838e62602c0a63a64ff4bd144a0d16cc9a7cf8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.xiaopeng.itinerary.controller;
 
import com.xiaopeng.api.feignClient.order.XpOrderClient;
import com.xiaopeng.api.model.order.XpOrder;
import com.xiaopeng.common.core.domain.R;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
 
/**
 * Seata 分布式事务测试接口
 */
@Slf4j
@RestController
@RequestMapping("/test/seata")
@Api(tags = "Seata分布式事务测试")
public class SeataTestController {
 
    @Resource
    private XpOrderClient xpOrderClient;
 
    /**
     * 测试分布式事务 - 正常提交
     * 调用order服务创建订单,正常返回,事务提交
     */
    @GetMapping("/commit")
    @ApiOperation("测试分布式事务-正常提交")
    @GlobalTransactional(name = "seata-test-commit", rollbackFor = Exception.class)
    public R<Long> testCommit() {
        XpOrder order = buildTestOrder();
        log.info("[Seata测试] 开始创建测试订单,事务正常提交场景");
        R<Long> result = xpOrderClient.addOrder(order);
        log.info("[Seata测试] 订单创建结果: code={}, orderId={}", result.getCode(), result.getData());
        return R.ok(result.getData(), "分布式事务正常提交成功,订单ID: " + result.getData());
    }
 
    /**
     * 测试分布式事务 - 回滚
     * 调用order服务创建订单后,手动抛异常触发全局回滚
     * 验证:调用后去数据库查,订单应该不存在
     */
    @GetMapping("/rollback")
    @ApiOperation("测试分布式事务-回滚")
    @Transactional
    @ShardingSphereTransactionType(TransactionType.BASE)
    public R<String> testRollback() {
        XpOrder order = buildTestOrder();
        log.info("[Seata测试] 开始创建测试订单,事务回滚场景");
        R<Long> result = xpOrderClient.addOrder(order);
        log.info("[Seata测试] 订单已创建 orderId={},即将抛出异常触发回滚...", result.getData());
        // 故意抛异常,触发Seata全局事务回滚
        throw new RuntimeException("[Seata测试] 模拟业务异常,触发分布式事务回滚");
    }
 
    private XpOrder buildTestOrder() {
        LocalDateTime now = LocalDateTime.now();
        XpOrder order = new XpOrder();
        // 主键由数据库或框架生成,不设置
        order.setAppUserId(99999L);
        order.setCityId(1L);
        order.setVehicleId(1001L);
        order.setFleetId(1L);
        order.setOrderNumber("SEATA_TEST_" + System.currentTimeMillis());
        order.setTransactionId("TXN_SEATA_TEST_" + System.currentTimeMillis());
        order.setOrderSource(1);
        order.setRideTime(now);
 
        // 下单地点(深圳南山)
        order.setPlacementLon(113.934528);
        order.setPlacementLat(22.530381);
        order.setPlacementAddress("Seata分布式事务测试-下单地点");
 
        // 出发地
        order.setStartLon(113.934528);
        order.setStartLat(22.530381);
        order.setStartAddress("测试起点-科技园");
 
        // 目的地
        order.setEndLon(113.944528);
        order.setEndLat(22.540381);
        order.setEndAddress("测试终点-深圳湾");
 
        // 上车点
        order.setBoardingLon(113.934528);
        order.setBoardingLat(22.530381);
        order.setBoardingAddress("测试上车点-科技园地铁站");
        order.setBoardingTime(now);
 
        // 下车点
        order.setGetoffLon(113.944528);
        order.setGetoffLat(22.540381);
        order.setGetoffAddress("测试下车点-深圳湾公园");
        order.setGetoffTime(now.plusMinutes(15));
 
        // 支付信息
        order.setPayType(1);
        order.setOrderMoney(new BigDecimal("25.00"));
        order.setPayMoney(new BigDecimal("20.00"));
        order.setPrepayMoney(new BigDecimal("25.00"));
 
        // 后备箱
        order.setOpenTrunk(0);
 
        // 里程费用
        order.setStartMileage(3.0);
        order.setStartMoney(new BigDecimal("10.00"));
        order.setMileageKilometers(5.5);
        order.setMileageMoney(new BigDecimal("8.00"));
 
        // 时长费用
        order.setDuration(15.0);
        order.setDurationType(1);
        order.setDurationMoney(new BigDecimal("5.00"));
 
        // 等待费用
        order.setWait(2.0);
        order.setWaitMoney(new BigDecimal("2.00"));
 
        // 远途费用
        order.setLongDistance(0.0);
        order.setLongDistanceType(1);
        order.setLongDistancemoney(new BigDecimal("0.00"));
 
        // 优惠
        order.setRedPacketMoney(new BigDecimal("3.00"));
        order.setCouponMoney(new BigDecimal("2.00"));
        order.setRedPacketId(0L);
        order.setCouponId(0L);
 
        // 订单状态:待接单
        order.setStatus(1);
 
        // 城市信息
        order.setCityName("深圳市");
        order.setCityCode("440300");
 
        // 站点信息
        order.setStartPointId(1L);
        order.setEndPointId(2L);
        order.setStartPointName("科技园站");
        order.setEndPointName("深圳湾站");
 
        // 预估时间
        order.setEstimatedTime(now.plusMinutes(20));
 
        // 区域与时段
        order.setXpElectronicFenceAreaId(1L);
        order.setTimeSlot("08:00-10:00");
 
        // 车辆到达、接单、行程时间
        order.setArrivalTime(now.plusMinutes(5));
        order.setAcceptanceTime(now);
        order.setTripBegin(now.plusMinutes(5));
        order.setTripEnd(now.plusMinutes(20));
 
        // 队列名称
        order.setQueueName("default_queue");
 
        // BaseModel 字段(通常自动填充,这里显式设置避免null)
        order.setCreateBy("seata-test");
        order.setCreateTime(now);
        order.setUpdateBy("seata-test");
        order.setUpdateTime(now);
        order.setDeleted(false);
 
        return order;
    }
 
}