springboot2整合zookeeper集成curator
2021-05-17 00:30
标签:out art off getchild cep lse runnable sts span 步骤: 1- pom.xml 2- yml配置: zk: 3- 配置类 4- 使用 5- 调用demo springboot2整合zookeeper集成curator 标签:out art off getchild cep lse runnable sts span 原文地址:https://www.cnblogs.com/domi22/p/9748083.html
url: 127.0.0.1:2181
localPath: /newlock
timeout: 3000package com.test.domi.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 ZookeeperConf {
@Value("${zk.url}")
private String zkUrl;
@Bean
public CuratorFramework getCuratorFramework(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zkUrl,retryPolicy);
client.start();
return client;
}
}
package com.test.domi.common.utils.lock;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
@Component("zklock")
public class ZKlock implements Lock {
@Autowired
private CuratorFramework zkClient;
@Value("${zk.localPath}")
private String lockPath;
private String currentPath;
private String beforePath;
@Override
public boolean tryLock() {
try {
//根节点的初始化放在构造函数里面不生效
if (zkClient.checkExists().forPath(lockPath) == null) {
System.out.println("初始化根节点==========>" + lockPath);
zkClient.create().creatingParentsIfNeeded().forPath(lockPath);
}
System.out.println("当前线程" + Thread.currentThread().getName() + "初始化根节点" + lockPath);
} catch (Exception e) {
}
if (currentPath == null) {
try {
currentPath = this.zkClient.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath(lockPath + "/");
} catch (Exception e) {
return false;
}
}
try {
//此处该如何获取所有的临时节点呢?如locks00004.而不是获取/locks/order中的order作为子节点??
List
package com.test.domi.controller;
import com.test.domi.common.utils.ZkUtil;
import com.test.domi.common.utils.lock.ZKlock;
import org.I0Itec.zkclient.ZkClient;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/zk")
public class ZKController {
@Autowired
private CuratorFramework zkClient;
// @Autowired
// private ZkClient zkClient;
private String url = "127.0.0.1:2181";
private int timeout = 3000;
private String lockPath = "/testl";
@Autowired
private ZKlock zklock;
private int k = 1;
@GetMapping("/lock")
public Boolean getLock() throws Exception{
for (int i = 0; i ) {
new Thread(new Runnable() {
@Override
public void run() {
zklock.lock(); zklock.unlock();
} }).start(); }
return true; } }
文章标题:springboot2整合zookeeper集成curator
文章链接:http://soscw.com/index.php/essay/86488.html