zookeeper第一章:集群搭建
zookeeper第二章:API接口
zookeeper第三章:项目案例
zookeeper的基础内容差不多学完了,所以我们来完成几个案例
再之前的项目中,由于依赖的问题,控制台无法正常输出日志文件,现在我们来修复这个问题。
修改pom.xml
<?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.example</groupId>
<artifactId>zk</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.1</version>
</dependency>
//新增依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
之后运行上一个项目的最后一个方法
可以看到日志功能以及恢复。
某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知到主节点服务器的上下线。
创建新项目
DistributeServer.java
package com.atguigu.case1;
import org.apache.zookeeper.*;
import java.io.IOException;
public class DistributeServer {
private String connectString="hadoop103:2181,hadoop103:2181,hadoop104:2181";
private int seesionTimeout=4000;
private ZooKeeper zk;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributeServer server = new DistributeServer();
server.getConnect();
server.regist(args[0]);
server.business();
}
//业务逻辑
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
//客户端注册
private void regist(String hostname) throws InterruptedException, KeeperException {
zk.create("/servers/"+hostname, hostname.getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname + " is online");
}
// 创建到 zk 的客户端连接
private void getConnect() throws IOException {
zk = new ZooKeeper(connectString, seesionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
}
服务端注册需要输入注册信息所以需要输入参数。
这里输入注册的节点名称
一定要应用
DistributeClient.java
package com.atguigu.case1;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DistributeClient {
private String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int seesionTimeout=4000;
private ZooKeeper zk;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributeClient client = new DistributeClient();
client.getConnect();
client.getServerList();
client.business();
}
//业务代码
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
//获取节点信息
private void getServerList() throws InterruptedException, KeeperException {
List<String> children = zk.getChildren("/servers", true);
ArrayList<Object> servers = new ArrayList<>();
for (String child : children) {
byte[] data = zk.getData("/servers/" + child, false, null);
servers.add(new String(data));
}
System.out.println(servers);
}
//创建链接
private void getConnect() throws IOException {
zk = new ZooKeeper(connectString, seesionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
getServerList();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (KeeperException e) {
throw new RuntimeException(e);
}
}
});
}
}
咱们再/servers目录下测试,要先自己创建。
先运行DistributeClient.java
在运行DistributeServer.java
再查看DistributeClient
现在我们再运行DistributeServer,输入换成hadoop103
由于咱们创建的是临时节点,断开后源节点就会消失,可以根据自己需求修改。
什么叫做分布式锁呢?
比如说"进程 1"在使用该资源的时候,会先去获得锁,"进程 1"获得锁以后会对该资源保持独占,这样其他进程就无法访问该资源,"进程 1"用完该资源以后就将锁释放掉,让其他进程来获得锁,那么通过这个锁机制,我们就能保证了分布式系统中多个进程能够有序的访问该临界资源。那么我们把这个分布式环境下的这个锁叫作分布式锁。
DistributedLock.java
package com.atguigu.case2;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class DistributedLock {
private final String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
private final int seesionTimeout=4000;
private final ZooKeeper zk;
private CountDownLatch connectLatch =new CountDownLatch(1);
private CountDownLatch waitLath =new CountDownLatch(1);
private String waitPath;
private String currentMode;
//建立连接
public DistributedLock() throws IOException, InterruptedException, KeeperException {
zk = new ZooKeeper(connectString, seesionTimeout, new Watcher() {
@Override
//监控节点
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
connectLatch.countDown();
}
if (watchedEvent.getType()==Event.EventType.NodeDeleted && watchedEvent.getPath().equals(waitPath)){
waitLath.countDown();
}
}
});
//等待连接建立(成功前阻塞进程)
connectLatch.await();
//检测测试节点/locks
Stat stat = zk.exists("/locks", false);
//若测试节点不存在便创建接待你
if (stat == null) {
zk.create("/locks","locks".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
}
}
//加锁
public void zkLock(){
try {
currentMode = zk.create("/locks/" + "seq-", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
List<String> children = zk.getChildren("/locks", false);
if (children.size() == 1) {
return;
}else {
Collections.sort(children);
String thisNode = currentMode.substring("/locks/".length());
int index = children.indexOf(thisNode);
if (index == -1) {
System.out.println("数据异常");
} else if (index == 0) {
return;
}else {
waitPath="/locks/"+children.get(index-1);
zk.getData(waitPath,true,null);
waitLath.await();
return;
}
}
} catch (KeeperException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//释放锁
public void unzkLock(){
try {
zk.delete(currentMode,-1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (KeeperException e) {
throw new RuntimeException(e);
}
}
}
DistractedLockTest.java
package com.atguigu.case2;
import org.apache.zookeeper.KeeperException;
import java.io.IOException;
public class DistractedLockTest {
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributedLock lock1 = new DistributedLock();
DistributedLock lock2 = new DistributedLock();
//两个线程同时运行
new Thread(new Runnable() {
@Override
public void run() {
try {
lock1.zkLock();
System.out.println("线程1 启动 获取到锁");
Thread.sleep(5000);
lock1.unzkLock();
System.out.println("线程1 释放锁");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
lock2.zkLock();
System.out.println("线程2 启动 获取到锁");
Thread.sleep(5000);
lock2.unzkLock();
System.out.println("线程2 释放锁");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
}
运行DistractedLockTest.java文件.
Curator 是一个专门解决分布式锁的框架,解决了原生 JavaAPI 开发分布式遇到的问题。
代码编写
CuratorLockTest.java
package com.atguigu.case3;
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;
public class CuratorLockTest {
public static void main(String[] args) {
InterProcessMutex lock1 = new InterProcessMutex(getCuratorFramework(), "/locks");
InterProcessMutex lock2 = new InterProcessMutex(getCuratorFramework(), "/locks");
new Thread(new Runnable() {
@Override
public void run() {
try {
lock1.acquire();
System.out.println("线程1 获取到锁");
lock1.acquire();
System.out.println("线程1 再次获取到锁");
Thread.sleep(5000);
lock1.release();
System.out.println("线程1 释放锁");
lock1.release();
System.out.println("线程1 再次释放锁");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
lock2.acquire();
System.out.println("线程2 获取到锁");
lock2.acquire();
System.out.println("线程2 再次获取到锁");
Thread.sleep(5000);
lock2.release();
System.out.println("线程2 释放锁");
lock2.release();
System.out.println("线程1 再次释放锁");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
}
private static CuratorFramework getCuratorFramework() {
ExponentialBackoffRetry policy = new ExponentialBackoffRetry(3000, 3);
String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
int seesionTimeout=4000;
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectString)
.connectionTimeoutMs(seesionTimeout)
.sessionTimeoutMs(seesionTimeout)
.retryPolicy(policy).build();
client.start();
System.out.println("zookeeper启动成功");
return client;
}
}
一些zookeeper的基础案例就分享到这里。