redis-game-transaction

README.mdgit

Redis-分佈式-事務

在大型遊戲中常用分佈式,分佈式中由於遊戲邏輯會常常遊戲事務,藉助redis某些特性咱們能夠實現分佈式鎖和分佈式事務。不少redis集羣不支持redis的事務特性。
這個框架用來解決分佈式服務器下redis集羣事務失效的狀況下多線程分佈式鎖和分佈式鎖,支持獨佔鎖,共享鎖,讀寫鎖,而且支持事務提交失敗狀況下的回滾操做,讓開發者能夠有更多時間側重遊戲邏輯.github

互斥鎖使用例子

可參考test下的entity.redis

  1. 生成事務緣由,GameTransactionCauseImpl裏面構造.
  2. 生成鎖實體,GameTransactionEntityFactoryImpl裏面構造TimeMutexEntity.
  3. 提交鎖實體跟事務,transactionService.commitTransaction裏面提交.
  4. 後去事務提交結果,根據返回值作出判斷.

讀取鎖使用例子

可參考test下的read 能夠設置默認是否成功讀取到服務器

  1. 生成讀取事務緣由GameTransactionCause裏面已經構造好了.
  2. 生成讀取鎖實體GameTransactionEntityFactory裏面已經構造好了.
  3. 提交讀取鎖實體跟事務transactionService.commitTransaction裏面提交.
  4. 後去事務提交結果 根據返回值作出判斷.

標記鎖使用例子

可參考test下的lockattchment能夠設置默認是否成功讀取到多線程

寫鎖例子以下

  1. 生成事務緣由,GameTransactionCauseImpl裏面構造.
  2. 生成鎖實體,GameTransactionEntityFactoryImpl裏面構造.
  3. 生成鎖標記內容 GameTransactionEntity對象獲取GameTransactionLockInterface鎖使用setContent
  4. 提交鎖實體跟事務,transactionService.commitTransaction裏面提交.
  5. 後去事務提交結果,根據返回值作出判斷.

讀鎖例子以下

  1. 生成讀取事務緣由GameTransactionCause裏面構造.
  2. 生成讀取鎖實體GameTransactionEntityFactory裏面構.
  3. 生成鎖標記內容 GameTransactionEntity對象獲取GameTransactionLockInterface鎖使用setContent
  4. 提交讀取鎖實體跟事務transactionService.commitTransaction裏面提交.
  5. 後去事務提交結果 根據返回值作出判斷.

強制寫鎖使用例子以下

可參考test下的force
當鎖存在的狀況,延遲鎖佔用時間。
例子以下
1. 生成事務緣由,GameTransactionCauseImpl裏面構造.
2. 生成鎖實體,GameTransactionEntityFactoryImpl裏面構造ForceEntity.
3. 提交鎖實體跟事務,transactionService.commitTransaction裏面提交.
4. 後去事務提交結果,根據返回值作出判斷.框架

帶時間寫鎖使用例子以下

參考test下的testlock.
當鎖存在的狀況,延遲鎖佔用時間。
例子以下maven

  1. 生成事務緣由,GameTransactionCauseImpl裏面構造.
  2. 生成鎖實體,GameTransactionEntityFactoryImpl裏面構造TestTimeMutexEntity.
  3. 提交鎖實體跟事務,transactionService.commitTransaction裏面提交.
  4. 後去事務提交結果,根據返回值作出判斷.

帶時間等待寫鎖使用例子以下

參考test下的wait.當鎖失敗的狀況下,會嘗試每秒檢查鎖,直到設置的等待時間結束.例子以下分佈式

  1. 生成事務緣由,GameTransactionCauseImpl裏面構造.
  2. 生成鎖實體,GameTransactionEntityFactoryImpl裏面構造WaitMutexEntity.
  3. 提交鎖實體跟事務,transactionService.commitTransaction裏面提交.
  4. 後去事務提交結果,根據返回值作出判斷.

帶事務回滾寫鎖使用例子以下

參考test下的rollback.當事務提交失敗的狀況下,根據提交進度進行rollback.例子以下spa

  1. 生成事務緣由,GameTransactionCauseImpl裏面構造.
  2. 生成鎖實體,GameTransactionEntityFactoryImpl裏面構造RollbackMutexEntity.
  3. 提交鎖實體跟事務,transactionService.commitTransaction裏面提交.
  4. 後去事務提交結果,根據返回值作出判斷.

代碼最後經過maven部署線程

  • 做者qq 330258845
  • QQ羣310158485
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息