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

中英网站怎么做国际物流网站制作模板

中英网站怎么做,国际物流网站制作模板,物业公司网站模板,wordpress 语法ZooKeeper提供了多种功能#xff0c;包括分布式锁、配置管理、服务发现、领导选举等。 下面是一些常见的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) {// 获取锁节点下的所有子节点ListString 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 ListString 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 ListString getServices() throws KeeperException, InterruptedException {// 获取服务节点的子节点列表并等待服务更新ListString 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 {// 当服务节点的子节点发生变化时获取最新的服务列表并唤醒等待线程ListString children zooKeeper.getChildren(SERVICE_PATH, true);currentServices children;serviceSignal.countDown();} catch (KeeperException | InterruptedException e) {e.printStackTrace();}}} } 以上是对示例代码的详细注释希望能够帮助您理解代码的功能和使用方式。
http://www.yutouwan.com/news/439386/

相关文章:

  • 深圳微商城网站设计制作wordpress js弹窗
  • 外贸网站哪个比较好登录可见wordpress
  • 东营刚刚发生青山seo排名公司
  • 网站网页建设一般多少钱阿里免费做网站
  • 互联网电子商务网站开发技术电商网站建设与维护意味着什么
  • 做外贸网站效果好吗建设银行网络平台
  • 网页设计与制作课程相关信息北京百度seo公司
  • 网站栏目名称大全网站策划书背景介绍
  • 装修设计图网站排名wordpress移动端底部添加菜单
  • 长春火车站停车场24小时收费标准建新网站开发流程图
  • 怎样写企业网站建设方案微信分销平台排行
  • 莱芜一中官网宁波网站建设优化
  • 单位网站查询工资链接怎么做中科建声公司简介
  • 免费手机网站建站古典网站建设
  • 品牌网站建设 蝌蚪5小免费大数据查询
  • 延平网站建设wzjseo长春建站怎么做
  • 南京app定制公司优化电池充电是关闭还是打开好
  • 腾冲住房和城乡建设局网站wordpress禁止google
  • 罗源城乡建设网站宜宾移动网站建设
  • 百度收录网站中文称asp网站页面设计
  • 免费ppt模板 网站开发西安网站建设设计的好公司哪家好
  • oa系统的概念网站semseo先做哪个
  • iis 默认网站删除住房和城乡建设部叉车证能用吗
  • 高端网站开发公司代发视频赚钱app
  • 济南cms建站利鑫做彩票网站
  • 如何做网站在网上销售ps做网站登陆界面
  • 湖北建设企业网站价格哈尔滨一个好网站建设
  • 企业营销型网站建设公司wap浏览器下载
  • 做地方门户网站赚钱吗aitt网站建设中
  • 林河西网站建设理发美发培训学校