|
@@ -0,0 +1,70 @@
|
|
|
|
|
+package com.jzg.quotation.summary.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.jzg.commons.core.page.HttpResult;
|
|
|
|
|
+import com.jzg.quotation.summary.client.SeataTestOrgClient;
|
|
|
|
|
+import io.seata.core.context.RootContext;
|
|
|
|
|
+import io.seata.spring.annotation.GlobalTransactional;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/seataTest")
|
|
|
|
|
+public class SeataTestController {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(SeataTestController.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private SeataTestOrgClient seataTestOrgClient;
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping("/success")
|
|
|
|
|
+ @GlobalTransactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public HttpResult<String> testSuccess(@RequestParam(defaultValue = "test") String name) {
|
|
|
|
|
+ String xid = RootContext.getXID();
|
|
|
|
|
+ log.info("【quotation-summary 发起方】开启全局事务,XID=[{}]", xid);
|
|
|
|
|
+
|
|
|
|
|
+ jdbcTemplate.update("INSERT INTO seata_test (name, source) VALUES (?, ?)", name, "quotation-summary");
|
|
|
|
|
+ log.info("【quotation-summary 发起方】本地写入成功");
|
|
|
|
|
+
|
|
|
|
|
+ HttpResult<String> result = seataTestOrgClient.seataTestWrite(name);
|
|
|
|
|
+ log.info("【quotation-summary 发起方】远程调用 organization 结果:{}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return HttpResult.ok("分布式事务测试-成功路径,XID=" + xid + ",org结果=" + result.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping("/rollback")
|
|
|
|
|
+ @GlobalTransactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public HttpResult<String> testRollback(@RequestParam(defaultValue = "test") String name) {
|
|
|
|
|
+ String xid = RootContext.getXID();
|
|
|
|
|
+ log.info("【quotation-summary 发起方】开启全局事务(回滚测试),XID=[{}]", xid);
|
|
|
|
|
+
|
|
|
|
|
+ jdbcTemplate.update("INSERT INTO seata_test (name, source) VALUES (?, ?)", name, "quotation-summary-rollback");
|
|
|
|
|
+ log.info("【quotation-summary 发起方】本地写入成功,即将调用 organization 故意失败接口");
|
|
|
|
|
+
|
|
|
|
|
+ HttpResult<String> remoteResult = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ remoteResult = seataTestOrgClient.seataTestWriteFail(name);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.info("【quotation-summary 发起方】Feign 调用异常:{},分布式事务将回滚", e.getMessage());
|
|
|
|
|
+ throw e;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 双重保障:即使 Feign 没抛异常,检查返回码,非 200 也触发回滚
|
|
|
|
|
+ if (remoteResult == null || remoteResult.getCode() != 200) {
|
|
|
|
|
+ String errMsg = remoteResult != null ? remoteResult.getMsg() : "远程调用返回为空";
|
|
|
|
|
+ log.info("【quotation-summary 发起方】远程调用返回错误码 [{}]:{},手动触发回滚",
|
|
|
|
|
+ remoteResult != null ? remoteResult.getCode() : "null", errMsg);
|
|
|
|
|
+ throw new RuntimeException("远程调用失败: " + errMsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return HttpResult.ok("不会走到这里");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|