详解SpringCloud-Alibaba-Seata分布式事务
前言
seata 是一款阿里巴巴开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。
seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。seata 将为用户提供了 at、tcc、saga 和 xa 事务模式,为用户打造一站式的分布式解决方案。
术语
tc (transaction coordinator) - 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。
tm (transaction manager) - 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。
rm (resource manager) - 资源管理器
管理分支事务处理的资源,与tc交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
1.
xid:全局唯一事务id
tc:事务协调器,维护全局事务的运行状态,负责协调并驱动全局的提交或回滚
tm:控制全局事务的边界,负责开启一全局事务,并最终发起全局提交或者回滚的决议
rm:控制分支事务,负责分支注册,状态汇报,并接受事务协调的指令,驱动分支的事务提交或者回滚
2.
tm向tc申请开启一个全局的事务,全局事务创建成功并生成一个唯一的全局id,xid在微服务调用链路的上下文中传播
rm向tc注册分支事务,将其纳入xid对应事务的 管辖
tm向tc发起针对xid的全局提交或者回滚
tc调度xid下管辖的全部分支事务完成提交或者回滚
官方文档https://seata.io/zh-cn/docs/overview/what-is-seata.html
示例 win版
下载安装
https://github.com/seata/seata/releases/tag/v1.3.0
下载好的包解压 解压
配置 seata
打开\seata\seata\conf 下编辑 file.conf文件
修改数据库模式
file.conf 文件修改链接数据的 信息
修改本地mysql的地址与账号密码
file.conf 中的service也没有手动添加
vgroup_mapping.my_test_tx_group = “default”
default任意定义名字
service { #transaction service group mapping #修改,可不改,my_test_tx_grou。 vgroup_mapping.my_test_tx_group = "default" #only support when registry.type=file, please don't set multiple addresses # 此服务的地址 default.grouplist = "127.0.0.1:8091" #disable seata disableglobaltransaction = false }
创建数据库 运行官方给的额sql文件
我草!没有sql脚本, 0.9版本有sql脚本
网上找了一个
drop table if exists `branch_table`; create table `branch_table` ( `branch_id` bigint(20) not null, `xid` varchar(128) character set utf8 collate utf8_general_ci not null, `transaction_id` bigint(20) null default null, `resource_group_id` varchar(32) character set utf8 collate utf8_general_ci null default null, `resource_id` varchar(256) character set utf8 collate utf8_general_ci null default null, `branch_type` varchar(8) character set utf8 collate utf8_general_ci null default null, `status` tinyint(4) null default null, `client_id` varchar(64) character set utf8 collate utf8_general_ci null default null, `application_data` varchar(2000) character set utf8 collate utf8_general_ci null default null, `gmt_create` datetime(6) null default null, `gmt_modified` datetime(6) null default null, primary key (`branch_id`) using btree, index `idx_xid`(`xid`) using btree ) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic; -- ---------------------------- -- records of branch_table -- ---------------------------- -- ---------------------------- -- table structure for global_table -- ---------------------------- drop table if exists `global_table`; create table `global_table` ( `xid` varchar(128) character set utf8 collate utf8_general_ci not null, `transaction_id` bigint(20) null default null, `status` tinyint(4) not null, `application_id` varchar(32) character set utf8 collate utf8_general_ci null default null, `transaction_service_group` varchar(32) character set utf8 collate utf8_general_ci null default null, `transaction_name` varchar(128) character set utf8 collate utf8_general_ci null default null, `timeout` int(11) null default null, `begin_time` bigint(20) null default null, `application_data` varchar(2000) character set utf8 collate utf8_general_ci null default null, `gmt_create` datetime(0) null default null, `gmt_modified` datetime(0) null default null, primary key (`xid`) using btree, index `idx_gmt_modified_status`(`gmt_modified`, `status`) using btree, index `idx_transaction_id`(`transaction_id`) using btree ) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic; -- ---------------------------- -- records of global_table -- ---------------------------- -- ---------------------------- -- table structure for lock_table -- ---------------------------- drop table if exists `lock_table`; create table `lock_table` ( `row_key` varchar(128) character set utf8 collate utf8_general_ci not null, `xid` varchar(96) character set utf8 collate utf8_general_ci null default null, `transaction_id` bigint(20) null default null, `branch_id` bigint(20) not null, `resource_id` varchar(256) character set utf8 collate utf8_general_ci null default null, `table_name` varchar(32) character set utf8 collate utf8_general_ci null default null, `pk` varchar(36) character set utf8 collate utf8_general_ci null default null, `gmt_create` datetime(0) null default null, `gmt_modified` datetime(0) null default null, primary key (`row_key`) using btree, index `idx_branch_id`(`branch_id`) using btree ) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic; -- ---------------------------- -- records of lock_table -- ---------------------------- -- ---------------------------- -- table structure for undo_log -- ---------------------------- drop table if exists `undo_log`; create table `undo_log` ( `id` bigint(20) not null auto_increment, `branch_id` bigint(20) not null, `xid` varchar(100) character set utf8 collate utf8_general_ci not null, `context` varchar(128) character set utf8 collate utf8_general_ci not null, `rollback_info` longblob not null, `log_status` int(11) not null, `log_created` datetime(0) not null, `log_modified` datetime(0) not null, `ext` varchar(100) character set utf8 collate utf8_general_ci null default null, primary key (`id`) using btree, unique index `ux_undo_log`(`xid`, `branch_id`) using btree ) engine = innodb auto_increment = 1 character set = utf8 collate = utf8_general_ci row_format = dynamic;
修改conf文件夹下 registry.conf ,
seata 支持注册nacos 、eureka、redis、zk、consul、etcd3、sofa
本地的nacos的地址
改完配置文件保存 测试
启动ncos 在启动seata
seata 下bin目录
代码示例
pom
<dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-seata</artifactid> <exclusions> <exclusion> <artifactid>seata-all</artifactid> <groupid>io.seata</groupid> </exclusion> </exclusions> </dependency> <dependency> <groupid>io.seata</groupid> <artifactid>seata-all</artifactid> <version>1.3.0</version> </dependency>
使用库下创建`undo_log表
drop table `undo_log`; create table `undo_log` ( `id` bigint(20) not null auto_increment, `branch_id` bigint(20) not null, `xid` varchar(100) not null, `context` varchar(128) not null, `rollback_info` longblob not null, `log_status` int(11) not null, `log_created` datetime not null, `log_modified` datetime not null, `ext` varchar(100) default null, primary key (`id`), unique key `ux_undo_log` (`xid`,`branch_id`) ) engine=innodb auto_increment=1 default charset=utf8;
yml
server: port: 2001 spring: application: name: seata-order-service cloud: alibaba: seata: #自定义事务组名称需要与seata-server中的对应 tx-service-group: default nacos: discovery: server-addr: localhost:8848 datasource: driver-class-name: com.mysql.jdbc.driver url: jdbc:mysql://localhost:3306/seata_order username: root password: root feign: hystrix: enabled: false logging: level: io: seata: info mybatis: mapperlocations: classpath:mapper/*.xml
file.conf
transport { # tcp udt unix-domain-socket type = "tcp" #nio native server = "nio" #enable heartbeat heartbeat = true #thread factory for netty thread-factory { boss-thread-prefix = "nettyboss" worker-thread-prefix = "nettyservernioworker" server-executor-thread-prefix = "nettyserverbizhandler" share-boss-worker = false client-selector-thread-prefix = "nettyclientselector" client-selector-thread-size = 1 client-worker-thread-prefix = "nettyclientworkerthread" # netty boss thread size,will not be used for udt boss-thread-size = 1 #auto default pin or 8 worker-thread-size = 8 } shutdown { # when destroy server, wait seconds wait = 3 } serialization = "seata" compressor = "none" } service { vgroup_mapping.fsp_tx_group = "default" default.grouplist = "127.0.0.1:8091" enabledegrade = false disable = false max.commit.retry.timeout = "-1" max.rollback.retry.timeout = "-1" disableglobaltransaction = false } client { async.commit.buffer.limit = 10000 lock { retry.internal = 10 retry.times = 30 } report.retry.count = 5 tm.commit.retry.count = 1 tm.rollback.retry.count = 1 } ## transaction log store store { ## store mode: file、db mode = "db" ## file store file { dir = "sessionstore" # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions max-branch-session-size = 16384 # globe session size , if exceeded throws exceptions max-global-session-size = 512 # file buffer size , if exceeded allocate new buffer file-write-buffer-cache-size = 16384 # when recover batch read size session.reload.read_size = 100 # async, sync flush-disk-mode = async } ## database store db { ## the implement of javax.sql.datasource, such as druiddatasource(druid)/basicdatasource(dbcp) etc. datasource = "dbcp" ## mysql/oracle/h2/oceanbase etc. db-type = "mysql" driver-class-name = "com.mysql.jdbc.driver" url = "jdbc:mysql://127.0.0.1:3306/seata" user = "root" password = "root" min-conn = 1 max-conn = 3 global.table = "global_table" branch.table = "branch_table" lock-table = "lock_table" query-limit = 100 } } lock { ## the lock store mode: local、remote mode = "remote" local { ## store locks in user's database } remote { ## store locks in the seata's server } } recovery { #schedule committing retry period in milliseconds committing-retry-period = 1000 #schedule asyn committing retry period in milliseconds asyn-committing-retry-period = 1000 #schedule rollbacking retry period in milliseconds rollbacking-retry-period = 1000 #schedule timeout retry period in milliseconds timeout-retry-period = 1000 } transaction { undo.data.validation = true undo.log.serialization = "jackson" undo.log.save.days = 7 #schedule delete expired undo_log in milliseconds undo.log.delete.period = 86400000 undo.log.table = "undo_log" } ## metrics settings metrics { enabled = false registry-type = "compact" # multi exporters use comma divided exporter-list = "prometheus" exporter-prometheus-port = 9898 } support { ## spring spring { # auto proxy the datasource bean datasource.autoproxy = false } }
registry.conf
registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { serveraddr = "localhost:8848" namespace = "" cluster = "default" } eureka { serviceurl = "http://localhost:8761/eureka" application = "default" weight = "1" } redis { serveraddr = "localhost:6379" db = "0" } zk { cluster = "default" serveraddr = "127.0.0.1:2181" session.timeout = 6000 connect.timeout = 2000 } consul { cluster = "default" serveraddr = "127.0.0.1:8500" } etcd3 { cluster = "default" serveraddr = "http://localhost:2379" } sofa { serveraddr = "127.0.0.1:9603" application = "default" region = "default_zone" datacenter = "defaultdatacenter" cluster = "default" group = "seata_group" addresswaittime = "3000" } file { name = "file.conf" } } config { # file、nacos 、apollo、zk、consul、etcd3 type = "file" nacos { serveraddr = "localhost" namespace = "" } consul { serveraddr = "127.0.0.1:8500" } apollo { app.id = "seata-server" apollo.meta = "http://192.168.1.204:8801" } zk { serveraddr = "127.0.0.1:2181" session.timeout = 6000 connect.timeout = 2000 } etcd3 { serveraddr = "http://localhost:2379" } file { name = "file.conf" } }
@globaltransactional(name = "tang,rollbackfor = exception.class)开启全局事物
@override @globaltransactional(name = "tang,rollbackfor = exception.class) public void create(providers providers ){ test1mapper.add(providers); test2mapper.add(providers) }
到此这篇关于springcloud-alibaba-seata分布式事务的文章就介绍到这了,更多相关springcloud-alibaba-seata分布式事务内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!