代码拉取完成,页面将自动刷新
一个模拟Spring应用,Saga事务的管理,假设用户有4元余额,想购买一个3元产品
如下是购买流程
String orderAddUrl = "http://127.0.0.1:8081/order/item/{orderId}/{userId}/{fee}";
String userBalanceUpdateUrl = "http://127.0.0.1:8082/user/fee/{orderId}/{userId}/{fee}";
SagaContext sagaContext = SagaContext.sagaContextFactory.current();
try {
sagaContext.start(gid);
//模拟调用俩个微服务,订单和用户
rest.postForEntity(orderAddUrl, null,String.class, paras);
rest.postForEntity(userBalanceUpdateUrl, null,String.class, paras);
if (1 == 1) {
throw new RuntimeException("模拟失败,查询saga-server 看效果");
}
} catch (Exception e) {
sagaContext.rollback();
return e.getMessage();
}
{
"success": true,
"msg": "成功",
"data": [
{
"id": "a717d181-608b-4f99-ab8a-9c60e83cce12",
"gid": "123",
"appName": "userSystem",
"status": "Success",
"rollbackStatus": "Success",
"time": 1138055785025836,
"taskInfo": "{\"tasks\":[{\"@Clazz\":\"org.beetl.sql.saga.ms.client.SagaClientTransaction$KafkaSagaTaskTrace\",\"rollbackTask\":{\"@Clazz\":\"org.beetl.sql.saga.common.ami.SagaUpdateByIdAMI$UpdateSagaRollbackTask\",\"sqlManagerName\":\"mySqlManager\",\"obj\":{\"@Clazz\":\"org.beetl.sql.saga.demo.entity.UserEntity\",\"id\":\"xiandafu\",\"name\":\"闲大赋\",\"balance\":4}},\"success\":false}],\"success\":true}",
"createTime": 1607864163823,
"updateTime": 1607864164067
},
{
"id": "c4765a46-cf2f-4d7a-a714-dc35bf723df2",
"gid": "123",
"appName": "orderSystem",
"status": "Success",
"rollbackStatus": "Success",
"time": 1138055465293352,
"taskInfo": "{\"tasks\":[{\"@Clazz\":\"org.beetl.sql.saga.ms.client.SagaClientTransaction$KafkaSagaTaskTrace\",\"rollbackTask\":{\"@Clazz\":\"org.beetl.sql.saga.common.ami.SagaInsertAMI$InsertSagaRollbackTask\",\"sqlManagerName\":\"mySqlManager\",\"entityClass\":\"org.beetl.sql.saga.demo.entity.OrderEntity\",\"pkId\":\"4a40f48b-4b29-4b62-8d47-5f3867b03afd\"},\"success\":false}],\"success\":true}",
"createTime": 1607864163570,
"updateTime": 1607864164058
},
{
"id": "48eb2fbe-9ff8-4913-bfd2-63176b3646b4",
"gid": "123",
"appName": "demoSystem",
"status": "Error",
"rollbackStatus": "Success",
"time": 1138055258883126,
"taskInfo": "{\"tasks\":[],\"success\":true}",
"createTime": 1607864163543,
"updateTime": 1607864164052
}
],
"errorCode": 0
}
status字段标识业务执行是否成功,rollbackStatus标识回滚是否执行成功,如上可以看到userSystem 系统和orderSystem都执行成功,但demoSystem执行失败抛出异常,导致所有三个系统都回滚
如下为是使用的topic和数据库
./kafka-topics.sh --delete --zookeeper 127.0.0.1:2181 --topic saga-client-demoSystem
./kafka-topics.sh --delete --zookeeper 127.0.0.1:2181 --topic saga-client-userSystem
./kafka-topics.sh --delete --zookeeper 127.0.0.1:2181 --topic saga-client-orderSystem
./kafka-topics.sh --delete --zookeeper 127.0.0.1:2181 --topic saga-server-topic
rm -rf ~/.h2
ls
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。