《分布式事务》https://blog.csdn.net/u011060911/article/details/122210788上面的文章系统介绍了分布式事务相关的理论知识,本文则通过代码来展示如何在项目中来使用并落地。

目录

1.  启动 seata-server

2. SpringCloud 整合 seata

3. 启动应用


Seata 作为分布式事务的框架应用非常广泛,刚好最近公司的项目有用到,所以就写下本文以供参考。

下面就带大家亲手搭建基于 SpringBoot + SpringCloud + Nacos + Seata 这样一套框架。服务见关系图如下:

推荐大家用 Seata 1.4.2 版本,配置信息可以通过一个 dataId 在 nacos 上指定,省去了很多麻烦,并且不容易出错。

1.  启动 seata-server

修改 /conf/registry.conf 文件

registry {# file 、nacos 、eureka、redis、zk、consul、etcd3、sofatype = "nacos"nacos {application = "seata-server"serverAddr = "127.0.0.1:8849"group = "SEATA_GROUP"namespace = "baeba154-3521-468d-b5de-8efb86cbf3d9"cluster = "default"username = "nacos"password = "nacos"}}config {# file、nacos 、apollo、zk、consul、etcd3type = "nacos"nacos {serverAddr = "127.0.0.1:8849"namespace = "baeba154-3521-468d-b5de-8efb86cbf3d9"group = "SEATA_GROUP"username = "nacos"password = "nacos"# 需要在nacos上配置 dataId = "seataServer.properties"}
}

在nacos上添加 seataServer.properties 配置,内容可以拷贝 github  根据自己的实际信息修改。

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=true
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
# 事务组集群名:default
service.vgroupMapping.my_test_tx_group=default
service.vgroupMapping.order-service-group=default
service.vgroupMapping.storage-service-group=default
# 通过 service.集群名.grouplist 找到 TC 服务地址
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
store.mode=db
store.lock.mode=file
store.session.mode=file
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3307/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h

启动 seata-server

./bin/seata-server.sh 

2. SpringCloud 整合 seata

注意依赖也需要使用1.4以上的,

        <dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>1.2.0</version></dependency><dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.4.2</version></dependency>

应用配置:(这里指列出一个服务的,另一个类似)

logging:level:io:seata: debug
server:port: 9092
spring:application:name: storage-servicecloud:nacos:discovery:server-addr: 127.0.0.1:8849username: nacospassword: nacosnamespace: baeba154-3521-468d-b5de-8efb86cbf3d9group: SEATA_GROUPdatasource:druid:driverClassName: com.mysql.cj.jdbc.Driverpassword: 123456url: jdbc:mysql://localhost:3307/seata_storage?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&characterSetResults=utf8&useSSL=true&allowMultiQueries=trueusername: rootseata:application-id: ${spring.application.name}# 这里的配置对应 seataServer.properties 中的 service.vgroupMapping.storage-service-group=defaulttx-service-group: storage-service-groupenable-auto-data-source-proxy: falseconfig:type: nacosnacos:server-addr: 127.0.0.1:8849username: nacospassword: nacosnamespace: baeba154-3521-468d-b5de-8efb86cbf3d9# 微服务必须在一个组,否则服务发现不了,但Seata-server 可以在不同的组中group: SEATA_GROUPdataId: "seataServer.properties"registry:type: nacosnacos:server-addr: 127.0.0.1:8849username: nacospassword: nacosnamespace: baeba154-3521-468d-b5de-8efb86cbf3d9# 微服务必须在一个组,否则服务发现不了,但Seata-server 可以在不同的组中group: SEATA_GROUP

3. 启动应用

http://localhost:9091/order/placeOrder/rollback

http://localhost:9091/order/placeOrder/commit


如果觉得还不错的话,关注、分享、在看(关注不失联~), 原创不易,且看且珍惜~

SpringCloud 整合 Seata相关推荐

  1. springcloud整合seata

    springcloud整合seata 一.背景 二.项目结构 三.实现功能: 四.项目使用到的技术 五.整合步骤 1.引入spring-cloud-starter-alibaba-seata jar包 ...

  2. springCloud整合seata实现分布式事务

    seata简介 Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.Seata 将为用户提供了 AT.TCC.SAGA 和 XA 事务模式,为用户打造一站式的分布式 ...

  3. SpringCloud整合Seata(Docker)版本异常处理

    打印报错信息:[eoutChecker_1_1] i.s.c.r.netty.NettyClientChannelManager : Failed to get available servers: ...

  4. SpringCloud集成Seata精简入门教程

    seata注册到nacos,实现高可用 一枚路过的程序猿 https://www.jianshu.com/p/cf455eaa650a nacos下载 seata官网文档 seata下载 在Mysql ...

  5. 8.Spring Cloud Alibaba教程:整合Seata分布式事务

    概述 Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务. 更多的介绍可以参考官方文档:Seata快速入门 本篇主要是介绍Spring Cloud Alibaba ...

  6. SpringCloud 整合 Dubbo

    目录 1.介绍 2.代码实现 2.1 抽取公共模块 2.2 改造服务提供者 2.3 改造服务消费者 3.启动测试 1.介绍 Dubbo有两种使用方式: 1.基于SOA的思想,将一个单体架构拆分为web ...

  7. 分布式事务解决方案 - SpringCloud Alibaba Seata

    目录 github代码:GitHub - 18409489244/seata: 基于springcloud alibaba seata 的分布式事务demo 一.常见分布式事务解决方案 二.分布式事务 ...

  8. SpringCloud - 整合Nacos启动报错Consider defining a bean of type IClientConfig

    SpringCloud - 整合Nacos启动报错Consider defining a bean of type IClientConfig 前言 一. 尝试解决Bug的几种不合适方案 1.1 添加 ...

  9. SpringCloud整合TX-LCN5.0.2使用LCN模式实现分布式事务

    一.TM配置 pom.xml文件中添加依赖: <dependency><groupId>com.codingapi.txlcn</groupId><artif ...

最新文章

  1. 综述:解决目标检测中的样本不均衡问题
  2. python 设置开机自动启动 .py 文件
  3. 使用Navicat for Oracle新建用户无法登陆(用户名大小写问题)
  4. 运动目标检测混合高斯背景建模
  5. java.lang.IllegalArgumentException: Request header is too large
  6. navicate将远程数据库导入到本地数据库
  7. Continuous Laplacian, Functional Map, Spectral CNN
  8. 无限循环小数四则运算_无限循环小数的加减乘除及无限循环小数转换为分数形式-何长峻...
  9. 第五届蓝桥杯java试题答案_2014年第五届蓝桥杯省赛试题(JavaA组)
  10. 灰色关联分析法详细步骤解释
  11. cf 936B Sleepy Game
  12. 邮储银行的规模有多大?凭什么可以成为第6大国有银行?
  13. 如何用 MacBook 提高工作效率 【配置篇】
  14. java下雪_java多线程编程实现下雪效果
  15. 基于Python的新浪新闻文本分类
  16. UG二次开发GRIP批量打开图纸
  17. 【读书笔记】iOS-UIFont-动态下载系统提供的字体-官方代码
  18. 广播电台常用51首背景音乐
  19. [案例4-7]经理与员工工资案例
  20. C++之map-set-multimap-multiset

热门文章

  1. 大型网站存储瓶颈(广义水平拆分)
  2. 使用vue,实现前端导入excel数据
  3. 三菱系统四轴正反转参数_三菱第四轴参数.docx
  4. 超强反爬虫方案!Requests 什么的通通爬不了
  5. 康托展开(hdu1430)
  6. 荐书 | 从启蒙到进阶,值得推荐的五本少儿编程
  7. 屏蔽第三方网站中的百度广告和百度推荐
  8. C Primer Plus 第五章 复习题编程练习 答案
  9. 全局、独享、局部路由守卫
  10. 编程读取Revit中材料的渲染属性