关键词:
Seata简介
在传统的单体项目中,我们使用@Transactional注解就能实现基本的ACID事务了。
但是前提是:
1) 数据库支持事务(如:MySQL的innoDB引擎)
2) 所有业务都在同一个数据库中执行
随着微服务架构的引入,需要对数据库进行分库分表,每个服务拥有自己的数据库,这样传统的事务就不起作用了,那么我们如何保证多个服务中数据的一致性呢?
这样就出现了分布式事务,而Seata就是为微服务架构而生的一种高性能、易于使用的分布式事务解决方案。
Seata 中有三个基础组件:
-
Transaction Coordinator(TC协调者):维护全局和分支事务的状态,驱动全局提交或回滚。
-
Transaction Manager(TM事务管理):定义全局事务的范围,开启、提交或回滚一个全局事务。
-
Resource Manager(RM资源管理):管理分支事务资源,与 TC 通讯并报告分支事务状态,管理本地事务的提交与回滚。
可以这么说一个分布式事务就是全局事务GlobalTransaction,而全局事务是由一个个的分支事务组成的,每个分支事务就是一个本地事务。
Seata的生命周期 -
TM 要求 TC 生成一个全局事务,并由 TC 生成一个全局事务XID 返回。
-
XID 通过微服务调用链传播。
-
RM 向 TC 注册本地事务,将其注册到 ID 为 XID 的全局事务中。
-
TM 要求 TC 提交或回滚XID 对应的全局事务。
-
TC 驱动 XID 对应的全局事务对应的所有的分支事务提交或回滚。
Seata安装和配置
安装nacos,本案例使用了nacos作为注册中心
https://github.com/alibaba/nacos/releases
下载nacos,本文使用的是windows版本1.4.0
使用命令行进入bin目录,以单机模式启动nacos
startup -m standalone
安装和配置Seata
http://seata.io/zh-cn/blog/download.html
下载Seata,这里是Windows版本的1.4.0
解压后,进入conf目录,配置file.conf和registry.conf两个文件
file.conf主要是数据库的配置,配置如下
registry.conf 是注册中心的配置
另外conf目录中还需要一个脚本文件:nacos-config.sh 用于对nacos进行初始化配置
在seata1.4.0中是没有的,需要自行创建,内容如下:
#!/usr/bin/env bash
# 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.
while getopts ":h:p:g:t:u:w:" opt
do
case $opt in
h)
host=$OPTARG
;;
p)
port=$OPTARG
;;
g)
group=$OPTARG
;;
t)
tenant=$OPTARG
;;
u)
username=$OPTARG
;;
w)
password=$OPTARG
;;
?)
echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
exit 1
;;
esac
done
urlencode()
for ((i=0; i < $#1; i++))
do
char="$1:$i:1"
case $char in
[a-zA-Z0-9.~_-]) printf $char ;;
*) printf '%%%02X' "'$char" ;;
esac
done
if [[ -z $host ]]; then
host=localhost
fi
if [[ -z $port ]]; then
port=8848
fi
if [[ -z $group ]]; then
group="SEATA_GROUP"
fi
if [[ -z $tenant ]]; then
tenant=""
fi
if [[ -z $username ]]; then
username=""
fi
if [[ -z $password ]]; then
password=""
fi
nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"
echo "set nacosAddr=$nacosAddr"
echo "set group=$group"
failCount=0
tempLog=$(mktemp -u)
function addConfig()
curl -X POST -H "$contentType" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$(urlencode $1)&group=$group&content=$(urlencode $2)&tenant=$tenant&username=$username&password=$password" >"$tempLog" 2>/dev/null
if [[ -z $(cat "$tempLog") ]]; then
echo " Please check the cluster status. "
exit 1
fi
if [[ $(cat "$tempLog") =~ "true" ]]; then
echo "Set $1=$2 successfully "
else
echo "Set $1=$2 failure "
(( failCount++ ))
fi
count=0
for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
(( count++ ))
key=$line%%=*
value=$line#*=
addConfig "$key" "$value"
done
echo "========================================================================="
echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
echo "========================================================================="
if [[ $failCount -eq 0 ]]; then
echo " Init nacos config finished, please start seata-server. "
else
echo " init nacos config fail. "
fi
在seata的根目录,与conf同级的目录下,还需要config.txt 配置文件,默认也是没有的
只需要对mysql的配置进行修改
完整文件:
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=true
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
store.mode=file
store.lock.mode=file
store.session.mode=file
store.publicKey=xx
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.sentinel.masterName=xx
store.redis.sentinel.sentinelHosts=xx
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=xx
store.redis.queryLimit=100
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
在conf目录中,使用Git Bash进入命令行,输入
sh nacos-config.sh 127.0.0.1
这是对Seata进行初始化配置,上图表示所有配置都成功设置了
在nacos中可以看到出现了seata相关的配置
接下来在seata数据库中,新建三个表
drop table if exists `global_table`;
create table `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`)
);
drop table if exists `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(32),
`resource_id` varchar(256) ,
`lock_key` varchar(128) ,
`branch_type` varchar(8) ,
`status` tinyint,
`client_id` varchar(64),
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`branch_id`),
key `idx_xid` (`xid`)
);
drop table if exists `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(96),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(32) ,
`pk` varchar(36) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);
在项目相关的数据库中,新建表undo_log 用于记录撤销日志
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) 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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
最后在bin目录中,启动命令行,执行seata-server.bat 启动Seata服务
项目应用Seata
SpringCloud项目中有两个服务:订单服务和库存服务,基本业务是:
- 购买商品
- 插入订单
- 减少库存
订单详情表
DROP TABLE IF EXISTS `tb_order_detail`;
CREATE TABLE `tb_order_detail` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单详情id ',
`order_id` bigint(20) NOT NULL COMMENT '订单id',
`sku_id` bigint(20) NOT NULL COMMENT 'sku商品id',
`num` int(11) NOT NULL COMMENT '购买数量',
`title` varchar(256) NOT NULL COMMENT '商品标题',
`own_spec` varchar(1024) DEFAULT '' COMMENT '商品动态属性键值集',
`price` bigint(20) NOT NULL COMMENT '价格,单位:分',
`image` varchar(128) DEFAULT '' COMMENT '商品图片',
PRIMARY KEY (`id`),
KEY `key_order_id` (`order_id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=131 DEFAULT CHARSET=utf8 COMMENT='订单详情表';
库存表
DROP TABLE IF EXISTS `tb_stock`;
CREATE TABLE `tb_stock` (
`sku_id` bigint(20) NOT NULL COMMENT '库存对应的商品sku id',
`seckill_stock` int(9) DEFAULT '0' COMMENT '可秒杀库存',
`seckill_total` int(9) DEFAULT '0' COMMENT '秒杀总数量',
`stock` int(9) NOT NULL COMMENT '库存数量',
PRIMARY KEY (`sku_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='库存表,代表库存,秒杀库存等信息';
父项目定义了springboot、springcloud、springcloud-alibaba的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
子项目的依赖定义了nacos和seata客户端
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<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>
子项目配置文件
完整配置
server:
port: 8001
spring:
application:
name: stock-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
alibaba:
seata:
enabled: true
enable-auto-data-source-proxy: true
tx-service-group: my_test_tx_group
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
service:
vgroup-mapping:
my_test_tx_group: default
disable-global-transaction: false
client:
rm:
report-success-enable: false
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/eshop?serverTimezone=UTC&useUnicode=true&useSSL查看详情
springcloudalibabaseata分布式事务使用快速入门,nacos做seata的注册中心和配置中心
SpringCloudSeata分布式事务使用快速入门特点1、文档教程详细且提供完整源码及数据库脚本2、有配套全手敲代码的视频演示教程使用版本SpringCloudAlibaba2021.1Nacos1.4.1Seata1.3.0概念说明:本地事务及分布式事务本地事务1、在一个进... 查看详情
springcloudalibabaseata分布式事务使用快速入门,nacos做seata的注册中心和配置中心(代码片段)
SpringCloudSeata分布式事务使用快速入门特点1、文档教程详细且提供完整源码及数据库脚本2、有配套全手敲代码的视频演示教程使用版本SpringCloudAlibaba2021.1Nacos1.4.1Seata1.3.0概念说明:本地事务及分布式事务本地事务1、在一个进... 查看详情
springcloudalibabaseata分布式事务解决方案(代码片段)
SpringCloudAlibabaSeata分布式事务解决方案一、分布式事务问题二、Seata简介三、Seata的部署3.1SeataServer端配置3.1.1修改配置文件3.1.2MySQL数据库配置3.1.3启动SeataServer端3.2SeataClient客户端配置3.2.1业务前置准备3.2.2创建undo_log表3.2.3SeataClient... 查看详情
springcloudalibabaseata分布式事务(代码片段)
超详细的Java知识点路线图Seata简介在传统的单体项目中,我们使用@Transactional注解就能实现基本的ACID事务了。但是前提是:1)数据库支持事务(如:MySQL的innoDB引擎)2)所有业务都在同一个数据库... 查看详情
springcloudalibabaseata分布式事务使用快速入门,nacos做seata的注册中心和配置中心(代码片段)
SpringCloudSeata分布式事务使用快速入门特点1、文档教程详细且提供完整源码及数据库脚本2、有配套全手敲代码的视频演示教程使用版本SpringCloudAlibaba2021.1Nacos1.4.1Seata1.3.0概念说明:本地事务及分布式事务本地事务1、在一个进... 查看详情
springcloudalibabaseata集成(代码片段)
主项目链接:https://gitee.com/java_wxid/java_wxid项目架构及博文总结:点击:【使用SpringBoot快速构建应用】点击:【使用SpringCloudOpenFeign基于动态代理动态构造请求实现与其他系统进行交互】点击:【使用SpringCloudHy... 查看详情
seata:分布式事务(代码片段)
SpringCloudAlibabaSeata:分布式事务一、Seata简介1.Seata的由来随着互联网项目的发展,分布式架构的项目显示出了独特的独特的魅力。但是分布式事务问题在分布式架构项目中越显重要。2019年1月阿里巴巴团队发起了开源项目Fesca... 查看详情
springcloudalibabaseata集成(代码片段)
文章目录SeataServer部署:DB存储模式+Nacos(注册&配置中心)部署步骤一:下载安装包步骤二:建表(仅db模式)步骤三:修改store.mode步骤四:修改数据库连接步骤五:配置Nacos步骤六:启动SeataServerSeataCl... 查看详情
springcloud微服务架构(基础详解)(代码片段)
...acos集群与持久化SpringCloudAlibabaSentinel服务熔断降级、限流SpringCloudAlibabaSeata分布式事务框架微服务架构的概念微服务架构是一种架构模式,它提倡将单一的应用程序划分成一组小的服务,服务之间互相协 查看详情
4.抽样分布—卡方分布f分布t分布beta分布gamma分布——python实战
...统计量常用统计量次度统计量充分统计量2、什么是抽样分布3、卡方分布概率密度python代码实现性质非中心卡方分布4、t分布概率密度python代码实现性质5、F分布概率密度python代码实现性质6、样本分布7、其他类型分布Gamma分布族Be... 查看详情
常用的概率分布:二项式分布,贝塔分布,狄里克雷分布
知识点:伯努利分布、二项式分布、多项式分布、先验概率,后验概率,共轭分布、贝塔分布、贝塔-二项分布、负二项分布、狄里克雷分布,伽马函数、分布一,伯努利分布(bernoulidistribution)又叫做0-1分布,指一次随机试验,结... 查看详情
分布式(分布式事务/分布式协议/分布式id/分布式锁)
分布式事务/分布式协议一文说清各种分布式一致性协议_wh柒八九的博客-CSDN博客_分布式一致性协议分布式ID 美团(Leaf)分布式ID算法_wh柒八九的博客-CSDN博客_美团leaf算法 脑裂问题解决办法高可用集群的脑裂问题_wh柒八... 查看详情
抽样分布t分布
定义t分布设X~N(0,1),Y~ χ2(n),且X,Y相互独立,则称随机变量 服从自由度为n的t分布(学生氏分布)记为t~t(n),其概率密度为由于tn(x)是偶函数,其图形关于y轴对称。当n趋于无穷大时,t分布以标准正态... 查看详情
(0,1)分布(两点分布)
定义两点分布的期望和方差期望[EX=p]方差[DX=p(1-p)]注:证明见二项分布. 查看详情
正态分布与偏态分布的概念
正态分布又称高斯分布,是一种最重要的连续型分布。它是以均数为中心呈对称的钟型分布。早在1733年a.demoivre首先提出这种分布的方程,他以此作为二项分布的极限形式。至19世纪初期,德国数学家c.f.gauss与法国数学家p.s.delapla... 查看详情
伯努利分布二项分布多项分布beta分布dirichlet分布
1.伯努利分布伯努利分布(Bernoullidistribution)又名两点分布或0-1分布,介绍伯努利分布前首先需要引入伯努利试验(Bernoullitrial)。伯努利试验是只有两种可能结果的单次随机试验,即对于一个随机变量X而言: 伯努利试验都可... 查看详情
二项分布,多项分布,以及与之对应的beta分布和狄利克雷分布
1.二项分布与beta分布对应 2.多项分布与狄利克雷分布对应3.二项分布是什么?n次bernuli试验服从二项分布 二项分布是N次重复bernuli试验结果的分布。bernuli实验是什么?做一次抛硬币实验,该试验结果只有2种情况,x=1,... 查看详情