http://seata.io/zh-cn/blog/download.html
注:我這邊下載的是1.4.1,seata部署版本需要與SpringBoot依賴的版本相對應!!!!!!
準備好Nacos、mysql
注:nacos設定中心資料是持久化到mysql的!!!!
上傳至伺服器,目錄為:/usr/local/software
# 1、建立目錄
mkdir -p /usr/local/software
# 2、解壓
unzip seata-server-1.4.1.zip
# 3、修改儲存模式 DB
cd seata/conf/
vi file.conf
注:修改為自己的mysql!!!!
## transaction log store, only used in seata-server store { ## store mode: file、db、redis mode = "file" ## database store property db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.cj.jdbc.Driver" url = "jdbc:mysql://47.116.143.16:3306/seata?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai" user = "root" password = "root" minConn = 5 maxConn = 100 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } }
將seata需要的3張表匯入資料庫中,分別是:global_table、branch_table、lock_table
官網地址:http://seata.io/zh-cn/docs/user/quickstart.html
github地址:https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql
注:修改成自己的nacos資訊
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "47.116.143.16:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "47.116.143.16:8848"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
因為Seata的設定中心是nacos,需要把Seata的設定,通過指令碼推播到nacos中
官網地址:https://seata.io/zh-cn/docs/user/configuration/nacos.html
指令碼地址:https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh
config.txt地址(可以暫時不修改設定引數,直接到nacos中修改設定):https://github.com/seata/seata/blob/develop/script/config-center/config.txt
具體config.txt裡的引數解釋:https://seata.io/zh-cn/docs/user/configurations.html
新建2個設定需要與微服務中的設定對應上
service.vgroupMapping.${spring.alibaba.seata.tx-service-group}=default
如下
service.vgroupMapping.order_service_group=default
service.vgroupMapping.product_service_group=default
注意:分組為:SEATA_GROUP
注意:如果seata部署在伺服器,微服務在本地啟動的話,2個服務不在一個區域網下,因此沒法通訊,啟動Seata時,需要指定ip和埠號
sh seata-server.sh -p 8091 -h 47.116.143.16
<dependency> <groupId>com.esotericsoftware</groupId> <artifactId>kryo</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>de.javakaffee</groupId> <artifactId>kryo-serializers</artifactId> <version>0.42</version> </dependency>
將
client.undo.logSerialization=jackson
修改為
client.undo.logSerialization=kryo
在每個微服務所連的庫,新建一張表
-- 注意此處0.3.0+ 增加唯一索引 ux_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;
。。。。
資料庫分表為:order(訂單微服務庫)、product(商品微服務庫)、seata(Seata全域性事務涉及的表)、nacos(Nacos設定中心,mysql持久化)
<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.4.1</version> </dependency> <!-- seata 自身序列化bug問題-開始 --> <dependency> <groupId>com.esotericsoftware</groupId> <artifactId>kryo</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>de.javakaffee</groupId> <artifactId>kryo-serializers</artifactId> <version>0.42</version> </dependency> <!-- seata 自身序列化bug問題-結束 -->
關鍵程式碼片段
order-service
@Autowired OrderMapper orderMapper; @Autowired ProductStockControllerFeign productStockControllerFeign; @Override //開啟分散式事務 Seta AT模式 @GlobalTransactional public ReturnT<String> add() { OrderDO orderDO = new OrderDO(); int outTradeNo = new Random().nextInt(1000); orderDO.setOutTradeNo("T" + outTradeNo); orderDO.setCreateTime(new Date()); int rows = orderMapper.insert(orderDO); if (rows > 0) { //扣減商品庫存 ReturnT<String> reduceReturn = productStockControllerFeign.reduce(); if (ReturnT.isSuccess(reduceReturn)) { log.info("購買成功"); //TODO 模擬異常方式二 // int num = 1 / 0; return ReturnT.buildSuccess("購買成功"); } // 解決全域性攔截器問題,通過介面響應狀態碼,來判斷是否主動拋異常!!!!!!! if (reduceReturn.getCode() != 0) { log.info("扣減商品庫存失敗,介面響應:{}", reduceReturn); throw new BizException(110, "扣減商品庫存失敗"); } log.info("扣減商品庫存失敗"); return ReturnT.buildError("扣減商品庫存失敗"); } log.info("購買失敗"); return ReturnT.buildError("購買失敗"); }
product-service
@Autowired ProductStockMapper productStockMapper; @Override public ReturnT<String> reduceProductStock() { ProductStockDO stockDO = new ProductStockDO(); stockDO.setProductId(10086); stockDO.setBuyNum(1); stockDO.setCreateTime(new Date()); int rows = productStockMapper.insert(stockDO); //TODO 模擬異常方式一 // int num = 1 / 0; if (rows > 0) { log.info("扣減商品庫存成功,rows=" + rows); return ReturnT.buildSuccess("扣減商品庫存成功"); } else { log.info("扣減商品庫存失敗,rows=" + rows); return ReturnT.buildError("扣減失敗"); } }
場景描述:product微服務和order微服務均正常,2個微服務的事務全部提交成功,2個庫都插入資料成功
場景描述:product微服務發生異常,order微服務正常情況,出現異常情況時,需要2個微服務的事務全部回滾,2個庫插入的資料都回滾
場景描述:order微服務發生異常,product微服務正常,出現異常情況時,需要2個微服務的事務全部回滾,2個庫插入的資料都回滾
場景描述:order微服務正常啟動,product微服務未啟動,需要把order微服務插入的資料回滾
https://github.com/543210188/ybchen-seata
https://gitee.com/yenbin_chen/ybchen-seatay