Distributed-Kit - 基于redis和zookeeper分布式工具集

Distributed-Kit

基于redis和zookeeper分布式工具集-包括:分布式鎖實現,分布式速率限制器,分布式序列等.

使用

maven: 需先編譯安裝到本地倉庫或者本地私服。

    <dependency>
      <groupId>dance</groupId>
      <artifactId>Distributed-Kit</artifactId>
      <version>0.0.1</version>
    </dependency>

基于Redis實現的分布式鎖(可重入)

public static void main(String[] args){
    JedisPool jedisPool=new JedisPool("127.0.0.1",6379);//實際應用時可通過spring注入
    final RedisDistributedLockTemplate template=new RedisDistributedLockTemplate(jedisPool);//本類線程安全,可通過spring注入
    template.execute("訂單流水號", 5000, new Callback() {//獲取鎖超時時間為5秒
        @Override
        public Object onGetLock() throws InterruptedException {
            //TODO 獲得鎖后要做的事
            return null;
        }

        @Override
        public Object onTimeout() throws InterruptedException {
            //TODO 獲得鎖超時后要做的事
            return null;
        }
    });
}
public static void main(String[] args) throws Exception {
    JedisPool jedisPool=new JedisPool("127.0.0.1",6379);//實際應用時可通過spring注入
    RedisReentrantLock lock=new RedisReentrantLock(jedisPool,"訂單流水號");
    try {
        if (lock.tryLock(5000L, TimeUnit.MILLISECONDS)) {//獲取鎖超時時間為5秒
            //TODO 獲得鎖后要做的事
        }else{
            //TODO 獲得鎖超時后要做的事
        }
    }finally {
        lock.unlock();
    }
}

測試本實現的可靠性見測試用例

基于Zookeeper實現的分布式鎖(可重入)

public static void main(String[] args){
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
    client.start();

    final ZkDistributedLockTemplate template=new ZkDistributedLockTemplate(client);//本類多線程安全,可通過spring注入
    template.execute("訂單流水號", 5000, new Callback() {//獲取鎖超時時間為5秒
        @Override
        public Object onGetLock() throws InterruptedException {
            //TODO 獲得鎖后要做的事
            return null;
        }

        @Override
        public Object onTimeout() throws InterruptedException {
            //TODO 獲得鎖超時后要做的事
            return null;
        }
    });
}

測試本實現的可靠性見測試用例

基于Redis實現的分布式速率限制器

限制的資源,可以是ip,用戶id,訂單id,手機號,等等.

  • 例如限制一個手機號每分鐘只能發1條短信.
  • 例如限制一個手機號每10秒鐘只能發起1次叫車請求.
  • 例如限制一個ip地址每秒鐘只能訪問10次特定的資源.
public class AccessSpeedLimitTest {
    @Test
    public void test1() throws InterruptedException {
        JedisPool jp=new JedisPool("127.0.0.1",6379);
        AccessSpeedLimit accessSpeedLimit=new AccessSpeedLimit(jp);
        SimpleDateFormat sdf=new SimpleDateFormat(" mm:ss");
        while(true){
            //10.0.0.1這個ip每1秒鐘最多訪問5次if塊內代碼
            if(accessSpeedLimit.tryAccess("10.0.0.1", 1,5)){
                System.out.println("yes"+sdf.format(new Date()));
            }else{
                System.out.println("no"+sdf.format(new Date()));
            }
            Thread.sleep(100);
        }
    }

    @Test
    public void test2() throws InterruptedException {
        JedisPool jp=new JedisPool("127.0.0.1",6379);
        final RedisDistributedLockTemplate template=new RedisDistributedLockTemplate(jp);
        LimitRule limitRule=new LimitRule();
        limitRule.setSeconds(1);
        limitRule.setLimitCount(5);
        limitRule.setLockCount(7);
        limitRule.setLockTime(2);
        AccessSpeedLimit accessSpeedLimit=new AccessSpeedLimit(jp);
        SimpleDateFormat sdf=new SimpleDateFormat(" mm:ss");
        while(true){
            //10.0.0.1這個ip每1秒鐘最多訪問5次if塊內代碼.1秒超過10次后,鎖定2秒,2秒內無法訪問.
            if(accessSpeedLimit.tryAccess("10.0.0.1",limitRule)){
                System.out.println("yes"+sdf.format(new Date()));
            }else{
                System.out.println("no"+sdf.format(new Date()));
            }
            Thread.sleep(100);
        }
    }
}

項目地址: https://github.com/yujiasun/Distributed-Kit

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!