赞
踩
配置nacos打开下载好的文件夹找到配置文件
- config:
- # support: nacos, consul, apollo, zk, etcd3
- # type: file
- type: nacos
- nacos:
- server-addr: 127.0.0.1:8848
- namespace: train
- group: SEATA_GROUP
- username: nacos
- password: nacos
- context-path:
- ##if use MSE Nacos with auth, mutex with username/password attribute
- #access-key:
- #secret-key:
- data-id: seataServer.properties
配置中心的文件名就是data-id
接下来修改注册中心
启动项目模块打开seata服务出现注册中心,接下来打开nacos查看命名空间,nacos的配置在前面的博客当中有可以查看怎么集成。
访问nacos,显示的信息都是前面配置成功的信息
注意这里的id需要和配置文件中的一致, group也需要一致
下面的配置内容为
- # 和微服务模块的seata.tx-service-group保持一致
- service.vgroupMapping.train-group=default
- service.default.grouplist=127.0.0.1:8091
-
- # 和微服务模块的seata.tx-service-group保持一致
- service.vgroupMapping.test-group=default1
- service.default1.grouplist=127.0.0.1:18091
AT模式会有一个全局锁,用于防止脏读,线程1的事务修改了库存,但还没提交事务,线程2读库存的时候,读的还是原来的库存
然后下面就是连接数据库nacos所要执行的,官方提供的MySQL数据表存全局id,分布式锁的表
- -- -------------------------------- The script used when storeMode is 'db' --------------------------------
- -- 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 = utf8mb4;
-
- -- 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 = utf8mb4;
-
- -- 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`),
- KEY `idx_xid` (`xid`)
- ) ENGINE = InnoDB
- DEFAULT CHARSET = utf8mb4;
-
- 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 ('AsyncCommitting', ' ', 0);
- INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
- INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
- INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
现在所有的配置都放在nacos了。
在对应的模块下的配置文件 bootstrap.properties,下面的配置不用多说了
-
- # seata注册中心,要和seata server的application.yml配置保持一致
- seata.registry.type=nacos
- seata.registry.nacos.application=seata-server
- seata.registry.nacos.server-addr=127.0.0.1:8848
- seata.registry.nacos.group=SEATA_GROUP
- seata.registry.nacos.namespace=train
- seata.registry.nacos.username=nacos
- seata.registry.nacos.password=nacos
-
- # seata配置中心,要和seata server的application.yml配置保持一致
- seata.config.type=nacos
- seata.config.nacos.server-addr=127.0.0.1:8848
- seata.config.nacos.group=SEATA_GROUP
- seata.config.nacos.namespace=train
- seata.config.nacos.dataId=seataServer.properties
- seata.config.nacos.username=nacos
- seata.config.nacos.password=nacos
使得当我们的接口出现错误,seata事务会进行回滚,全局事务会放进前面官方提供的表中, 其他的配置都可以简化
事务回滚逆向生成SQL数据
当我们把接口的线程延迟10秒后点击执行这个接口,我们会发现两个表里面有数据,在全局id表里面只有一个id,当我们10秒之后再次运行SQL刷新SQL之后就会被清除
SQL限制官方
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。