GithubHelp home page GithubHelp logo

jwpttcg66 / redis-game-transaction Goto Github PK

View Code? Open in Web Editor NEW
154.0 15.0 101.0 2.35 MB

在大型游戏中经常使用分布式,分布式中因为游戏逻辑会经常游戏事务,借助redis特性我们可以实现分布式锁和分布式事务。很多redis集群不支持redis的事务特性。 这个框架用来解决分布式服务器下redis集群事务失效的情况下,基于分布式锁完成分布式事务。支持独占锁,共享锁,读写锁,并且支持事务提交失败情况下的回滚操作,让开发者可以有更多时间侧重游戏逻辑.

License: Apache License 2.0

Java 100.00%
redis transactionservice game-engine redis-game redisgame game-transaction transaction gameengine gametransaction redislock

redis-game-transaction's Introduction

Redis-分布式-事务

在大型游戏中经常使用分布式,分布式中因为游戏逻辑会经常游戏事务,借助redis特性我们可以实现分布式锁和分布式事务。很多redis集群不支持redis的事务特性。 这个框架用来解决分布式服务器下redis集群事务失效的情况下,基于分布式锁完成分布式事务。支持独占锁,共享锁,读写锁,并且支持事务提交失败情况下的回滚操作,让开发者可以有更多时间侧重游戏逻辑.此框架已经上线手游项目两年,经过百万级DAU验证,稳定运行.

互斥锁使用例子

可参考test下的entity.

  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. 当锁存在的情况,延迟锁占用时间。 例子如下

  1. 生成事务原因,GameTransactionCauseImpl里面构造.
  2. 生成锁实体,GameTransactionEntityFactoryImpl里面构造TestTimeMutexEntity.
  3. 提交锁实体跟事务,transactionService.commitTransaction里面提交.
  4. 后去事务提交结果,根据返回值做出判断.

带时间等待写锁使用例子如下

参考test下的wait.当锁失败的情况下,会尝试每秒检查锁,直到设置的等待时间结束.例子如下

  1. 生成事务原因,GameTransactionCauseImpl里面构造.
  2. 生成锁实体,GameTransactionEntityFactoryImpl里面构造WaitMutexEntity.
  3. 提交锁实体跟事务,transactionService.commitTransaction里面提交.
  4. 后去事务提交结果,根据返回值做出判断.

带事务回滚写锁使用例子如下

参考test下的rollback.当事务提交失败的情况下,根据提交进度进行rollback.例子如下

  1. 生成事务原因,GameTransactionCauseImpl里面构造.
  2. 生成锁实体,GameTransactionEntityFactoryImpl里面构造RollbackMutexEntity.
  3. 提交锁实体跟事务,transactionService.commitTransaction里面提交.
  4. 后去事务提交结果,根据返回值做出判断.

已经集成spring和redis集群

参考test下的spring

代码最后通过maven部署,

  • 作者qq 330258845
  • QQ群310158485

redis-game-transaction's People

Contributors

jwpttcg66 avatar thenewsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

redis-game-transaction's Issues

Found potential runtime exception

Hello,
First of all i want to thank you for sharing your code.
But when I was operating some of my files, there was a runtime exception of the type 'java.lang.IlegalArgumentException'. The runtime exception was from method:
com.redis.util.JdomUtils.getIntArrayAttriValue(org.jdom.Element,java.lang.String)

    public static int getIntAttriValue(Element e, String attrName) {
        String attributeValue = e.getAttributeValue(attrName);
        if (attributeValue == null) {
            return -1;
        }
        return Integer.valueOf(attributeValue);
    }

it might cause the program to crash. But the caller may not know these problems and may cause the program to crash suddenly.
so I suggest you add catch or throw to the source code An exception goes out, which is convenient for the caller to develop. Thank you!
The issue can be reproduced by passing the method with non-number string

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.