distributed-tools

This is a simple distributed tools based on Redis.

Categories

Categories

Redis Data Databases
GroupId

GroupId

top.crossoverjie.opensource
ArtifactId

ArtifactId

distributed-redis-tool
Last Version

Last Version

1.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

distributed-tools
This is a simple distributed tools based on Redis.
Source Code Management

Source Code Management

https://github.com/crossoverJie/distributed-redis-tool

Download distributed-redis-tool

How to add to project

<!-- https://jarcasting.com/artifacts/top.crossoverjie.opensource/distributed-redis-tool/ -->
<dependency>
    <groupId>top.crossoverjie.opensource</groupId>
    <artifactId>distributed-redis-tool</artifactId>
    <version>1.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/top.crossoverjie.opensource/distributed-redis-tool/
implementation 'top.crossoverjie.opensource:distributed-redis-tool:1.0.5'
// https://jarcasting.com/artifacts/top.crossoverjie.opensource/distributed-redis-tool/
implementation ("top.crossoverjie.opensource:distributed-redis-tool:1.0.5")
'top.crossoverjie.opensource:distributed-redis-tool:jar:1.0.5'
<dependency org="top.crossoverjie.opensource" name="distributed-redis-tool" rev="1.0.5">
  <artifact name="distributed-redis-tool" type="jar" />
</dependency>
@Grapes(
@Grab(group='top.crossoverjie.opensource', module='distributed-redis-tool', version='1.0.5')
)
libraryDependencies += "top.crossoverjie.opensource" % "distributed-redis-tool" % "1.0.5"
[top.crossoverjie.opensource/distributed-redis-tool "1.0.5"]

Dependencies

compile (14)

Group / Artifact Type Version
redis.clients : jedis jar 2.9.0
org.springframework.data : spring-data-redis jar 1.8.6.RELEASE
com.google.guava : guava jar 19.0
org.slf4j : slf4j-api jar 1.7.7
ch.qos.logback : logback-core jar 1.0.13
ch.qos.logback : logback-classic jar 1.0.13
org.springframework : spring-aop jar 4.2.1.RELEASE
org.springframework : spring-aspects jar 4.2.1.RELEASE
org.springframework : spring-beans jar 4.2.1.RELEASE
org.springframework : spring-context jar 4.2.1.RELEASE
org.springframework : spring-context-support jar 4.2.1.RELEASE
org.springframework : spring-core jar 4.2.1.RELEASE
org.springframework : spring-web jar 4.2.1.RELEASE
org.springframework : spring-webmvc jar 4.2.1.RELEASE

provided (1)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5

test (3)

Group / Artifact Type Version
junit : junit jar 4.8.2
org.mockito : mockito-all jar 1.9.5
org.easymock : easymock jar 3.2

Project Modules

There are no modules declared in this project.

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: [email protected]

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 packages.
@ComponentScan(value = "com.crossoverjie.distributed.intercept")

# RedisLimitConfig's package
@ComponentScan(value = "xx.xxx.config")

Native API:

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

Other apis:

@CommonLimit

@CommonLimit
@RequestMapping("/xx")
public void anyMethod(){}

@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>

It can be used for any methods.

Versions

Version
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1