试一试Seata

安装Seata

  1. https://github.com/seata/seata/releases下载最新版的安装包

  2. 解压后的conf目录中的两个重要配置文件

    file.conf:配置存储方式透传事务信息的NIO事务组配置锁配置等信息,file.conf.example文件中有可以参考的样例。这种文件形式的配置默认对应registry.conf文件内的file方式配置。

    registry.conf:配置服务注册方式配置读取方式。registry{}中是注册中心相关配置,config{}中是配置中心相关配置。

    详细配置项的含义参考http://seata.io/zh-cn/docs/user/configurations.html

  3. seata启动

    sh seata-server.sh -p 8091 -h 127.0.0.1 -m db

    -h: 注册到注册中心的ip
    -p: Server rpc 监听端口
    -m: 全局事务会话信息存储模式,file、db,优先读取启动参数
    -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
    -e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
    

    –storeMode, -m
    log store mode : file、db
    Default: file

demo工程

  1. 工程介绍
    用户购买商品的业务逻辑。整个业务逻辑由3个微服务提供支持:

    • 仓储服务:对给定的商品扣除仓储数量。
    • 订单服务:根据采购需求创建订单。
    • 帐户服务:从用户帐户中扣除余额。

    在官网有比较详细的介绍 http://seata.io/zh-cn/docs/user/quickstart.html

  2. 下载demo工程 https://github.com/seata/seata-samples

    我是用里面的springcloud-eureka-feign-mybatis-seata这个工程尝试的。业务就是仓储-订单-账户这套系统。涉及的内容也比较全面,包括

    注册中心:eureka
    服务间调用:feign
    持久层:mybatis
    数据库:mysql 5.7.22
    Springboot:2.1.7.RELEASE
    Springcloud:Greenwich.SR2
    jdk:1.8
    seata:我用的1.2.0版本

  3. 运行demo

    • 先启动注册中心eureka。后启seata和订单、库存、账户服务(三个RM)。seata要作为TC(事务协调者)注册上来的。

    • 这里我们的seata是基于db的配置的,要建立数据库seata, 再建立三个表global_table,branch_table,lock_table,建表sql在https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

      修改seata/conf目录下的file.conf文件,指定到我们创建的数据库。另外自己下载的seata的file.conf

    • undo_log是存储在业务库,订单、库存、账户每个项目的数据库中都要建立undo_log表,sql在 https://github.com/seata/seata/blob/develop/script/client/at/db/mysql.sql>。每个业务系统都有file.conf和registry.conf文件. registry.conf文件中的registry节点修改type为eureka,其他不用动,config节点我们默认就是使用file配置的,file指向file.conf。

      file.conf中的service配置中的default.grouplist要指向我们运行seata的ip和端口;vgroupMapping.my_test_tx_group = "default"中的这一项要改成vgroupMapping.fsp_tx_group = “default” , 与application.yml中的spring.cloud.alibaba.seata.tx-service-group: fsp_tx_group对应。这一条配置是作者自定义的一个事务组名称。

    • 都跑起来没有报错的时候,测试接口 http://localhost:8180/order/create,参数

      {
      “userId”:“1”,
      “productId”:“1”,
      “count”:“5”,
      “money”:“10”
      }

      注意把demo中OrderController中的create方法改成@PostMapping,请求参数前面加上@RequestBody,这样就能正常运行了。作者可能没来得及调试程序。

  4. 调用接口,观察中间状态和结束状态表格记录
    AccountServiceImpl中也有Thread.sleep(30*1000);代码模拟超时,全局事务回滚的情况。把这段代码的注释打开,请求接口10秒后失败,在这10秒内可以看到数据库每个表的中间状态,10秒后请求失败,事务回滚,订单、库存、账户的表数据恢复,seata的三个表global_table,branch_table,lock_table中本次请求相关的记录被删除。

我遇到的问题:

  1. 如果在windows下跑,可能出现这个错误,linux上没有
D:\pkg\seata-server-1.2.0\seata\bin>seata-server.bat -p 8091 -h 127.0.0.1 -m db
Java HotSpot(TM) 64-Bit Server VM warning: Cannot open file D:\pkg\seata-server-
1.2.0\seata\bin\\../logs/seata_gc.log due to No such file or directory

在seata目录下建logs目录即可

  1. 内存不够可以先调小内存seata-server.cmd(或seata-server.sh)中
D:\pkg\seata-server-1.2.0\seata\bin>seata-server.bat -p 8091 -h 127.0.0.1 -m db
Error occurred during initialization of VM
Could not reserve enough space for object heap

调小启动脚本中的内存设置

猜你喜欢

转载自blog.csdn.net/u013041642/article/details/105870104
今日推荐