<?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>com.test</groupId>
<artifactId>train-rpc</artifactId>
<version>0.0.1</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<zookeeper.version>3.4.9</zookeeper.version>
<zkclient.version>0.2-SNAPSHOT</zkclient.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-All</artifactId>
</dependency>
<!-- Msgpack序列化 -->
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-All</artifactId>
<version>4.1.16.Final</version>
<!--<version>4.1.6.Final</version>-->
</dependency>
<!-- Msgpack序列化 -->
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
<version>0.6.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.test.zookeeper.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
/**
* Created by test
* 2020/9/28.
*/
public class CuratorDemo {
private CuratorFramework curatorFramework;
private String curatorRoot = "/curatorRoot";
@Before
public void init() throws Exception {
curatorFramework = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.sessionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
curatorFramework.start();
//curatorFramework.delete().deletingChildrenIfNeeded().forPath(curatorRoot);
String path = curatorFramework.create().creatingParentsIfNeeded().forPath(curatorRoot);
System.out.println("init success,create root node:" + path);
}
@After
public void destroy() {
try {
curatorFramework.delete().deletingChildrenIfNeeded().forPath(curatorRoot);
System.out.println("come destroy,delete all the nodes of " + curatorRoot);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void createPersistentNode() {
try {
String path = curatorFramework.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.forPath(curatorRoot + "/demo1");
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getChildren() {
try {
String path1 = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(curatorRoot + "/c1/c11/c11");
System.out.println("create path1=" + path1);
String path2 = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(curatorRoot + "/c2/c22");
System.out.println("create path2=" + path2);
List<String> strings = curatorFramework.getChildren().forPath(curatorRoot);
System.out.println(strings);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getValue() {
try {
byte[] bytes = curatorFramework.getData().forPath(curatorRoot);
System.out.println(new String(bytes));
//System.out.println(InetAddress.getLocalHost());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void exists() {
try {
Stat stat = curatorFramework.checkExists().forPath(curatorRoot + "/c1");
System.out.println(stat);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void update() {
try {
Stat path = curatorFramework.setData().forPath(curatorRoot, "hello,world".getBytes());
byte[] bytes = curatorFramework.getData().forPath(curatorRoot);
System.out.println(new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testWatch_NodeCache(){
//监控当前节点数据变化 https://blog.csdn.net/sinat_19687693/article/details/50977567
NodeCache nodeCache = new NodeCache(curatorFramework, curatorRoot);
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("node changed of the path"+curatorRoot);
}
});
try {
nodeCache.start();
} catch (Exception e) {
e.printStackTrace();
}
this.update();
}
@Test
public void testWatch_PathChildrenCache(){
//PathChildrenCache --监控当前节点子节点的变化 新增 数据改变 删除 https://blog.csdn.net/sinat_19687693/article/details/50977567
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,curatorRoot,true);
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("---------------------event begin-----------------------------------------");
System.out.println("pathChildrenCacheEvent="+pathChildrenCacheEvent.getType()+" curatorFramework="+curatorFramework);
List<String> list = curatorFramework.getChildren().forPath(curatorRoot);
System.out.println("childNodeList of root is "+list);
System.out.println("---------------------event end-----------------------------------------");
}
});
try {
pathChildrenCache.start();
} catch (Exception e) {
e.printStackTrace();
}
try {
curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(curatorRoot+"/node1/node11");
System.out.println("创建了新节点,观察事件是否触发呢?");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.test.zookeeper.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import java.util.concurrent.CountDownLatch;
/**
* 利用curator 操作zookeeper实现分布式锁
* 2020/9/28.
*/
public class DistributedLock {
private static CuratorFramework curatorFramework;
static {
curatorFramework = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
curatorFramework.start();
}
static String path = "/lock";
public static void main(String[] args) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final InterProcessMutex lock = new InterProcessMutex(curatorFramework, path);
for (int i = 0; i < 30; i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await();
//获得锁
lock.acquire();
System.out.println(Thread.currentThread().getName()+"获得锁,睡1秒后释放锁");
Thread.sleep(1000);
lock.release();
System.out.println(Thread.currentThread().getName()+"已释放锁,其他线程可以过来争抢锁了");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
},"Thread" + i).start();
}
countDownLatch.countDown();
}
}