分布式事务解决方案Seata 整合 Spring Cloud + Nacos

news2024/10/7 18:53:14

1. 简介

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

2. Docker 安装 Seata

2.1 下载镜像
docker pull seataio/seata-server:1.4.2
2.2 启动容器
docker run -d --name seata-server -p 8091:8091 seataio/seata-server:1.4.2
2.3 拷贝配置文件
# docker cp 容器名称:/seata-server 本地存储配置文件的地址
docker cp seata-server:/seata-server  /mydata/seata-server
2.4 修改配置文件
  1. 修改配置文件/mydata/seata-server/resources/registry.conf,修改为Nacos
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "nacosIP:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "nacosIP:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
    dataId = "seataServer.properties"
  }
  file {
    name = "file.conf"
  }
}

  1. 修改配置文件/mydata/seata-server/resources/file.conf,修改为DB读取
## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""

  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://localhost:3306/seata?rewriteBatchedStatements=true"
    user = "root"
    password = "root"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

}

2.6 初始化Nacos中的配置文件
  1. 新增配置文件

在这里插入图片描述

  1. 书写配置:修改MySQL连接url

在这里插入图片描述


keyvalue
Data IDseataServer.properties
GroupSEATA_GROUP
#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=127.0.0.1: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=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.rm.sqlParserType=druid
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=file
store.session.mode=file
#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.cj.jdbc.Driver
store.db.url=jdbc:mysql://localhost:3306/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=127.0.0.1
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.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=true

#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
2.7 初始化Seata数据库
  1. 初始化Seata配置表
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
create database `seata`;
use `seata`;

-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_status_gmt_modified` (`status` , `gmt_modified`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('HandleAllSession', ' ', 0);
  1. 业务回滚日志表:每个需要回滚的对应服务数据库都需要创建
-- 日志文件表 --
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
    ) ENGINE = INNODB
    AUTO_INCREMENT = 1
    DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
2.8 删旧容器
docker stop seata-server
docker rm seata-server
2.9 启动新容器
docker run -d \
--restart always \
--name seata-server \
-p 8091:8091 \
-v /mydata/seata-server:/seata-server \
-e SEATA_IP=外网IP \
-e SEATA_PORT=8091 \
seataio/seata-server:1.4.2
2.7 查看Seata注册Nacos情况

在这里插入图片描述

3. Spring Cloud 整合 Seata

3.1 引入依赖
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <version>2021.1</version>
    <exclusions>
       <!-- 排除依赖 指定版本和服务器端一致 -->
       <exclusion>
           <groupId>io.seata</groupId>
           <artifactId>seata-all</artifactId>
       </exclusion>
       <exclusion>
           <groupId>io.seata</groupId>
           <artifactId>seata-spring-boot-starter</artifactId>
       </exclusion>
   </exclusions>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-all</artifactId>
    <version>1.4.2</version>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>
3.2 添加配置文件
# 分布式事务配置
seata:
  enabled: true
  enable-auto-data-source-proxy: true #是否开启数据源自动代理,默认为true
  tx-service-group: default_tx_group  #要与配置文件中的vgroupMapping一致
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      dataId: seataServer.properties
3.3 代码示例

只需要使用一个 @GlobalTransactional 注解在业务方法上:

@GlobalTransactional
public void purchase(String userId, String commodityCode, int orderCount) {
    ......
}
3.4 常见问题:Spring 全局异常处理类捕获异常导致Seata回滚失败
@ExceptionHandler({Throwable.class})
public BaseRes validationBodyException(Exception exception) {
    // 手动回滚seata
    if (StrUtil.isNotEmpty(RootContext.getXID())) {
        log.info("分布式全局事务XID:{}", RootContext.getXID());
        try {
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
        } catch (TransactionException e) {
            e.printStackTrace();
        }
    }

    // ...code
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/572975.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

电子合同网页预览盖章效果实现

电子合同在现在应用越来越广&#xff0c;需求也就随之产生。 本篇文章主要记录两种网页盖章效果实现方式&#xff0c;自己记录一下&#xff0c; 也给需要的人提供一点思路和帮助。 效果 JqueryCSS实现 原理 通过定位盖章位置&#xff0c;之后操作图片悬浮到盖章位置 1.设置…

浙江大学计算机考研分析

关注我们的微信公众号 姚哥计算机考研 更多详情欢迎咨询 浙江大学&#xff08;A&#xff09;考研难度&#xff08;☆☆☆☆☆☆&#xff09; 浙江大学计算机科学与技术学院成立于1978年&#xff0c;始终秉承“人为本&#xff0c;和为贵&#xff0c;变则通”的文化理念&#…

SecureCRT日志设置每行时间

SecureCRT日志设置时针对每个会话单独设置的 下图两个串口打印的地方&#xff0c;每个是一个会话。 打开【选项】 按照如下选项进行配置&#xff1a; 每次断开重新链接都会重新存一个日志文件&#xff0c;文件生成时间以秒为最小单位。 并且每行都有时间记录。 一般使用”年…

聊聊得物数据研发优化策略 | 精选

1.前言 在离线数据研发中&#xff0c;随着业务的快速发展以及业务复杂度的不断提高&#xff0c;数据量的不断增长&#xff0c;尤其得物这种业务的高速增长&#xff0c;必然带来数据逻辑复杂度的提升&#xff0c;数据量越大&#xff0c;复杂度越高&#xff0c;对任务的性能的要…

【嵌入式Linux】源码菜单配置 | 编译 | 菜单配置的实现 | 源码编译的实现

源码配置编译 源码配置编译,要把中间各个环节都理清楚 厂商把自己增加的东西专门放了个文件独立&#xff0c;方便开发者发现变化 1.菜单配置 移植的第一步&#xff0c;就是选配&#xff0c;通过make menuconfig图形化界面选配 //载入配置 $ make ARCHarm64 tegra_defconfi…

JVM(HotSpot)

1、 类加载机制&#xff1a; 引导类&#xff08;Bootstrap &#xff09;加载器&#xff1a;负责加载支撑JVM运行的位于JRE的lib目录下的核心类库&#xff0c;比如 rt.jar、charsets.jar等扩展类&#xff08;Extension &#xff09;加载器&#xff1a;负责加载支撑JVM运行的位于…

真题详解(哈希表)-软件设计(八十五)

真题详解&#xff08;树的结点&#xff09;-软件设计&#xff08;八十四)https://blog.csdn.net/ke1ying/article/details/130869095 要求邮件加密方式传输&#xff0c;邮件最大附件内容可达500MB&#xff0c;发送者不可抵赖&#xff0c;若邮件被第三方截获&#xff0c;第三方…

有哪些pdf转word的免费软件?这个办法值得一试

在日常工作和学习中&#xff0c;我们经常需要将PDF文件转换为Word文档。尤其是在需要编辑PDF文档中的内容时&#xff0c;将其转换为Word文档是非常必要的。但是&#xff0c;很多人不知道该如何快速完成这项任务。在本文中&#xff0c;我们将介绍一些简单的转换方式&#xff0c;…

智能排班系统 【管理系统功能、操作说明——上篇】

文章目录 功能设计共有功能系统管理员企业管理员门店管理员门店员工 页面与功能展示用户登录企业注册系统首页系统管理员首页企业管理员首页门店管理员首页 个人中心菜单管理日志管理登录日志 功能设计 不同的角色关注的任务和功能不同&#xff0c;针对不同的角色&#xff0c;…

树莓派485转USB串口调试教程

步骤1&#xff1a;接线方式&#xff1a;485转USB 注意接线口是否对应&#xff1a;A1B1 步骤2&#xff1a;查看串口配置—映射关系是否正确 命令&#xff1a;ls -l /dev serial0即GPIO映射的串口&#xff0c;默认是ttyS0&#xff0c;即mini串口 serial1即板载蓝牙映射的串口&am…

人工智能(Pytorch)搭建模型7-改造后的新型RegNet设计空间模型的搭建与训练

大家好&#xff0c;我是微学AI&#xff0c;今天给大家带来人工智能(Pytorch)搭建模型7-新型的卷积神经网络RegNet模型的搭建与训练&#xff0c;RegNet是一种新颖的卷积神经网络架构&#xff0c;它的设计理念是通过稀疏网络结构和精细的正则化来实现高效的计算和更好的泛化能力。…

小白看了也会的Redux编程

目录 介绍 演示 异步action react-redux 多组件管理的react-redux 扩展 介绍 redux是专门用于集中式管理状态的javascript库&#xff0c;并不是react的插件库。 比如你有多个组件A-E都想要用同一个组件D中的状态&#xff1a; 1&#xff09;像以前我们可以通过父子组件通…

前几天面了个30岁左右的测试员,年薪50w问题基本都能回答上,必是刷了不少八股文···

互联网行业竞争是一年比一年严峻&#xff0c;作为测试工程师的我们唯有不停地学习&#xff0c;不断的提升自己才能保证自己的核心竞争力从而拿到更好的薪水&#xff0c;进入心仪的企业&#xff08;阿里、字节、美团、腾讯等大厂.....&#xff09; 所以&#xff0c;大家就迎来了…

TCP三次握手四次挥手(幽默版)

三次握手&#xff1a; 假设你是一只鸟&#xff0c;你要与另一只鸟进行交流。&#xff08;你是客户端&#xff09; 1.首先你会问候&#xff1a;“你好&#xff0c;我是一只鸟&#xff0c;你可以听到我说话吗&#xff1f;”&#xff08;一次会话&#xff09; 2.另一只鸟回答&am…

一文带你了解MySQL之Explain执行计划

前言&#xff1a; 一条查询语句在经过MySQL查询优化器的各种基于成本和规则的优化会后生成一个所谓的执行计划&#xff0c;这个执行计划展示了接下来具体执行查询的方式&#xff0c;比如多表连接的顺序是什么&#xff0c;对于每个表采用什么访问方法来具体执行查询等等。MySQL…

MySQL---show profile分析SQL、trace分析优化器执行计划

1. show profile分析SQL Mysql从5.0.37版本开始增加了对 show profiles 和 show profile 语句的支持。show profiles 能够 在做SQL优化时帮助我们了解时间都耗费到哪里去了。 通过 have_profiling 参数&#xff0c;能够看到当前MySQL是否支持profile&#xff1a; select ha…

3年软件测试经验月薪7k,只会“点点点”,我该如何破局?

经常听到一些行业内的朋友说 “做测试&#xff0c;有手就行” 但事实真的是如此嘛&#xff1f; 随着测试行业的发展&#xff0c;越来越多的测试岗位对自动化测试&#xff0c;性能测试都有所要求&#xff0c;这对于很多只会功能测试的职场老人们来说&#xff0c;有了一丝丝的危…

Druid连接池技术实践

什么是Druid连接池&#xff1f; Druid连接池是阿里巴巴开源的数据库连接池项目。 Druid连接池为监控而生&#xff0c;内置强大的监控功能&#xff0c;监控特性不影响性能。功能强大&#xff0c;能防SQL注入&#xff0c;内置Loging能诊断Hack应用行为。 哦&#xff0c;首先Dru…

2023ACP世界大赛教育者论坛:让职业教育直面AI机遇与挑战

“AI技术的普及对创意行业和教育带来的影响和变革-2023 Adobe Certified Professional教育者论坛”在苏州西交利物浦大学成功举办。 本次论坛&#xff0c;由Adobe Certified Professional 世界大赛中国赛区组委会主办&#xff0c;联动了来自院校、海内外杰出的创意公司及国际知…

搭建飞书早报机器人

飞书是字节跳动推出的一款企业级通讯及协作平台&#xff0c;于2016年正式上线。它是一款基于云计算技术的软件工具&#xff0c;可以帮助企业实现快速高效的沟通和协作&#xff0c;提升工作效率&#xff0c;降低沟通成本。下面将详细介绍飞书的功能、特点以及使用体验。 功能介…