zoukankan      html  css  js  c++  java
  • 老王讲自制RPC框架.(三.ZOOKEEPER)

    (#)定义
    Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群
    管理、分布式应用配置项的管理

    (#)使用
    配置zk客户端我就不多说了,这个网上一找一堆,我主要说一下就是,自己玩玩的话,没必要搭建一个集群,用一台基本问题不大,在此假设集群已经搭建完成了
    需要引入zk client的jar包

    (#)code

      private Logger logger = LoggerFactory.getLogger(ServiceRepository.class);
        @Value("${session.timeout}")
        private int SESSION_TIMEOUT;
        @Value("${registry.address}")
        private String CONNECT_STRING;
        @Value("${repository.path}")
        private String RPC_PATH;
        private ZooKeeper zooKeeper = null;
        private CountDownLatch latch = new CountDownLatch(1);
    
    
    
        private void createConnect() {
            releaseConnection();
            try {
                zooKeeper = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT,
                        new Watcher() {
                            public void process(WatchedEvent event) {
                                logger.info("receive message :{}" + event.getState());
                                latch.countDown();
                            }
                        });
                latch.await();
            } catch (IOException e) {
                logger.error("create异常信息:{}", e);
            } catch (InterruptedException e) {
                logger.error("create异常信息:{}", e);
            }
            ;
        }
    
        private void releaseConnection() {
            if (zooKeeper != null) {
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                    logger.error("release异常信息:{}", e);
                }
            }
        }
    
        private boolean createPath(String path, String data) {
            try {
                zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException e) {
                logger.error("create异常信息:{}", e);
            } catch (InterruptedException e) {
                logger.error("create异常信息:{}", e);
            }
            return true;
        }
    
        public String readData(String path) {
            try {
                return new String(zooKeeper.getData(path, false, null));
            } catch (KeeperException e) {
                logger.error("read异常信息:{}", e);
                return "";
            } catch (InterruptedException e) {
                logger.error("read异常信息:{}", e);
                return "";
            }
        }
    
        public boolean writeData(String path, String data) {
            try {
                zooKeeper.setData(path, data.getBytes(), -1);
            } catch (KeeperException e) {
                logger.error("write异常信息:{}", e);
            } catch (InterruptedException e) {
                logger.error("write异常信息:{}", e);
            }
            return false;
        }
    
        public void deleteNode(String path) {
            try {
                zooKeeper.delete(path, -1);
            } catch (KeeperException e) {
                logger.error("delete异常信息:{}", e);
            } catch (InterruptedException e) {
                logger.error("delete异常信息:{}", e);
            }
        }
    }
    

      

  • 相关阅读:
    罗尔定理、微分中值定理、广义微分中值定理
    高等数学和数学分析教材推荐及其学习方法浅谈
    音视频下载插件 安装及使用
    win10台式机rtl8188eu(FW 150 UM V2.0)无线网卡无法连接wifi(无法连接到这个网络)
    django模板中的extends和include使用方法
    wordpress中文目录出现“有点尴尬诶!该页无法显示"
    wordpress迁移后登陆时出现Forbidden You don’t have permission to access /wp-login.php on this server
    centos设置开机自启动脚本
    hexo的jacman主题设置语言为英文后偶尔出现中文
    安卓QQ聊天记录导出、备份完全攻略
  • 原文地址:https://www.cnblogs.com/wscit/p/6055485.html
Copyright © 2011-2022 走看看