Skip to content

Latest commit

 

History

History
230 lines (156 loc) · 5.65 KB

README.md

File metadata and controls

230 lines (156 loc) · 5.65 KB

distributed-redis-tool

Build Status codecov Maven Central

This is a simple distributed tools based on Redis.

Distributed lock

Distributed limiting

Visit this website for more information.

Contact

Mail: [email protected]

weixinchat.jpg

Distributed lock

Features

  • High performance.
  • No deadlock.
  • Support Redis cluster, single.
  • Non-blocking lock.
  • blocking lock.

Quick start

maven dependency:

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

Set bean:

@Configuration
public class RedisLockConfig {

    @Bean
    public RedisLock build(){
        //Need to get Redis connection 
        RedisLock redisLock = new RedisLock() ;
        HostAndPort hostAndPort = new HostAndPort("127.0.0.1",7000) ;
        JedisCluster jedisCluster = new JedisCluster(hostAndPort) ;
        RedisLock redisLock = new RedisLock.Builder(jedisCluster)
                .lockPrefix("lock_test")
                .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.2</version>
</dependency>
  1. Set bean:
@Configuration
public class RedisLimitConfig {


    @Value("${redis.limit}")
    private int limit;


    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @Bean
    public RedisLimit build() {
        //Need to get Redis connection 
        RedisClusterConnection clusterConnection = jedisConnectionFactory.getClusterConnection();
        JedisCluster jedisCluster = (JedisCluster) clusterConnection.getNativeConnection();
        RedisLimit redisLimit = new RedisLimit.Builder<>(jedisCluster)
                .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.

@CommonLimit

@CommonLimit
public void anyMethod(){}

It can be used for any Methods.