部署seata
1.下载镜像,启动容器
docker run -d --name seata-server -p 8091:8091 seataio/seata-server:1.6.1
2.复制容器中的配置文件夹
docker cp seata-server:/seata-server/resources D:/seata-server
3.新建数据库
mysql脚本
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);
4.修改application.yml
将application.example.yml内容根据需要粘贴进application.yml并调整
nacos,mysql,seata的ip同为192.168.2.178
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
config:
type: nacos
nacos:
server-addr: 192.168.2.178:8848
namespace:
group: SEATA_GROUP
username:
password:
context-path:
data-id: seataServer.properties
registry:
type: nacos
nacos:
application: seata-server
server-addr: 192.168.2.178:8848
group: SEATA_GROUP
namespace:
cluster: default
username:
password:
context-path:
store:
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.2.178:3306/seata?rewriteBatchedStatements=true
user: mysql
password: mysql
min-conn: 10
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 1000
max-wait: 5000
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
5.删除原容器,使用修改后的配置重启容器
docker run -d --name seata-server -p 8091:8091 -p 7091:7091 -v D:\seata-server\resources\resources:/seata-server/resources -e SEATA_IP=192.168.2.178 seataio/seata-server:1.6.1
seata启动成功,查看页面
http://192.168.2.178:7091/
springcloud集成
1.引入maven依赖
<dependency> <artifactId>seata-spring-boot-starter</artifactId> <groupId>io.seata</groupId> <version>1.6.1</version></dependency><dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2022.0.0.0</version> <exclusions> <!--自带1.7.0 改为1.6.1--> <exclusion> <artifactId>seata-spring-boot-starter</artifactId> <groupId>io.seata</groupId> </exclusion> </exclusions></dependency>
2.添加配置
seata:
enabled:true
application-id : ${spring.application.name}
tx-service-group: default_tx_group
use-jdk-proxy:true
enable-auto-data-source-proxy:true
registry:
type:nacos
nacos:
application: seata-server
server-addr: 192.168.2.178:8848
group: SEATA_GROUP
config:
type:nacos
nacos:
server-addr: 192.168.2.178:8848
group: SEATA_GROUP
service:
vgroupMapping:
default_tx_group: default
3.应用服务数据库添加undo_log表
CREATE TABLE `undo_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`branch_id` bigint NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int 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 DEFAULT CHARSET=utf8mb3;
4.方法加注解@GlobalTransactional
@GlobalTransactional
@Transactional
public void test(){
。。。
}
5.nacos创建配置service.vgroupMapping.default_tx_group = default
Data ID:service.vgroupMapping.default_tx_group
Group:SEATA_GROUP
配置格式:TEXT
配置内容:default
6.启动服务,完事
遇到的问题
1.can not get cluster name in registry config 'service.vgroupMapping.default_tx_group'
服务启动报错can not get cluster name in registry config 'service.vgroupMapping.default_tx_group', please make sure registry config correct
未配置事务组,按照上述步骤添加配置service.vgroupMapping.default_tx_group即可
2.0101 can not connect to 10.200.4.68:8091 cause:can not register RM,err:can not connect to services-server.
nacos注册中心中的ip为docker容器ip,需改为宿主机ip,启动容器添加参数-e SEATA_IP=XXX
3.分布式事务失效
没和springcloud组件集成
除seata-spring-boot-starter外,需完整引入spring-cloud-starter-alibaba-seata
