1. 下载seata-server
1.从 https://github.com/seata/seata/releases,下载服务器软件包,将其解压缩。
2. 建表
全局事务过程中,会涉及3块内容:
- 全局事务 global_table
- 分支事务 branch_table
- 全局锁 lock_table
-- -------------------------------- 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_gmt_modified_status` (`gmt_modified`, `status`),
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(96),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
### 3. 修改seata-server配置
#### 3.1 配置registry.conf
//注册中心配置
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
eureka {
serviceUrl = "http://192.xx.xx.xx:8761/eureka"
application = "fsp_tx" //tc注册时服务名
weight = "1"
}
}
//配置中心配置
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
name = "file.conf"
}
}
3.2 配置file.conf
## transaction log store, only used in seata-server
store {
## store mode: file、db
mode = "db"
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://192.168.173.95:3306/seata-server" //tc的数据库,可自定义命名,对应就好
user = "mysql"
password = "runlion@123"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
4. 启动seata-server 注册到eureka
- linux
nohup sh seata-server.sh -h xx.xx.xx.xx -p 8091 -m db -n 1 &
- windows
seata-server.bat
- 这里是以nohup的方式后台启动,参数可选:
-h: 注册到注册中心的ip
-p: Server rpc 监听端口
-m: 全局事务会话信息存储模式,file、db,优先读取启动参数
-n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
-e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
高可用部署可参考:https://seata.io/zh-cn/docs/ops/deploy-ha.html
5. 服务引入seata依赖
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
//如果内嵌最新版本starter,我们不用排除再引入
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
6. 服务配置文件
# -----------seata--------------
seata:
enabled: true
application-id: order-server #服务名
tx-service-group: default # default是自定义的事务分组名称
enable-auto-data-source-proxy: true # 启用自动数据源代理
use-jdk-proxy: false
# excludes-for-auto-proxying:
# client:
# rm:
# async-commit-buffer-limit: 1000
# report-retry-count: 5
# table-meta-check-enable: false
# report-success-enable: false
# saga-branch-register-enable: false
# lock:
# retry-interval: 10
# retry-times: 30
# retry-policy-branch-rollback-on-conflict: true
# tm:
# commit-retry-count: 5
# rollback-retry-count: 5
# undo:
# data-validation: true
# log-serialization: jackson
# log-table: undo_log
# log:
# exceptionRate: 100
service:
vgroup-mapping:
default: seata-server # default是自定义的事务分组名称,fsp_tx是tc注册到注册中心的服务名称
# grouplist:
# default: 127.0.0.1:8091 # 仅注册中心为file时使用
enable-degrade: false # 是否启用降级
disable-global-transaction: false # 是否禁用全局事务
# transport:
# shutdown:
# wait: 3
# 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
# worker-thread-size: default
# boss-thread-size: 1
# type: TCP
# server: NIO
# heartbeat: true
# serialization: seata
# compressor: none
# enable-client-batch-send-request: true
config:
type: file # 配置中心为file模式
# consul:
# server-addr: 127.0.0.1:8500
# apollo:
# apollo-meta: http://192.168.1.204:8801
# app-id: seata-server
# namespace: application
# etcd3:
# server-addr: http://localhost:2379
# nacos:
# namespace:
# serverAddr: localhost
# group: SEATA_GROUP
# userName: ""
# password: ""
# zk:
# server-addr: 127.0.0.1:2181
# session-timeout: 6000
# connect-timeout: 2000
# username: ""
# password: ""
registry:
type: eureka # 注册中心为eureka
eureka:
weight: 1
service-url: http://127.0.0.1:50024/eureka # 注册中心地址
# consul:
# server-addr: 127.0.0.1:8500
# etcd3:
# serverAddr: http://localhost:2379
# nacos:
# application: seata-server
# server-addr: localhost
# namespace:
# userName: ""
# password: ""
# redis:
# server-addr: localhost:6379
# db: 0
# password:
# timeout: 0
# sofa:
# server-addr: 127.0.0.1:9603
# region: DEFAULT_ZONE
# datacenter: DefaultDataCenter
# group: SEATA_GROUP
# addressWaitTime: 3000
# application: default
# zk:
# server-addr: 127.0.0.1:2181
# session-timeout: 6000
# connect-timeout: 2000
# username: ""
# password: ""
# -----------seata--------------
7. 使用
@GlobalTransaction 全局事务注解