Curator可重入分布式锁的实现

Curator实现zk分布式锁的确很简单,比zkclient便捷太多了。
部分内容参考 stwen 阿甘正专,向原作者致谢。

环境:SpringBoot,redis集群,zookeeper3.4.14,Curator4.2,jmeter测试。
相关依赖:
		
            org.apache.curator
            curator-recipes
            4.2.0
        
        
            org.apache.curator
            curator-framework
            4.2.0
            
                
                    zookeeper
                    org.apache.zookeeper
                
            
        
        
            org.apache.zookeeper
            zookeeper
            3.4.7
        
        
        
            org.projectlombok
            lombok
            1.18.8
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            2.1.7.RELEASE
        
配置文件 application.properties
#重试次数
curator.retryCount=5
#重试间隔时间
curator.elapsedTimeMs=5000
#zookeeper 地址
curator.connectString=192.168.37.134:2181
#session超时时间
curator.sessionTimeoutMs=60000
#连接超时时间
curator.connectionTimeoutMs=5000

spring.redis.cluster.nodes=192.168.37.134:7001,192.168.37.134:7002,192.168.37.134:7003,192.168.37.134:7004,192.168.37.134:7005,192.168.37.134:7006
Configuration配置类
import lombok.Data;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
  
@Configuration
@ConfigurationProperties(prefix = "curator")
@Data
public class CuratorConfig {
   private int retryCount;                //连接重试次数
   private int elapsedTimeMs;             //重试的间隔时间
   private String connectString;          //zookeeper连接地址
   private int sessionTimeoutMs;          //session超时时间
   private int connectionTimeoutMs;       //连接超时时间

   @Bean(initMethod = "start")
   public CuratorFramework getCuratorFramework(){
      RetryPolicy retryPolicy=new ExponentialBackoffRetry(elapsedTimeMs,retryCount);
      CuratorFramework curatorFramework= CuratorFrameworkFactory.builder()
              .connectString(connectString)
              .sessionTimeoutMs(sessionTimeoutMs)
              .retryPolicy(retryPolicy)
              .build();
      return curatorFramework;

   }
}
测试业务类 ,实现分布式锁

取得可重入互斥锁InterProcessMutex,以lock.acquire()加锁,lock.release()解锁。

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class OrderImp {
    @Autowired
    private CuratorFramework curatorFramework;
    @Autowired
    private RedisTemplate rt;
    private static int sellnum=0;                       //售出数量
    private final String path="/distribute/lock";       //zk临时节点位置
    /*redis现有库存 store=100*/
    public void generateOrder()
    {
        InterProcessMutex rlock=new InterProcessMutex(curatorFramework,path);

        try {
        rlock.acquire();
        log.info(Thread.currentThread().getName()+"取锁成功");
        int store=Integer.valueOf(rt.opsForValue().get("store"));
        if(0
测试Controller

Curator可重入分布式锁的实现_第1张图片

jmeter测试

500线程,重复2次访问。
Curator可重入分布式锁的实现_第2张图片
Curator可重入分布式锁的实现_第3张图片

结果正常

Curator可重入分布式锁的实现_第4张图片
Curator可重入分布式锁的实现_第5张图片

你可能感兴趣的:(zookeeper,redis,Intellij,Idea)