Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法
大家可以自行百度下阿里分布式事務(wù),在這里我就不啰嗦了。下面是阿里分布式事務(wù)開源框架的一些資料,本文是springboot+dubbo+fescar的集成。
快速開始
https://github.com/alibaba/fescar/wiki/Quick-Start
GIT地址
https://github.com/alibaba/fescar
1、sql
CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, `xid` varchar(100) 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`), KEY `idx_unionkey` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `storage_tbl`; CREATE TABLE `storage_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT, `commodity_code` varchar(255) DEFAULT NULL, `count` int(11) DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY (`commodity_code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `order_tbl`; CREATE TABLE `order_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` varchar(255) DEFAULT NULL, `commodity_code` varchar(255) DEFAULT NULL, `count` int(11) DEFAULT 0, `money` int(11) DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `account_tbl`; CREATE TABLE `account_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` varchar(255) DEFAULT NULL, `money` int(11) DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `storage_tbl` VALUES ('1', 'C00321', '100'); INSERT INTO `account_tbl` VALUES ('1', 'U100001', '9999');
2、下載fescar-server,并啟動(dòng)
https://github.com/alibaba/fescar/releases
解壓
sh fescar-server.sh 8091 /home/admin/fescar/data/
win操作系統(tǒng)直接啟動(dòng)
fescar-server.bat
3、下載zookeeper并啟動(dòng)。
4、demo結(jié)構(gòu)
分為庫(kù)存微服務(wù)storage、賬號(hào)微服務(wù)account、訂單微服務(wù)order和購(gòu)買下單的消費(fèi)者purchase。
購(gòu)買流程如下,先調(diào)用storage微服務(wù)減庫(kù)存,然后調(diào)用order微服務(wù)更新賬戶余額和生成訂單。訂單微服務(wù)order也作為消費(fèi)者消費(fèi)account賬號(hào)微服務(wù),賬號(hào)微服務(wù)主要用來(lái)更新賬號(hào)余額。
5、demo地址如下
https://github.com/TalkIsCheapGiveMeMoney/springboot-dubbo-fescar.git
6、依次運(yùn)行storage微服務(wù)、account微服務(wù)和order微服務(wù)、purchase消費(fèi)者。
我們?cè)趧?chuàng)建訂單的時(shí)候模擬異常,具體類如下
package com.hcsoc.order.service; import com.alibaba.fescar.core.context.RootContext; import com.hcsoc.account.api.AccountService; import com.hcsoc.order.api.OrderService; import com.hcsoc.order.api.bean.Order; import lombok.Setter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @Setter public class OrderServiceImpl implements OrderService{ private static final Logger LOGGER = LoggerFactory.getLogger(OrderService.class); @Autowired private AccountService accountService; @Autowired private JdbcTemplate jdbcTemplate; public Order create(String userId, String commodityCode, int orderCount) { LOGGER.info("Order Service Begin ... xid: " + RootContext.getXID()); // 計(jì)算訂單金額 int orderMoney = calculate(commodityCode, orderCount); // 從賬戶余額扣款 accountService.debit(userId, orderMoney); final Order order = new Order(); order.userId = userId; order.commodityCode = commodityCode; order.count = orderCount; order.money = orderMoney; //模擬異常 Integer.parseInt("2u"); KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement pst = con.prepareStatement( "insert into order_tbl (user_id, commodity_code, count, money) values (?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS); pst.setObject(1, order.userId); pst.setObject(2, order.commodityCode); pst.setObject(3, order.count); pst.setObject(4, order.money); return pst; } }, keyHolder); order.id = keyHolder.getKey().longValue(); LOGGER.info("Order Service End ... Created " + order); return order; } private int calculate(String commodityId, int orderCount) { return 200 * orderCount; } }
在購(gòu)買下單之前確認(rèn)下數(shù)據(jù)庫(kù)中的數(shù)據(jù),以便購(gòu)買后對(duì)比,商品單價(jià)200.
我們看到 account_tbl 賬號(hào)表,用戶U100001賬戶余額為9999,庫(kù)存表storage_tbl,商品庫(kù)存是100.訂單表order_tbl沒(méi)有訂單數(shù)據(jù)。
下面我們執(zhí)行購(gòu)買下單操作,
執(zhí)行以下url
http://127.0.0.1:9094/purchase?userId=U100001&commodityCode=C00321&orderCount=4
userId 用戶id,commodityCode 商品編號(hào),orderCount 購(gòu)買數(shù)量。
再去,返回?cái)?shù)據(jù)如下
{ "timestamp": "2019-02-01T02:54:27.422+0000", "status": 500, "error": "Internal Server Error", "message": "For input string: \"2u\"", "path": "/purchase" }
查看上面提到的account_tbl 賬號(hào)表,用戶U100001賬戶余額為9999,庫(kù)存表storage_tbl,商品庫(kù)存是100.訂單表order_tbl沒(méi)有訂單數(shù)據(jù)。說(shuō)明分布式事務(wù)成功。
我們把模擬的異常去掉,再次執(zhí)行購(gòu)買操作。
http://127.0.0.1:9094/purchase?userId=U100001&commodityCode=C00321&orderCount=5
我們發(fā)現(xiàn)商品庫(kù)存由100變成了95,用戶余額由9999變成了8999,并且生成了一個(gè)訂單號(hào),前面提到了商品的單價(jià)是200,數(shù)據(jù)正確。
其他的分布式事務(wù)框架,比如TCC和byteTcc需要操作冪等性,了解TCC和ByteTcc的應(yīng)該知道,開發(fā)工作量很大而且操作還需要冪等性,而阿里的fescar則完全不需要,而且不需要那么多的開發(fā)工作量,希望fescar越來(lái)越成熟。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解SpringBoot開發(fā)案例之整合Dubbo分布式服務(wù)
- Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法
- SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
- 詳解SpringBoot基于Dubbo和Seata的分布式事務(wù)解決方案
- SpringBoot中dubbo+zookeeper實(shí)現(xiàn)分布式開發(fā)的應(yīng)用詳解
- SpringBoot+Dubbo+Zookeeper實(shí)現(xiàn)簡(jiǎn)單分布式開發(fā)的應(yīng)用詳解
- springboot分布式整合dubbo的方式
- springBoot+dubbo+zookeeper實(shí)現(xiàn)分布式開發(fā)應(yīng)用的項(xiàng)目實(shí)踐
- Spring與Dubbo搭建一個(gè)簡(jiǎn)單的分布式詳情
相關(guān)文章
JDBC中Statement和Preparement的使用講解
今天小編就為大家分享一篇關(guān)于JDBC中Statement和Preparement的使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比
本文主要介紹了Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比,分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java基礎(chǔ)之static關(guān)鍵字的使用講解
這篇文章主要介紹了Java基礎(chǔ)之static關(guān)鍵字的使用講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Java實(shí)現(xiàn)泡泡堂對(duì)戰(zhàn)版游戲的示例代碼
本文將利用Java制作經(jīng)典游戲《泡泡堂》,文中使用了MVC模式,分離了模型、視圖和控制器,使得項(xiàng)目結(jié)構(gòu)清晰易于擴(kuò)展,感興趣的可以了解一下2022-04-04Elasticsearch查詢Range Query語(yǔ)法示例
這篇文章主要為大家介紹了Elasticsearch查詢Range Query語(yǔ)法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Java常用數(shù)字工具類 大數(shù)乘法、加法、減法運(yùn)算(2)
這篇文章主要為大家詳細(xì)介紹了Java常用數(shù)字工具類,大數(shù)乘法、加法、減法運(yùn)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java去掉小數(shù)點(diǎn)后面無(wú)效0的方案與建議
當(dāng)前小數(shù)點(diǎn)后面的位數(shù)過(guò)多的時(shí)候,多余的0沒(méi)有實(shí)際意義,下面這篇文章主要給大家介紹了關(guān)于Java去掉小數(shù)點(diǎn)后面無(wú)效0的方案與建議,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07maven的pom.xml中repositories和distributionManagement使用
這篇文章主要介紹了maven的pom.xml中repositories和distributionManagement使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03使用spring實(shí)現(xiàn)郵件的發(fā)送實(shí)例(含測(cè)試,源碼,注釋)
本篇文章主要介紹了使用spring實(shí)現(xiàn)郵件的發(fā)送實(shí)例,詳細(xì)的介紹了使用spring配置實(shí)現(xiàn)郵件發(fā)送,含測(cè)試,源碼,注釋,有興趣的可以下2017-05-05Java狀態(tài)機(jī)的一種優(yōu)雅寫法分享
狀態(tài)機(jī)是一種數(shù)學(xué)模型,對(duì)于我們業(yè)務(wù)實(shí)現(xiàn)有很大的幫助。我們可以用非常多的方法實(shí)現(xiàn)狀態(tài)機(jī),這篇文章就來(lái)介紹一個(gè)狀態(tài)機(jī)優(yōu)雅的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助2023-04-04