荣耀彩票代理

IT技术互动交流平台

深入理解分布式事务

作者:biakia  来源:IT165收集  发布日期:2016-12-19 20:32:17

WOZAISHANGYIQIJIESHAOLEspringDESHIWUYUANLI(XIANGQINGJIAN《SHENRULIJIEspringSHIWUYUANLI》),SpringSHIWUBENZHISHIDANJIXIADESHIWU,SHIYOUSHUJUKUBENSHENBAOZHENGDE。JINTIAN,WOJIANGJIESHAOYIZHONGBIJIAOFUZADESHIWU:FENBUSHISHIWU。

1、什么是分布式事务

FENBUSHISHIWUJIUSHIZHISHIWUDECANYUZHE、ZHICHISHIWUDEFUWUQI、ZIYUANFUWUQIYIJISHIWUGUANLIQIFENBIEWEIYUBUTONGDEFENBUSHIXITONGDEBUTONGJIEDIANZHISHANG。YISHANGSHIBAIDUBAIKEDEJIESHI,JIANDANDESHUO,JIUSHIYICIDADECAOZUOYOUBUTONGDEXIAOCAOZUOZUCHENG,ZHEIXIEXIAODECAOZUOFENBUZAIBUTONGDEFUWUQISHANG,QIESHUYUBUTONGDEYINGYONG,FENBUSHISHIWUXUYAOBAOZHENGZHEIXIEXIAOCAOZUOYAOMEQUANBUCHENGGONG,YAOMEQUANBUSHIBAI。BENZHISHANGLAISHUO,FENBUSHISHIWUJIUSHIWEILEBAOZHENGBUTONGSHUJUKUDESHUJUYIZHIXING。

2、分布式事务的产生的原因

2.1、数据库分库分表

DANGSHUJUKUDANBIAOYINIANCHANSHENGDESHUJUCHAOGUO1000W,NEIMEJIUYAOKAOLVFENKUFENBIAO,JUTIFENKUFENBIAODEYUANLIZAICIBUZUOJIESHI,YIHOUYOUKONGXIANGXISHUO,JIANDANDESHUOJIUSHIYUANLAIDEYIGESHUJUKUBIANCHENGLEDUOGESHUJUKU。ZHEISHIHOU,RUGUOYIGECAOZUOJIFANGWEN01KU,YOUFANGWEN02KU,ERQIEYAOBAOZHENGSHUJUDEYIZHIXING,NEIMEJIUYAOYONGDAOFENBUSHISHIWU。

2.2、应用SOA化

SUOWEIDESOAHUA,JIUSHIYEWUDEFUWUHUA。BIRUYUANLAIDANJIZHICHENGLEZHENGGEDIANSHANGWANGZHAN,XIANZAIDUIZHENGGEWANGZHANJINXINGCHAIJIE,FENLICHULEDINGDANZHONGXIN、YONGHUZHONGXIN、KUCUNZHONGXIN。DUIYUDINGDANZHONGXIN,YOUZHUANMENDESHUJUKUCUNCHUDINGDANXINXI,YONGHUZHONGXINYEYOUZHUANMENDESHUJUKUCUNCHUYONGHUXINXI,KUCUNZHONGXINYEHUIYOUZHUANMENDESHUJUKUCUNCHUKUCUNXINXI。ZHEISHIHOURUGUOYAOTONGSHIDUIDINGDANHEKUCUNJINXINGCAOZUO,NEIMEJIUHUISHEJIDAODINGDANSHUJUKUHEKUCUNSHUJUKU,WEILEBAOZHENGSHUJUYIZHIXING,JIUXUYAOYONGDAOFENBUSHISHIWU。

荣耀彩票代理YISHANGLIANGZHONGQINGKUANGBIAOXIANGBUTONG,DANSHIBENZHIXIANGTONG,DOUSHIYINWEIYAOCAOZUODESHUJUKUBIANDUOLE!

3、事务的ACID特性

3.1、原子性(A)

荣耀彩票代理SUOWEIDEYUANZIXINGJIUSHISHUO,ZAIZHENGGESHIWUZHONGDESUOYOUCAOZUO,YAOMEQUANBUWANCHENG,YAOMEQUANBUBUZUO,MEIYOUZHONGJIANZHUANGTAI。DUIYUSHIWUZAIZHIXINGZHONGFASHENGCUOWU,SUOYOUDECAOZUODOUHUIBEIHUIGUN,ZHENGGESHIWUJIUXIANGCONGMEIBEIZHIXINGGUOYIYANG。

3.2、一致性(C)

SHIWUDEZHIXINGBIXUBAOZHENGXITONGDEYIZHIXING,JIUNAZHUANZHANGWEILI,AYOU500YUAN,BYOU300YUAN,RUGUOZAIYIGESHIWULIACHENGGONGZHUANJIB50YUAN,NEIMEBUGUANBINGFADUOSHAO,BUGUANFASHENGSHENME,ZHIYAOSHIWUZHIXINGCHENGGONGLE,NEIMEZUIHOUAZHANGHUYIDINGSHI450YUAN,BZHANGHUYIDINGSHI350YUAN。

3.3、隔离性(I)

SUOWEIDEGELIXINGJIUSHISHUO,SHIWUYUSHIWUZHIJIANBUHUIHUXIANGYINGXIANG,YIGESHIWUDEZHONGJIANZHUANGTAIBUHUIBEIQITASHIWUGANZHI。

3.4、持久性(D)

SUOWEIDECHIJIUXING,JIUSHISHUOYIDANSHIWUWANCHENGLE,NEIMESHIWUDUISHUJUSUOZUODEBIANGENGJIUWANQUANBAOCUNZAILESHUJUKUZHONG,JISHIFASHENGTINGDIAN,XITONGDANGJIYESHIRUCI。

4、分布式事务的应用场景

4.1、支付

荣耀彩票代理ZUIJINGDIANDECHANGJINGJIUSHIZHIFULE,YIBIZHIFU,SHIDUIMAIJIAZHANGHUJINXINGKOUKUAN,TONGSHIDUIMAIJIAZHANGHUJINXINGJIAQIAN,ZHEIXIECAOZUOBIXUZAIYIGESHIWULIZHIXING,YAOMEQUANBUCHENGGONG,YAOMEQUANBUSHIBAI。ERDUIYUMAIJIAZHANGHUSHUYUMAIJIAZHONGXIN,DUIYINGDESHIMAIJIASHUJUKU,ERMAIJIAZHANGHUSHUYUMAIJIAZHONGXIN,DUIYINGDESHIMAIJIASHUJUKU,DUIBUTONGSHUJUKUDECAOZUOBIRANXUYAOYINRUFENBUSHISHIWU。

4.2、在线下单

荣耀彩票代理MAIJIAZAIDIANSHANGPINGTAIXIADAN,WANGWANGHUISHEJIDAOLIANGGEDONGZUO,YIGESHIKOUKUCUN,DIERGESHIGENGXINDINGDANZHUANGTAI,KUCUNHEDINGDANYIBANSHUYUBUTONGDESHUJUKU,XUYAOSHIYONGFENBUSHISHIWUBAOZHENGSHUJUYIZHIXING。

5、常见的分布式事务解决方案

5.1、基于XA协议的两阶段提交

荣耀彩票代理XASHIYIGEFENBUSHISHIWUXIEYI,YOUTuxedoTICHU。XAZHONGDAZHIFENWEILIANGBUFEN:SHIWUGUANLIQIHEBENDIZIYUANGUANLIQI。QIZHONGBENDIZIYUANGUANLIQIWANGWANGYOUSHUJUKUSHIXIAN,BIRUOracle、DB2ZHEIXIESHANGYESHUJUKUDOUSHIXIANLEXAJIEKOU,ERSHIWUGUANLIQIZUOWEIQUANJUDEDIAODUZHE,FUZEGEGEBENDIZIYUANDETIJIAOHEHUIGUN。XASHIXIANFENBUSHISHIWUDEYUANLIRUXIA:

荣耀彩票代理ZONGDELAISHUO,XAXIEYIBIJIAOJIANDAN,ERQIEYIDANSHANGYESHUJUKUSHIXIANLEXAXIEYI,SHIYONGFENBUSHISHIWUDECHENGBENYEBIJIAODI。DANSHI,XAYEYOUZHIMINGDEQUEDIAN,NEIJIUSHIXINGNENGBULIXIANG,TEBIESHIZAIJIAOYIXIADANLIANLU,WANGWANGBINGFALIANGHENGAO,XAWUFAMANZUGAOBINGFACHANGJING。XAMUQIANZAISHANGYESHUJUKUZHICHIDEBIJIAOLIXIANG,ZAImysqlSHUJUKUZHONGZHICHIDEBUTAILIXIANG,mysqlDEXASHIXIAN,MEIYOUJILUprepareJIEDUANRIZHI,ZHUBEIQIEHUANHUIDAOZHIZHUKUYUBEIKUSHUJUBUYIZHI。XUDUOnosqlYEMEIYOUZHICHIXA,ZHEIRANGXADEYINGYONGCHANGJINGBIANDEFEICHANGXIAAI。

5.2、消息事务+最终一致性

SUOWEIDEXIAOXISHIWUJIUSHIJIYUXIAOXIZHONGJIANJIANDELIANGJIEDUANTIJIAO,BENZHISHANGSHIDUIXIAOXIZHONGJIANJIANDEYIZHONGTESHULIYONG,TASHIJIANGBENDISHIWUHEFAXIAOXIFANGZAILEYIGEFENBUSHISHIWULI,BAOZHENGYAOMEBENDICAOZUOCHENGGONGCHENGGONGBINGQIEDUIWAIFAXIAOXICHENGGONG,YAOMELIANGZHEDOUSHIBAI,KAIYUANDERocketMQJIUZHICHIZHEIYITEXING,JUTIYUANLIRUXIA:

1、A系统向消息中间件发送一条预备消息
2、消息中间件保存预备消息并返回成功
3、A执行本地事务
4、A发送提交消息给消息中间件

TONGGUOYISHANG4BUWANCHENGLEYIGEXIAOXISHIWU。DUIYUYISHANGDE4GEBUZHOU,MEIGEBUZHOUDOUKENENGCHANSHENGCUOWU,XIAMIANYIYIFENXI:

步骤一出错,则整个事务失败,不会执行A的本地操作 步骤二出错,则整个事务失败,不会执行A的本地操作 步骤三出错,这时候需要回滚预备消息,怎么回滚?答案是A系统实现一个消息中间件的回调接口,消息中间件会去不断执行回调接口,检查A事务执行是否执行成功,如果失败则回滚预备消息 步骤四出错,这时候A的本地事务是成功的,那么消息中间件要回滚A吗?答案是不需要,其实通过回调接口,消息中间件能够检查到A执行成功了,这时候其实不需要A发提交消息了,消息中间件可以自己对消息进行提交,从而完成整个消息事务

JIYUXIAOXIZHONGJIANJIANDELIANGJIEDUANTIJIAOWANGWANGYONGZAIGAOBINGFACHANGJINGXIA,JIANGYIGEFENBUSHISHIWUCHAICHENGYIGEXIAOXISHIWU(AXITONGDEBENDICAOZUO+FAXIAOXI)+BXITONGDEBENDICAOZUO,QIZHONGBXITONGDECAOZUOYOUXIAOXIQUDONG,ZHIYAOXIAOXISHIWUCHENGGONG,NEIMEACAOZUOYIDINGCHENGGONG,XIAOXIYEYIDINGFACHULAILE,ZHEISHIHOUBHUISHOUDAOXIAOXIQUZHIXINGBENDICAOZUO,RUGUOBENDICAOZUOSHIBAI,XIAOXIHUIZHONGTOU,ZHIDAOBCAOZUOCHENGGONG,ZHEIYANGJIUBIANXIANGDISHIXIANLEAYUBDEFENBUSHISHIWU。YUANLIRUXIA:

SUIRANSHANGMIANDEFANGANNENGGOUWANCHENGAHEBDECAOZUO,DANSHIAHEBBINGBUSHIYANGEYIZHIDE,ERSHIZUIZHONGYIZHIDE,WOMENZAIZHEILIXISHENGLEYIZHIXING,HUANLAILEXINGNENGDEDAFUDUTISHENG。DANGRAN,ZHEIZHONGWANFAYESHIYOUFENGXIANDE,RUGUOBYIZHIZHIXINGBUCHENGGONG,NEIMEYIZHIXINGHUIBEIPOHUAI,JUTIYAOBUYAOWAN,HAISHIDEKANYEWUNENGGOUCHENGDANDUOSHAOFENGXIAN。

5.3、TCC编程模式

SUOWEIDETCCBIANCHENGMOSHI,YESHILIANGJIEDUANTIJIAODEYIGEBIANZHONG。TCCTIGONGLEYIGEBIANCHENGKUANGJIA,JIANGZHENGGEYEWULUOJIFENWEISANKUAI:Try、ConfirmHECancelSANGECAOZUO。YIZAIXIANXIADANWEILI,TryJIEDUANHUIQUKOUKUCUN,ConfirmJIEDUANZESHIQUGENGXINDINGDANZHUANGTAI,RUGUOGENGXINDINGDANSHIBAI,ZEJINRUCancelJIEDUAN,HUIQUHUIFUKUCUN。ZONGZHI,TCCJIUSHITONGGUODAIMARENWEISHIXIANLELIANGJIEDUANTIJIAO,BUTONGDEYEWUCHANGJINGSUOXIEDEDAIMADOUBUYIYANG,FUZADUYEBUYIYANG,YINCI,ZHEIZHONGMOSHIBINGBUNENGHENHAODIBEIFUYONG。

6、总结

荣耀彩票代理FENBUSHISHIWU,BENZHISHANGSHIDUIDUOGESHUJUKUDESHIWUJINXINGTONGYIKONGZHI,ANZHAOKONGZHILIDUKEYIFENWEI:BUKONGZHI、BUFENKONGZHIHEWANQUANKONGZHI。BUKONGZHIJIUSHIBUYINRUFENBUSHISHIWU,BUFENKONGZHIJIUSHIGEZHONGBIANZHONGDELIANGJIEDUANTIJIAO,BAOKUOSHANGMIANTIDAODEXIAOXISHIWU+ZUIZHONGYIZHIXING、TCCMOSHI,ERWANQUANKONGZHIJIUSHIWANQUANSHIXIANLIANGJIEDUANTIJIAO。BUFENKONGZHIDEHAOCHUSHIBINGFALIANGHEXINGNENGHENHAO,QUEDIANSHISHUJUYIZHIXINGJIANRUOLE,WANQUANKONGZHIZESHIXISHENGLEXINGNENG,BAOZHANGLEYIZHIXING,JUTIYONGNAZHONGFANGSHI,ZUIZHONGHAISHIQUJUEYUYEWUCHANGJING。ZUOWEIJISHURENYUAN,YIDINGBUNENGWANGLEJISHUSHIWEIYEWUFUWUDE,BUYAOWEILEJISHUERJISHU,ZHENDUIBUTONGYEWUJINXINGJISHUXUANXINGYESHIYIZHONGHENZHONGYAODENENGLI!

荣耀彩票代理========================================================

荣耀彩票代理RUGUOJUEDEHAIBUCUO,QINGSUIYIDASHANG

Tag标签:      
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规