跟Seata爱恨情仇的一天

      最近两天入坑SpringCloud Ali,项目已经搭起来,服务之间的调用碰到一些小问题也解决了,但是服务之间在调用的时候出现异常却无法保证事务的正常回滚,百度了一圈,发现Seata在百度搜索的最上面,于是我决定要把它集成到我的项目里面,然而这缺失痛苦的开始。。

我的项目结构如下,将Feign调用的服务接口统一放在了API中,一些公共代码放在了commons模块里面,我这里用到的主要有三个木块,分别是用户 zous-cloud-fix-user、分布式ID生成 zous-cloud-fix-leaf、认证登录模块 zous-cloud-fix-auth,当我在用户模块中调用完分布式ID生成服务,并且保存完用户的基础信息,将要存储用户的账户信息时出现异常,这时候用户信息录入账户信息却因异常没有录入数据库,这时候我希望他能统一回滚,于是我盯上了Seata。

官网

https://seata.io/zh-cn/docs/user/quickstart/

在集成Seata的过程中,我们首先要过一遍文档,文档上很多信息很重要,比如在最近发布的版本中配置已经完全在application.yml文件中,那么要怎么去配置application.yml文件?官方给出了 application.example.yml 文件,在里面给出了Seata各个模式下的完整配置,各位可以自行参考。完整遵循此过程,可以避免很多不理解的问题以及配置的误解。

下面描述一下我集成Seata的过程:

拉取镜像

我用Docker拉取了 seataio/seata-server:1.6.1 的镜像到本地,命令如下

docker pull seataio/seata-server:1.6.1

挂载配置至磁盘

我按照官网的指示,先启动了一遍Seata并将resources文件夹挂载到本地并重启了seata服务

docker run -d --name seata-server -p 8091:8091 -p 7091:7091 -v F:\all-dev\resources:/seata-server/resources seataio/seata-server:1.6.1

修改Seata配置文件  application.yml

在这里,我指定了将Seata服务注册至nacos,并引用了nacos中定义的 seataServer.properties 配置文件,将其存储修改为数据库存储

查看nacos注册信息

服务启动成功后在服务列表可以看到seata服务,若没有看到,可以检查一下yml配置中nacos配置是否正确

完成上述步骤后,seata服务端算是已经配置完了。

接下来时在cloud项目中集成Seata

seata:
  config:
    type: nacos
    nacos:
      server-addr: 192.168.1.7:8848
      dataId: "seataServer.properties"
      tx-service-group: default_tx_group
      group: SEATA_GROUP
      username: nacos
      password: nacos

将配置加入至应用yml文件中,在这里我碰到了今天最难缠的异常

io.seata.common.exception.FrameworkException: No available service

该异常是因为在项目中Seata配置信息没有配置正确而无法正确获取Seata服务引起,请务必再次检查Seata配置是否与注册Seata服务时一致,并修改Seata配置 seataServer.properties 中数据库连接等配置信息

#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
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
transport.serialization=seata
transport.compressor=none

#Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=192.168.1.7:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

#Transaction rule configuration, only for the client
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=true
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
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
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h

#Log rule configuration, for client and server
log.exceptionRate=100

#Transaction storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=DB
store.lock.mode=DB
store.session.mode=DB
#Used for password encryption
store.publicKey=

#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100

#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.7:3306/fix-seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=root
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

#These configurations are required if the `store mode` is `redis`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `redis`, you can remove the configuration block.
store.redis.mode=single
store.redis.single.host=192.168.1.7
store.redis.single.port=6379
store.redis.sentinel.masterName=
store.redis.sentinel.sentinelHosts=
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100

#Transaction rule configuration, only for the server
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
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false

#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值