seata使用总结
2022-01-28 13:26:46 0 举报
seata使用总结
作者其他创作
大纲/内容
seata库三张表建表语句
branch_table
CREATE TABLE `branch_table` (
`branch_id` bigint NOT NULL,
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`resource_group_id` varchar(32) DEFAULT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`branch_type` varchar(8) DEFAULT NULL,
`status` tinyint DEFAULT NULL,
`client_id` varchar(64) DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
`branch_id` bigint NOT NULL,
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`resource_group_id` varchar(32) DEFAULT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`branch_type` varchar(8) DEFAULT NULL,
`status` tinyint DEFAULT NULL,
`client_id` varchar(64) DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
global_table
CREATE TABLE `global_table` (
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`status` tinyint NOT NULL,
`application_id` varchar(32) DEFAULT NULL,
`transaction_service_group` varchar(32) DEFAULT NULL,
`transaction_name` varchar(128) DEFAULT NULL,
`timeout` int DEFAULT NULL,
`begin_time` bigint DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`status` tinyint NOT NULL,
`application_id` varchar(32) DEFAULT NULL,
`transaction_service_group` varchar(32) DEFAULT NULL,
`transaction_name` varchar(128) DEFAULT NULL,
`timeout` int DEFAULT NULL,
`begin_time` bigint DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
lock_table
CREATE TABLE `lock_table` (
`row_key` varchar(128) NOT NULL,
`xid` varchar(96) DEFAULT NULL,
`transaction_id` bigint DEFAULT NULL,
`branch_id` bigint NOT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`table_name` varchar(32) DEFAULT NULL,
`pk` varchar(36) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
undo_log建表语句
CREATE TABLE `undo_log` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` bigint NOT NULL COMMENT 'branch transaction id',
`xid` varchar(100) 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 NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` datetime NOT NULL COMMENT 'create datetime',
`log_modified` datetime NOT NULL COMMENT 'modify datetime',
`ext` varchar(100) DEFAULT NULL COMMENT 'reserved field',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb3 COMMENT='AT transaction mode undo table';
`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` bigint NOT NULL COMMENT 'branch transaction id',
`xid` varchar(100) 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 NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` datetime NOT NULL COMMENT 'create datetime',
`log_modified` datetime NOT NULL COMMENT 'modify datetime',
`ext` varchar(100) DEFAULT NULL COMMENT 'reserved field',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb3 COMMENT='AT transaction mode undo table';
seata maven 依赖问题
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</exclusion>
</exclusions>
</dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</exclusion>
</exclusions>
</dependency>
seata官网 : seata.io
seata基础配置问题
数据库配置
首先配置seata数据库,创建表branch_table,global_table,lock_table,库名默认为seata
所有的业务表中也就是要用到分布式事务表中加入undo_log 表
file.config
修改 模式为db 并且修改db的相关属性,属性对应上方新建的seata库的信息
registry.conf 文件配置
选择注册中心为nacos
选择配置中心为nacos
具体信息可参考文档
https://blog.csdn.net/jixieguang/article/details/110621561
https://blog.csdn.net/qq_41133155/article/details/109737266
springcloud整合Seata,nacaos 注意点
同步nacos配置数据
导入nacos的相关配置:
service.vgroup_mapping.seata_tx_group=default
注意seata1.4 已经使用驼峰命名所以要改成
service.vgroupMapping.seata_tx_group=default
同时注意seata_tx_group 要与springboot中的相关信息一致 也可以在springboot配置文件中配置
store.mode=db
store.db.dbType=mysql
store.db.datasource=druid
store.db.driver-class-name=com.mysql.cj.jdbc.Driver mysql 8.0以上
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
service.default.grouplist=127.0.0.1:8091 此处就是你的seata服务 也可以在springboot配置文件中配置
service.vgroup_mapping.seata_tx_group=default
注意seata1.4 已经使用驼峰命名所以要改成
service.vgroupMapping.seata_tx_group=default
同时注意seata_tx_group 要与springboot中的相关信息一致 也可以在springboot配置文件中配置
store.mode=db
store.db.dbType=mysql
store.db.datasource=druid
store.db.driver-class-name=com.mysql.cj.jdbc.Driver mysql 8.0以上
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
service.default.grouplist=127.0.0.1:8091 此处就是你的seata服务 也可以在springboot配置文件中配置
springboot 也就是application.yml相关配置
seata:
enabled: true
enableAutoDataSourceProxy: true
tx-service-group: seata_tx_group
registry:
type: nacos
nacos:
application: seata-server
group : SEATA_GROUP
server-addr: localhost:8848
username: nacos
password: nacos
namespace: 66cac8e5-f076-4ed1-a3d9-93a68d17c342
config:
type: nacos
nacos:
application: seata-server
server-addr: localhost:8848
group: SEATA_GROUP
username: nacos
password: nacos
namespace: 66cac8e5-f076-4ed1-a3d9-93a68d17c342
# 如果nacos为配置中心,则service配置相关信息 必须放在nacos中
#service:
# grouplist:
# default: localhost:8081
#注意驼峰
# vgroupMapping:
# seata_tx_group: default
# disable-global-transaction: false
client:
rm:
report-success-enable: false
#--------------------------------------------------------------
#注意: tx-service-group: seata_tx_group 必须与service.vgroupMapping.seata_tx_group=default 一致
enabled: true
enableAutoDataSourceProxy: true
tx-service-group: seata_tx_group
registry:
type: nacos
nacos:
application: seata-server
group : SEATA_GROUP
server-addr: localhost:8848
username: nacos
password: nacos
namespace: 66cac8e5-f076-4ed1-a3d9-93a68d17c342
config:
type: nacos
nacos:
application: seata-server
server-addr: localhost:8848
group: SEATA_GROUP
username: nacos
password: nacos
namespace: 66cac8e5-f076-4ed1-a3d9-93a68d17c342
# 如果nacos为配置中心,则service配置相关信息 必须放在nacos中
#service:
# grouplist:
# default: localhost:8081
#注意驼峰
# vgroupMapping:
# seata_tx_group: default
# disable-global-transaction: false
client:
rm:
report-success-enable: false
#--------------------------------------------------------------
#注意: tx-service-group: seata_tx_group 必须与service.vgroupMapping.seata_tx_group=default 一致
全局事务id即XID信息传递问题
在springcloud中用feign调用服务必须在头部传递XID信息,否则没有XID的传递 分布式事务也不会生效
/**
* Feign 配置类
*
* @author yaoj
* @since 2022/1/5
*/
@Configuration
public class FeignConfig {
@Bean
public RequestInterceptor headerRequestInterceptor() {
return template -> {
template.header(RootContext.KEY_XID, RootContext.getXID());
};
}
}
* Feign 配置类
*
* @author yaoj
* @since 2022/1/5
*/
@Configuration
public class FeignConfig {
@Bean
public RequestInterceptor headerRequestInterceptor() {
return template -> {
template.header(RootContext.KEY_XID, RootContext.getXID());
};
}
}
0 条评论
下一页