/distributed-redis-tool

🔒 distributed tools

Primary LanguageJavaMIT LicenseMIT

distributed-redis-tool

Build Status codecov Maven Central QQ群

This is a simple distributed tools based on Redis.

Distributed lock

Distributed limiting

Visit this website for more information.

ChangeLog

v1.0.5

  • Fixed #9.
  • Optimization RedisCluster.

v1.0.4

  • Upgrade distributed lock API.
  • Support connection pool.
  • Improve distributed lock performance âš¡.

v1.0.3

  • Upgrade API.
  • Add Anation.
  • Improve performance âš¡ .

Contact

Mail: crossoverJie@gmail.com

Distributed lock

Features

  • High performance.
  • No deadlock.
  • Support Redis cluster, single.
  • Non-blocking lock.
  • blocking lock.
  • Support connection pool.
  • Suppport Spring4.x+.

Quick start

maven dependency:

<dependency>
    <groupId>top.crossoverjie.opensource</groupId>
    <artifactId>distributed-redis-tool</artifactId>
    <version>1.0.4</version>
</dependency>

Set bean:

@Configuration
public class RedisLockConfig {
    private Logger logger = LoggerFactory.getLogger(RedisLockConfig.class);
    
    
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;
    
    @Bean
    public RedisLock build() {
        RedisLock redisLock = new RedisLock.Builder(jedisConnectionFactory,RedisToolsConstant.SINGLE)
                .lockPrefix("lock_")
                .sleepTime(100)
                .build();

        return redisLock;
    }
}

Non-blocking lock:

    @Autowired
    private RedisLock redisLock ;

    public void use() {
        String key = "key";
        String request = UUID.randomUUID().toString();
        try {
            boolean locktest = redisLock.tryLock(key, request);
            if (!locktest) {
                System.out.println("locked error");
                return;
            }


            //do something

        } finally {
            redisLock.unlock(key,request) ;
        }

    }

Blocking lock

redisLock.lock(String key, String request);

Blocking lock, Custom block time

redisLock.lock(String key, String request,int blockTime);

Distributed limiting

Features

  • High performance.
  • native API.
  • Annation API.
  • Support Redis cluster, single.
  • Suppport Spring4.x+

Quick start

maven dependency:

<dependency>
    <groupId>top.crossoverjie.opensource</groupId>
    <artifactId>distributed-redis-tool</artifactId>
    <version>1.0.4</version>
</dependency>
  1. Set bean:
@Configuration
public class RedisLimitConfig {
    private Logger logger = LoggerFactory.getLogger(RedisLimitConfig.class);
    
    @Value("${redis.limit}")
    private int limit;
    
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;
    
    @Bean
    public RedisLimit build() {
        RedisLimit redisLimit = new RedisLimit.Builder(jedisConnectionFactory, RedisToolsConstant.SINGLE)
                .limit(limit)
                .build();
        return redisLimit;
    }
}
  1. Scan com.crossoverjie.distributed.intercept package.
@ComponentScan(value = "com.crossoverjie.distributed.intercept")

Native API:

  	
    boolean limit = redisLimit.limit();
    if (!limit){
        res.setCode(StatusEnum.REQUEST_LIMIT.getCode());
        res.setMessage(StatusEnum.REQUEST_LIMIT.getMessage());
        return res ;
    }

Other apis:

@ControllerLimit

    @ControllerLimit
    public BaseResponse<OrderNoResVO> getOrderNoLimit(@RequestBody OrderNoReqVO orderNoReq) {
        BaseResponse<OrderNoResVO> res = new BaseResponse();
        res.setReqNo(orderNoReq.getReqNo());
        if (null == orderNoReq.getAppId()){
            throw new SBCException(StatusEnum.FAIL);
        }
        OrderNoResVO orderNoRes = new OrderNoResVO() ;
        orderNoRes.setOrderId(DateUtil.getLongTime());
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMessage(StatusEnum.SUCCESS.getMessage());
        res.setDataBody(orderNoRes);
        return res ;
    }

Used for @RequestMapping.

@SpringControllerLimit

If you are using native Spring:

    @SpringControllerLimit(errorCode = 200,errorMsg = "request has limited")
    @RequestMapping("/createOptimisticLimitOrderByRedis/{sid}")
    @ResponseBody
    public String createOptimisticLimitOrderByRedis(@PathVariable int sid) {
        logger.info("sid=[{}]", sid);
        int id = 0;
        try {
            id = orderService.createOptimisticOrderUseRedis(sid);
        } catch (Exception e) {
            logger.error("Exception",e);
        }
        return String.valueOf(id);
    }

Spring xml:

   <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/> 
            <bean class="com.crossoverjie.distributed.intercept.SpringMVCIntercept"/>
        </mvc:interceptor>
    </mvc:interceptors>

@CommonLimit

@CommonLimit
public void anyMethod(){}

It can be used for any Methods.