使用
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