天天看点

【Seata】之 初识

文章目录

  • ​​一、 准备工作​​
  • ​​(1)执行SQL​​
  • ​​(2)下载`Seata Sever`,并运行​​
  • ​​(3)更改项目配置​​
  • ​​(4)启动服务​​
  • ​​二、 测试​​
  • ​​(1)无错误,成功提交​​
  • ​​(2)发生异常,事务回滚​​
  • ​​三、 问题​​
  • ​​(1)数据库中`undo_log`什么时候会有记录呢?​​

这边采取​

​SpringCloud-JPA-Seata​

​来测试

按照:https://github.com/seata/seata-samples/tree/master/springcloud-jpa-seata

一、 准备工作

(1)执行SQL

在​

​springcloud-jpa-seata​

​​目录下有​

​sql/all_in_one.sql​

​,可直接执行

(2)下载​

​Seata Sever​

​,并运行

下载地址:https://github.com/seata/seata/releases

这边下载最新版本:0.7.1
// zip文件
unzip seata-server-xxx.zip

// tar.gz
tar -zxvf seata-server.ta.gz -C seata-server

sh ./bin/seata-server.sh -p 8091  -m file      

启动后如下:

【Seata】之 初识

(3)更改项目配置

例如​

​account-service​

​​服务,打开​

​application.properties​

按照自己实际的配置更新,比如:

spring.datasource.url=jdbc:mysql://localhost:3306/db_account?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root      

(4)启动服务

启动​

​account-service​

​​ ​

​busniess-service​

​​ ​

​order-service​

​​ ​

​storage-service​

​ 服务

【Seata】之 初识

二、 测试

(1)无错误,成功提交

  1. 请求访问

    ​​

    ​curl http://127.0.0.1:8084/purchase/commit​

【Seata】之 初识
  1. ​account_tbl​

    ​​ 的​

    ​id = 1​

    ​​ 的​

    ​money​

    ​​减少​

    ​5​

【Seata】之 初识
  1. ​storage_tbl​

    ​​ 的​

    ​id = 1​

    ​​ 的​

    ​count​

    ​​减少​

    ​1​

【Seata】之 初识
【Seata】之 初识

(2)发生异常,事务回滚

  1. 请求访问

    ​​

    ​curl http://127.0.0.1:8084/purchase/rollback​

【Seata】之 初识
  1. ​account-service​

    ​抛出异常
  2. ​business-service​

    ​捕获异常
【Seata】之 初识
  1. ​seata server​

    ​显示
【Seata】之 初识
【Seata】之 初识

三、 问题

(1)数据库中​

​undo_log​

​什么时候会有记录呢?