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

张店学校网站建设哪家好网络推广的方法你知道几个?

张店学校网站建设哪家好,网络推广的方法你知道几个?,仿站怎么修改成自己的网站,电子元器件采购网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.15wanjia.com/news/29066.html

相关文章:

  • wordpress局域网外网访问不了网络推广运营优化
  • 医院网站实例长沙弧度seo
  • 做哪个网站零售最好免费html网站制作成品
  • 网站建设需要多少人网络营销五种方法
  • app下载安装免费手机优化助手
  • app开发公司天品科技网络推广seo怎么弄
  • wordpress 整站 数据近几天的新闻摘抄
  • 兼职网站建设收费seo外链招聘
  • 戴瑞企业网站建设需求武汉网站设计公司
  • 网站开发调研餐饮营销引流都有什么方法
  • 注册网站后怎么建设表白网站制作
  • 男女做暖暖的网站大全自己怎么做游戏推广赚钱
  • 做网站开发要学多久链接搜索引擎
  • 做个公司网站要多少钱灰色seo推广
  • wordpress政企网站识图搜索在线 照片识别
  • 孝感建设公司网站百度广告公司联系方式
  • 网站建站的尺寸免费seo快速收录工具
  • 定制网站建设托管网站建站教程
  • 汉中专业网站建设开发百度旧版本
  • 新乡做网站智能优化大师下载
  • 网站制作公司的流程同城推广平台
  • php企业网站源码推荐网址最新连接查询
  • 郑州做网站哪里便宜上海关键词优化方法
  • 打代码做网站的软件网络营销策划的基本原则
  • 西安优化网站推广ciliba最佳磁力搜索引擎
  • 聊城网站建设培训班新网站如何推广
  • 网站建设远洋国际软文推广是什么意思
  • 郑州手机网站开发龙泉驿网站seo
  • wordpress如何添加注册登录界面汕头网站建设方案优化
  • 做国内打不开的网站百度一下百度一下百度一下