zookeeper使用分布式锁

使用

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

        //锁节点路径必须以/开始
        InterProcessMutex mutex = new InterProcessMutex(client, "/curator/lock");

        mutex.acquire();
        System.out.println("拿到锁");

        mutex.release();
        System.out.println("释放锁");

        //client.close();

    }
}

web项目中,锁封装成工具

参考的时我的redis锁

boot项目,maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.study</groupId>
    <artifactId>zookeeper-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <!--curator2.12.0和zookeeper3.4版本匹配-->
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>


</project>

配置文件application.yml

#集群用逗号隔开
zookeeper:
  server: 127.0.0.1:2181

配置类

@Configuration
@ConfigurationProperties(prefix = "zookeeper")
@Data
public class ZookeerpConfig {
    private String server;

    @Bean
    public CuratorFramework curatorFramework() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(100, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient(server, retryPolicy);
        client.start();
        return client;
    }
}

锁工具


@Slf4j
public class ZookeeperLock {

    private CuratorFramework curatorFramework;

    private String KEY_PREFIX = "/curator/lock/";
    private String key;

    private InterProcessMutex mutex;

    public ZookeeperLock(CuratorFramework curatorFramework, String key) {
        this.curatorFramework = curatorFramework;
        this.key = key;
        mutex = new InterProcessMutex(curatorFramework, KEY_PREFIX + key);
    }

    public ZookeeperLock() {
    }

    public void acquire() {
        try {
            mutex.acquire();
            log.debug(key + "获取锁");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    public void release() {
        try {
            mutex.release();
            log.debug(key + "释放锁");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

}


锁的使用


@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private CuratorFramework curatorFramework;

    @Autowired
    private UserService userService;

    //http://localhost:8080/user/update?id=1&name=lipo
    @RequestMapping("/update")
    public Object update(Integer id, String name) {
        ZookeeperLock zookeeperLock = new ZookeeperLock(curatorFramework, "user@update@" + id);
        try {
            zookeeperLock.acquire();
            User update = userService.update(id, name);
            return update;
        } finally {
            zookeeperLock.release();
        }

    }
}

可以参考我的redis锁,用注解封装

参考项目https://github.com/mingwulipo/zookeeper-demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值