Curator
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
zookeeper:
address: 192.168.139.101:2181
timeout: 4000
package com.config;
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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CuratorConfig {
@Value("${zookeeper.address}")
private String zkUrl;
@Bean(initMethod = "start",destroyMethod = "close")
public CuratorFramework curatorFrameworkClient() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zkUrl, retryPolicy);
return client;
}
}
@Autowired
private CuratorFramework client;
@GetMapping("/curator")
public String curator() {
String lockPath = "/lock";
InterProcessMutex lock = new InterProcessMutex(client, lockPath);
try {
if (lock.acquire(10, TimeUnit.SECONDS)) {
log.info("获取到锁");
Thread.sleep(10000);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
log.info("释放锁");
try {
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
}
return "执行完毕";
}
自定义实现zk锁
package com.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@Slf4j
public class ZkLock implements AutoCloseable, Watcher {
public ZooKeeper zooKeeper;
public String zNode;
public ZkLock(String addr, Integer expireTime) throws IOException {
this.zooKeeper = new ZooKeeper(addr, expireTime, this);
}
public ZkLock() throws IOException {
this.zooKeeper = new ZooKeeper("192.168.139.101:2181", 10000, this);
}
public boolean getLock(String businessCode) {
try {
Stat stat = zooKeeper.exists("/" + businessCode, false);
if (stat == null) {
zooKeeper.create("/" + businessCode, businessCode.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zNode = zooKeeper.create("/" + businessCode + "/" + businessCode + "_", businessCode.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
List<String> children = zooKeeper.getChildren("/" + businessCode, false);
Collections.sort(children);
String first = children.get(0);
if (zNode.endsWith(first)) {
return true;
}
String last = first;
for (String node : children) {
if (zNode.endsWith(node)) {
zooKeeper.exists("/" + businessCode + "/" + last, true);
} else {
last = node;
}
}
synchronized (this) {
wait();
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public void close() throws Exception {
zooKeeper.delete(zNode, -1);
zooKeeper.close();
log.info("我已经释放了锁");
}
@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
synchronized (this) {
notify();
}
}
}
}
@GetMapping("/lock")
public String lock() throws IOException {
log.info("进入方法");
try(ZkLock zkLock = new ZkLock()) {
if (zkLock.getLock("order")) {
log.info("我获取到锁");
Thread.sleep(10000);
}
} catch (Exception e) {
e.printStackTrace();
}
log.info("执行完毕");
return "";
}