分散式事務解決方案---------LCN
阿新 • • 發佈:2018-12-10
1.過多的原理我就不一一介紹了,我就用一個例項來展示LCN分散式事務解決方案的應用。
tx-lcn https://gitee.com/wangliang1991/tx-lcn
springcloud-demo版本的demo https://github.com/codingapi/springcloud-lcn-demo
第一步:
先搭建一個Eureka註冊中心,詳細參照Eureka註冊中心搭建
我這裡註冊中心的埠號為:9000
第二步
將tx-lcn專案中的tx-manager開啟,開啟它的配置檔案修改eureka註冊中心的埠號(這裡埠號是你剛剛搭建註冊中心的埠號),然後修改redis的資訊(你需要在本地大搭建redis服務或者在你的虛擬機器上搭建,可以選擇叢集版和單機版,我這裡選擇單機版),修改tx-manager的埠號,詳細配置如下:
#######################################txmanager-start################################################# #服務埠 server.port=8899 #tx-manager不得修改 spring.application.name=tx-manager spring.mvc.static-path-pattern=/** spring.resources.static-locations=classpath:/static/ #######################################txmanager-end################################################# #zookeeper地址 #spring.cloud.zookeeper.connect-string=127.0.0.1:2181 #spring.cloud.zookeeper.discovery.preferIpAddress = true #eureka 地址 eureka.client.service-url.defaultZone=http://127.0.0.1:9000/eureka/ eureka.instance.prefer-ip-address=true #######################################redis-start################################################# #redis 配置檔案,根據情況選擇叢集或者單機模式 ##redis 叢集環境配置 ##redis cluster #spring.redis.cluster.nodes=127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003 #spring.redis.cluster.commandTimeout=5000 ##redis 單點環境配置 #redis #redis主機地址 spring.redis.host=127.0.0.1 #redis主機埠 spring.redis.port=6379 #redis連結密碼 spring.redis.password=hsm1996 spring.redis.pool.maxActive=10 spring.redis.pool.maxWait=-1 spring.redis.pool.maxIdle=5 spring.redis.pool.minIdle=0 spring.redis.timeout=0 #####################################redis-end################################################### #######################################LCN-start################################################# #業務模組與TxManager之間通訊的最大等待時間(單位:秒) #通訊時間是指:發起方與響應方之間完成一次的通訊時間。 #該欄位代表的是Tx-Client模組與TxManager模組之間的最大通訊時間,超過該時間未響應本次請求失敗。 tm.transaction.netty.delaytime = 5 #業務模組與TxManager之間通訊的心跳時間(單位:秒) tm.transaction.netty.hearttime = 15 #儲存到redis下的資料最大儲存時間(單位:秒) #該欄位僅代表的事務模組資料的最大儲存時間,補償資料會永久儲存。 tm.redis.savemaxtime=30 #socket server Socket對外服務埠 #TxManager的LCN協議的埠 tm.socket.port=9999 #最大socket連線數 #TxManager最大允許的建立連線數量 tm.socket.maxconnection=100 #事務自動補償 (true:開啟,false:關閉) # 說明: # 開啟自動補償以後,必須要配置 tm.compensate.notifyUrl 地址,僅當tm.compensate.notifyUrl 在請求補償確認時返回success或者SUCCESS時,才會執行自動補償,否則不會自動補償。 # 關閉自動補償,當出現數據時也會 tm.compensate.notifyUrl 地址。 # 當tm.compensate.notifyUrl 無效時,不影響TxManager執行,僅會影響自動補償。 tm.compensate.auto=false #事務補償記錄回撥地址(rest api 地址,post json格式) #請求補償是在開啟自動補償時才會請求的地址。請求分為兩種:1.補償決策,2.補償結果通知,可通過通過action引數區分compensate為補償請求、notify為補償通知。 #*注意當請求補償決策時,需要補償服務返回"SUCCESS"字串以後才可以執行自動補償。 #請求補償結果通知則只需要接受通知即可。 #請求補償的樣例資料格式: #{"groupId":"TtQxTwJP","action":"compensate","json":"{\"address\":\"133.133.5.100:8081\",\"className\":\"com.example.demo.service.impl.DemoServiceImpl\",\"currentTime\":1511356150413,\"data\":\"C5IBLWNvbS5leGFtcGxlLmRlbW8uc2VydmljZS5pbXBsLkRlbW9TZXJ2aWNlSW1wbAwSBHNhdmUbehBqYXZhLmxhbmcuT2JqZWN0GAAQARwjeg9qYXZhLmxhbmcuQ2xhc3MYABABJCo/cHVibGljIGludCBjb20uZXhhbXBsZS5kZW1vLnNlcnZpY2UuaW1wbC5EZW1vU2VydmljZUltcGwuc2F2ZSgp\",\"groupId\":\"TtQxTwJP\",\"methodStr\":\"public int com.example.demo.service.impl.DemoServiceImpl.save()\",\"model\":\"demo1\",\"state\":0,\"time\":36,\"txGroup\":{\"groupId\":\"TtQxTwJP\",\"hasOver\":1,\"isCompensate\":0,\"list\":[{\"address\":\"133.133.5.100:8899\",\"isCompensate\":0,\"isGroup\":0,\"kid\":\"wnlEJoSl\",\"methodStr\":\"public int com.example.demo.service.impl.DemoServiceImpl.save()\",\"model\":\"demo2\",\"modelIpAddress\":\"133.133.5.100:8082\",\"channelAddress\":\"/133.133.5.100:64153\",\"notify\":1,\"uniqueKey\":\"bc13881a5d2ab2ace89ae5d34d608447\"}],\"nowTime\":0,\"startTime\":1511356150379,\"state\":1},\"uniqueKey\":\"be6eea31e382f1f0878d07cef319e4d7\"}"} #請求補償的返回資料樣例資料格式: #SUCCESS #請求補償結果通知的樣例資料格式: #{"resState":true,"groupId":"TtQxTwJP","action":"notify"} tm.compensate.notifyUrl=http://ip:port/path #補償失敗,再次嘗試間隔(秒),最大嘗試次數3次,當超過3次即為補償失敗,失敗的資料依舊還會存在TxManager下。 tm.compensate.tryTime=30 #各事務模組自動補償的時間上限(毫秒) #指的是模組執行自動超時的最大時間,該最大時間若過段會導致事務機制異常,該時間必須要模組之間通訊的最大超過時間。 #例如,若模組A與模組B,請求超時的最大時間是5秒,則建議改時間至少大於5秒。 tm.compensate.maxWaitTime=5000 #######################################LCN-end################################################# logging.level.com.codingapi=debug
配置完後啟動tx-manager
訪問http://localhost:8899進入成功,表示LCN 分散式事務協調器搭建成功
第三步
搭建你本地的業務邏輯
程式碼:上傳到github上了
https://github.com/babybabywang/springcloud-lcn-demo