整这个demo 整了几天 分布式事务始终不回滚 差点让我怀疑人生。。。。记录零零散散的东西
demo 参考的是这个大兄弟的 https://gitee.com/zjoin/seata-study.git 万分感谢
本次demo地址:https://gitee.com/jamesPanNew/seata-study.git
不说整合版本就是耍流氓
Springcloud Hoxton.SR9
seata 1.4.0
mybatis-plus 3.4.1
Springboot 2.3.7.RELEASE
首先服务端要装nacos 和seata-server seata-server服务端尽量和用的版本一致 我这里用的是1.4.0 用docker安装的 安装略
seata 服务端要启动
大致就是 nacos-stock ->feign调用nacos-order 插入一条数据 然后在nacos-stock 手动抛异常 看nacos-order 模块插入的数据是否回滚
需要引入jar包
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-seata</artifactId><version>2.2.0.RELEASE</version><exclusions><exclusion><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId></exclusion><exclusion><groupId>io.seata</groupId><artifactId>seata-all</artifactId></exclusion></exclusions></dependency><!--seata--><dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.4.0</version></dependency>
需要的seata 配置文件
seata:enabled: trueapplication-id: ${spring.application.name} #这个模块的项目名 nacos-stocktx-service-group: ${spring.application.name}-group #事务组名称 nacos-stock-group 一定要与下面的对应 service-vgroup-mapping对应enable-auto-data-source-proxy: truedata-source-proxy-mode: ATuse-jdk-proxy: falseclient:rm:async-commit-buffer-limit: 10000report-retry-count: 5table-meta-check-enable: falsereport-success-enable: falsesaga-branch-register-enable: falsesaga-json-parser: fastjsonlock:retry-interval: 10retry-times: 30retry-policy-branch-rollback-on-conflict: truetm:commit-retry-count: 5rollback-retry-count: 5default-global-transaction-timeout: 60000degrade-check: falsedegrade-check-period: 2000degrade-check-allow-times: 10undo:data-validation: truelog-serialization: jacksonlog-table: undo_logonly-care-update-columns: trueservice:vgroup-mapping:nacos-stock-group: defaultgrouplist:default: 192.168.0.243:8095 #seata 服务端ipenable-degrade: falsedisable-global-transaction: falsetransport:shutdown:wait: 3thread-factory:boss-thread-prefix: NettyBossworker-thread-prefix: NettyServerNIOWorkerserver-executor-thread-prefix: NettyServerBizHandlershare-boss-worker: falseclient-selector-thread-prefix: NettyClientSelectorclient-selector-thread-size: 1client-worker-thread-prefix: NettyClientWorkerThreadworker-thread-size: defaultboss-thread-size: 1type: TCPserver: NIOheartbeat: trueserialization: seatacompressor: noneenable-client-batch-send-request: trueconfig:type: nacosnacos:namespace: ""server-addr: 192.168.0.243:8848 #注册中心地址group: SEATA_GROUPusername: ""password: ""registry:type: nacosload-balance: RandomLoadBalanceload-balance-virtual-nodes: 10file:name: file.confnacos:application: seata-serverserver-addr: 192.168.0.243:8848 #注册中心地址group : SEATA_GROUPnamespace: ""username: ""password: ""log:exception-rate: 100
nacos 上也一定要有
启动两个服务 seata-server 会有注册这两个服务的日志,表示连接成功
分布式事务注解
@GlobalTransactional
然后调用这个接口
手动抛异常 然后对应的数据会回滚 详情看项目地址
demo地址:https://gitee.com/jamesPanNew/seata-study.git