8.Seata分布式事务
8.1. Seata简介
- Seata是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。
8.2. Seata工作组件
- XID:全局事务的唯一标识,在微服务调用链中传递,绑定到服务的事务的上下文。
- TC:事务协调者,就是Seata,维护全局和分支事务的状态,驱动全局事务提交或回滚。(以Seata Server的形式独立部署)
- TM:事务管理器,标注全局@GlobalTransactional启动入口动作的微服务模块,是事务的发起者。定义全局事务的范围:开始全局事务、提交或回滚全局事务。
- RM:资源管理器,是MySQL数据库本身,管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
8.3. Seata工作流程
1.TM向TC申请开启全局事务,创建成功生成全局唯一的XID。
2.XID在微服务调用链中传播。
3.RM向TC注册分支事务,将其纳入XID对应的全局事务的管辖。
4.TM向TC发起针对XID的全局提交或回滚决议。
5.TC调度XID下管辖的全部分支事务完成提交或回滚请求。
8.4. Seata安装
8.4.1.安装流程
-
下载解压
- https://github.com/seata/seata/releases
-
创建数据库
- seata的server端需要创建一个数据库,用于存储事务信息
CREATE DATABASE seata;
USE seata;
-
导入表
-
https://github.com/apache/incubator-seata/blob/2.x/script/server/db/mysql.sql
-
执行sql脚本
-
-
修改配置文件
- 修改application.yml
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${
log.home:${
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:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: ""
group: SEATA_GROUP
username: nacos
password: nacos
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace: ""
cluster: default
username: nacos
password: nacos
store:
# support: file 、 db 、 redis 、 raft
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.dj.jdbc.Driver
url: jdbc:mysql://localhost:3307/seata?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true
user: root
password: 123456
minConn: 10
maxConn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 1000
maxWait: 5000
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security