当前位置: 首页 > news >正文

玉林做绿化苗木网站的是哪个单位长沙网站seo技术厂家

玉林做绿化苗木网站的是哪个单位,长沙网站seo技术厂家,网站建设业务经理岗位职责,网站建设对企业的重要性ZooKeeper提供了多种功能,包括分布式锁、配置管理、服务发现、领导选举等。 下面是一些常见的ZooKeeper功能及其在Java中的应用示例代码。 分布式锁 import org.apache.zookeeper.*; import java.io.IOException; import java.util.concurrent.CountDownLatch;pu…

ZooKeeper提供了多种功能,包括分布式锁、配置管理、服务发现、领导选举等。

下面是一些常见的ZooKeeper功能及其在Java中的应用示例代码。

分布式锁

import org.apache.zookeeper.*;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;public class DistributedLock implements Watcher {private static final String ZOOKEEPER_ADDRESS = "localhost:2181";private static final int SESSION_TIMEOUT = 5000;private static final String LOCK_PATH = "/distributed-lock";private ZooKeeper zooKeeper;private String currentLockPath;private CountDownLatch lockSignal;public DistributedLock() throws IOException, InterruptedException, KeeperException {// 创建ZooKeeper对象,建立与ZooKeeper服务器的连接zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);lockSignal = new CountDownLatch(1);// 确保锁的根节点存在ensurePathExists(LOCK_PATH);}public void lock() throws KeeperException, InterruptedException {// 创建临时顺序节点作为锁节点String lockNodePath = zooKeeper.create(LOCK_PATH + "/lock-", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);while (true) {// 获取锁节点下的所有子节点List<String> children = zooKeeper.getChildren(LOCK_PATH, false);Collections.sort(children);// 获取当前锁节点在所有子节点中的位置int index = children.indexOf(lockNodePath.substring(LOCK_PATH.length() + 1));if (index == 0) {// 如果当前锁节点是第一个节点,则获取到了锁this.currentLockPath = lockNodePath;return;} else {// 如果当前锁节点不是第一个节点,则监听前一个节点的删除事件,然后等待String previousLockPath = LOCK_PATH + "/" + children.get(index - 1);zooKeeper.exists(previousLockPath, true);lockSignal.await();}}}public void unlock() throws KeeperException, InterruptedException {// 删除当前锁节点zooKeeper.delete(currentLockPath, -1);currentLockPath = null;}private void ensurePathExists(String path) throws KeeperException, InterruptedException {// 确保路径存在,如果不存在则创建持久节点if (zooKeeper.exists(path, false) == null) {zooKeeper.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}}@Overridepublic void process(WatchedEvent watchedEvent) {if (watchedEvent.getType() == Event.EventType.NodeDeleted && watchedEvent.getPath().equals(currentLockPath)) {// 当前锁节点被删除时,唤醒等待线程lockSignal.countDown();}}
}

配置管理

import org.apache.zookeeper.*;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;public class ConfigManager implements Watcher {private static final String ZOOKEEPER_ADDRESS = "localhost:2181";private static final int SESSION_TIMEOUT = 5000;private static final String CONFIG_PATH = "/config";private ZooKeeper zooKeeper;private CountDownLatch configSignal;private String currentConfig;public ConfigManager() throws IOException, InterruptedException, KeeperException {// 创建ZooKeeper对象,建立与ZooKeeper服务器的连接zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);configSignal = new CountDownLatch(1);// 确保配置节点存在ensurePathExists(CONFIG_PATH);}public String getConfig() throws KeeperException, InterruptedException {// 获取配置节点的数据,并等待配置更新byte[] data = zooKeeper.getData(CONFIG_PATH, true, null);configSignal.await();return new String(data);}private void ensurePathExists(String path) throws KeeperException, InterruptedException {// 确保路径存在,如果不存在则创建持久节点if (zooKeeper.exists(path, false) == null) {zooKeeper.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}}@Overridepublic void process(WatchedEvent watchedEvent) {if (watchedEvent.getType() == Event.EventType.NodeDataChanged && watchedEvent.getPath().equals(CONFIG_PATH)) {try {// 当配置节点数据发生变化时,获取最新的配置数据,并唤醒等待线程byte[] data = zooKeeper.getData(CONFIG_PATH, true, null);currentConfig = new String(data);configSignal.countDown();} catch (KeeperException | InterruptedException e) {e.printStackTrace();}}}
}

服务发现

import org.apache.zookeeper.*;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;public class ServiceDiscovery implements Watcher {private static final String ZOOKEEPER_ADDRESS = "localhost:2181";private static final int SESSION_TIMEOUT = 5000;private static final String SERVICE_PATH = "/services";private ZooKeeper zooKeeper;private CountDownLatch serviceSignal;private List<String> currentServices;public ServiceDiscovery() throws IOException, InterruptedException, KeeperException {// 创建ZooKeeper对象,建立与ZooKeeper服务器的连接zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);serviceSignal = new CountDownLatch(1);// 确保服务节点存在ensurePathExists(SERVICE_PATH);}public List<String> getServices() throws KeeperException, InterruptedException {// 获取服务节点的子节点列表,并等待服务更新List<String> children = zooKeeper.getChildren(SERVICE_PATH, true);serviceSignal.await();return currentServices;}private void ensurePathExists(String path) throws KeeperException, InterruptedException {// 确保路径存在,如果不存在则创建持久节点if (zooKeeper.exists(path, false) == null) {zooKeeper.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}}@Overridepublic void process(WatchedEvent watchedEvent) {if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged && watchedEvent.getPath().equals(SERVICE_PATH)) {try {// 当服务节点的子节点发生变化时,获取最新的服务列表,并唤醒等待线程List<String> children = zooKeeper.getChildren(SERVICE_PATH, true);currentServices = children;serviceSignal.countDown();} catch (KeeperException | InterruptedException e) {e.printStackTrace();}}}
}

以上是对示例代码的详细注释,希望能够帮助您理解代码的功能和使用方式。

http://www.khdw.cn/news/16528.html

相关文章:

  • 网站推广新手入门教程企业qq
  • 网站的做用百度竞价推广效果怎么样
  • 包头建站营销网络是什么意思
  • 单位网站设计制作广告代发平台
  • asp.net c 网站开发网站排名顾问
  • 武汉做网站深圳百度快速排名提升
  • 商业门户网站有哪些关键词排名怎么查
  • 青岛做优化网站哪家好人力资源培训与开发
  • 网站登录按钮点击没反应什么原因免费创建属于自己的网站
  • 南昌网站开发培训班外链工具在线
  • 西红门做网站合肥头条今日头条新闻最新消息
  • 做网站硬件跨境电商seo什么意思
  • qq空间怎么跟网站做链接吗如何在百度上发表文章
  • 网站安全建设目的图片优化网站
  • 上海专业建站公seo兼职平台
  • 收费网站有哪些免费自动推广手机软件
  • 建设工程监理招标网站引流用什么话术更吸引人
  • 网站建设服务价格上海网络公司seo
  • 江西做网站多少钱常见网络营销推广方法
  • 一键制作网站软件正规的计算机培训机构
  • 新乡专业做网站公司如何创建自己的卡网
  • 网站公告怎么做百度app推广方法
  • 定制高端网站建设报价免费发布信息网
  • 建设了网站怎么管理系统产品网络营销方案
  • 什么做自己的网站最新网络营销方式有哪些
  • js网站文字重叠网络整合营销的特点有
  • 深圳网站建设培训机构做百度线上推广
  • 深圳h5模板建站全球搜索引擎排名2021
  • 做企业网站要多长时间站长工具域名解析
  • h网站模板整合营销案例