1. 背景
2. 学习
分布式理论:
设计分布式系统的时候需要遵守的理论和算法
重要的理论:CAP、BASE理论
分布式一致性的算法:Paxos,ZAB,Paxos是理论算法,ZAB苏娜发事Paxos的工业级实现
zk 命名空间和文件系统命名类似,每个节点都可以存储数据,但是数据不要超过1M,建议不要用节点来存储数据,存储大数据的话,leader节点和flollwer节点、observer节点同步数据,耗费性能
临时节点的生命周期和客户端会话绑定,如果客户端会话失效,这个临时节点会被清除,是会话失效而不是链接断开,所以所产生的临时节点不是一下子就消失,要过一段时间,大概10s
2.1 zookeeper两种客户端的创建方式
/**
* @author dingyawu
*/
public class ClientFactory {
public static CuratorFramework createSimple(String connectionString) {
long stime = System.currentTimeMillis();
// 重试策略:第一次重试等待1s,第二次重试等待2s,第三次重试等待4s
// 第一个参数:等待时间的基础单位,单位为毫秒
// 第二个参数:最大重试次数
ExponentialBackoffRetry retryPolicy =
new ExponentialBackoffRetry(1000, 3);
// 获取 CuratorFramework 实例的最简单的方式
// 第一个参数:zk的连接地址
// 第二个参数:重试策略
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
Logger.info("创建连接耗费时间ms:"+ (System.currentTimeMillis()-stime));
return client;
}
/**
* @param connectionString zk的连接地址
* @param retryPolicy 重试策略
* @param connectionTimeoutMs 连接
* @param sessionTimeoutMs
* @return CuratorFramework 实例
*/
public static CuratorFramework createWithOptions(
String connectionString, RetryPolicy retryPolicy,
int connectionTimeoutMs, int sessionTimeoutMs) {
// builder 模式创建 CuratorFramework 实例
return CuratorFrameworkFactory.builder()
.connectString(connectionString)
.retryPolicy(retryPolicy)
.connectionTimeoutMs(connectionTimeoutMs)
.sessionTimeoutMs(sessionTimeoutMs)
// 其他的创建选项
.build();
}
}
2.2 curator 测试 zookeeper API
/**
* @Author: dingyawu
* @Description: TODO
* @Date: Created in 10:28 2023/3/25
*/
@Slf4j
public class CRUD {
private static final String ZK_ADDRESS = "1.117.109.40:2181,1.117.109.40:2182,1.117.109.40:2183";
/**
* 检查节点
*/
@Test
public void checkNode() {
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String zkPath = "/test/CRUD/ding-10000000001";
long start = System.currentTimeMillis();
Stat stat = client.checkExists().forPath(zkPath);
if (null == stat) {
log.info("节点不存在:{}", zkPath);
} else {
log.info("节点存在 stat is:{}", stat.toString());
}
log.info("forPath耗费时间ms:{}", (System.currentTimeMillis()-start));
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
/**
* 创建节点
*/
@Test
public void createNode() {
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String data = "hello";
byte[] payload = data.getBytes(StandardCharsets.UTF_8);
String zkPath = "/test/CRUD/remoteNode-1";
long start = System.currentTimeMillis();
client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(zkPath, payload);
log.info("forPath耗费时间ms:{}", (System.currentTimeMillis()-start));
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
@Test
public void testmod() {
for (int i = 0; i < 128; i++) {
int id=1000+i;
System.out.print("devId = " + id);
System.out.print(" devId%2 = " + id%2 );
System.out.print(" devId/2%16 = " + id/2%32 );
System.out.println(" " );
}
}
/**
* 创建 临时 节点
*/
@Test
public void createEphemeralNode() {
//创建客户端
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String data = "hello";
byte[] payload = data.getBytes(StandardCharsets.UTF_8);
String zkPath = "/test/remoteNode-3";
String path = client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.forPath(zkPath, payload);
log.info("create back path:{}", path);
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
/**
* 创建 持久化 顺序 节点
*/
@Test
public void createPersistentSeqNode() {
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String data = "hulan";
for (int i = 0; i < 10; i++) {
byte[] payload = data.getBytes(StandardCharsets.UTF_8);
String zkPath = "/test/CRUD/ding-" + i;
client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath(zkPath, payload);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
/**
* 读取节点
*/
@Test
public void readNode() {
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String zkPath = "/test/CRUD/ding-10000000001";
Stat stat = client.checkExists().forPath(zkPath);
if (null == stat) {
log.info("节点不存在:{}", zkPath);
}
//读取节点的数据
byte[] payload = client.getData().forPath(zkPath);
String data = new String(payload, StandardCharsets.UTF_8);
log.info("read data:{}", data);
String parentPath = "/test";
List<String> children = client.getChildren().forPath(parentPath);
for (String child : children) {
log.info("child:{}", JSON.toJSONString(child));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
public String readSingleNode(CuratorFramework client, String zkPath) {
try {
Stat stat = client.checkExists().forPath(zkPath);
if (null != stat) {
byte[] payload = client.getData().forPath(zkPath);
return new String(payload, StandardCharsets.UTF_8);
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 更新节点
*/
@Test
public void updateNode() {
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String data = "hello world";
byte[] payload = data.getBytes(StandardCharsets.UTF_8);
String zkPath = "/test/CRUD/ding-20000000002";
String result = readSingleNode(client, zkPath);
if (!StringUtils.isEmpty(result)){
client.setData().forPath(zkPath, payload);
}
String updateResult = readSingleNode(client, zkPath);
System.out.println("updateResult:" + updateResult);
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
/**
* 更新节点 - 异步模式
*/
@Test
public void updateNodeAsync() {
CountDownLatch countDownLatch = new CountDownLatch(1);
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
BackgroundCallback callback = (curatorFramework, curatorEvent) -> {
log.info("curatorEvent:{}",curatorEvent.toString() );
countDownLatch.countDown();
};
client.start();
String data = "hello ,every body! ";
byte[] payload = data.getBytes(StandardCharsets.UTF_8);
String zkPath = "/test/CRUD/ding-20000000002";
Stat stat = client.setData()
.inBackground(callback)
.forPath(zkPath, payload);
countDownLatch.await();
log.info("stat:{}", stat);
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
/**
* 删除节点
*/
@Test
public void deleteNode() {
CuratorFramework client = ClientFactory.createSimple(ZK_ADDRESS);
try {
client.start();
String zkPath = "/test/CRUD/remoteNode-1";
client.delete().forPath(zkPath);
String parentPath = "/test";
List<String> children = client.getChildren().forPath(parentPath);
for (String child : children) {
log.info("child:{}", child);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
}
2.3 mac安装PrettyZoo
安装地址: github.com/vran-dev/Pr…
通过终端命令打开mac的安全策略,如下:
# sudo是执行root有的权限命令,spctl(system policy control)即系统策略控制
# 当系统需要安装非信任的第三方软件时,需要把这个管控解除
# master表示管理员,disable表示禁用,enable表示启用
zhouyan@MacBook-Pro ~ % sudo spctl --master-disable
# 输入系统密码
Password:
# 再次执行上面的命令
zhouyan@MacBook-Pro ~ % sudo spctl --master-disable
zhouyan@MacBook-Pro ~ %
我参考的地址: blog.csdn.net/u010355502/…