seata的使用
1. Seata 概述
Seata 是 Simple Extensible Autonomous Transaction Architecture 的簡寫,由 feascar 改名而來。
Seata 是阿里開源的分布式事務框架,屬於二階段提交模式。
目前github上已經有 12267 顆星了,也很活躍,最新的提交時間很多都是幾天前。
Seata 有一個重要的機制:行鎖+回滾日志。
每個分支事務對應的數據庫中都需要有一個回滾日志表 UNDO_LOG,在真正修改數據庫記錄之前,都會先記錄修改前的記錄值,以便之后回滾。
在收到回滾請求后,就會根據 UNDO_LOG 生成回滾操作的 SQL 語句來執行。
如果收到的是提交請求,就把 UNDO_LOG 中的相應記錄刪除掉
原理就是spring的aop 有前置處理和后置處理我們的業務SQL語句 這里就不多說了 ,可以自行了解
下面開打:
先下載安裝Seata的服務端了 我這邊用的0.9.0版的
先來張圖:截了一部分 哈哈
Seata下載地址:https://github.com/seata/seata/releases
鏈接:https://pan.baidu.com/s/1lcfS8MjRKf64dbGP8BgEZQ
提取碼:th3j
這個是網盤下載好的包或者自己去seata官網下載 不多說了
解壓進入到seata-server.bat文件中編輯
替換
%JAVACMD% %JAVA_OPTS% -server -Xmx700m -Xms700m -Xmn512m -Xss512k -XX:SurvivorRatio=10 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=512m -XX:-OmitStackTraceInFastThrow -XX:+AggressiveOpts -XX:+UseFastAccessorMethods -XX:-UseAdaptiveSizePolicy -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASEDIR}/logs/java_heapdump.hprof -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 -Xloggc:${BASEDIR}/logs/seata_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dio.netty.leakDetectionLevel=advanced -classpath %CLASSPATH% -Dapp.name="seata-server" -Dapp.repo="%REPO%" -Dapp.home="%BASEDIR%" -Dbasedir="%BASEDIR%" io.seata.server.Server %CMD_LINE_ARGS%
改這個的原因是我的電腦是16G的seata消耗內存 頂不住 電腦好的不用改了
編輯這個兩個文件:
測試用的MySQL數據庫:
MySQL的驅動 5和6是不一樣的 我用的5這個版本
注冊中心用的nacos 和Alibaba的配套起來 用其他的也行
保存保存。。。
seata 的環境就搞好了
接下來啟動nacos 這里就不說了
MySQL數據庫的創建和配置:
CREATE DATABASE seata;
USE seata;
-- the table to store GlobalSession data
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`)
);
-- the table to store BranchSession data
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`)
);
-- the table to store lock data
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`)
);
在MySQL執行這些SQL就可以了
雙擊。。。
到此 啟動完成:
訪問nacos的界面
http://localhost:8848/nacos
用戶名和密碼都是 nacos
seata 的服務也注冊到nacos了 接下來就是 項目環境的搭建了 后面再說 因為要打游戲了。。。