首页 > 其他分享 >zookeeper

zookeeper

时间:2024-02-01 21:22:17浏览次数:28  
标签:void zookeeper forPath public client path 节点

zookeeper

学习内容

  • 简介:

    • 版本:3.8.3
    • 位置:云服务器,
    • 必要配置:jdk
  • 安装及使用

  • 客户端连接

    • 通过zookeeper提供的客户端进行连接

      • 进入bin目录,打开zkCli.sh即可连接

        ./zkCli.sh [-server ip:port] 不加ip和端口,默认连接localhost:2181
        
      • 常用命令

        ls path 查看路径下的节点信息
        create path/node_name 在路径下创建节点
        get path/node_name  查看节点的信息
        set path/node_name  修改节点保存的信息
        delete path/node_name 删除节点
        deleteall path/node_name 删除节点及其子节点
        help 帮助查看一些命令
        
  • 通过JavaAPI连接(借助于Curator框架)

    • 官网:Welcome to Apache Curator | Apache Curator

    • 前置准备

      • 导入坐标

        	<dependencies>
                <dependency>
                    <groupId>org.apache.curator</groupId>
                    <artifactId>curator-framework</artifactId>
                    <version>5.1.0</version>
                </dependency>
        
                <dependency>
                    <groupId>org.apache.curator</groupId>
                    <artifactId>curator-recipes</artifactId>
                    <version>5.1.0</version>
                </dependency>
            </dependencies>
        
    • 常用方法

      /**
       * @author strind
       * @version 1.0
       * @description curator 测试
       * @date 2024/2/1 12:04
       */
      public class CuratorTest {
      
          CuratorFramework client;
      
          @Before
          @Test
          public void testConnect(){
              // 第一种方式
              CuratorFramework client = CuratorFrameworkFactory.newClient(
                  "ip:port", // 连接地址
                  new ExponentialBackoffRetry(3 * 1000, 4) // 重试策略,每间隔3s,共4次
              );
      
              // 第二种方式
              client = CuratorFrameworkFactory.builder()
                  .connectString("ip:port")
                  .sessionTimeoutMs(60 * 1000) // 会话超时时间,单位ms
                  .connectionTimeoutMs(15 * 1000) // 连接超时时间 , 单位ms
                  .namespace("temp") // 命名空间,理解为一个节点,之后创建的节点均在该节点下(此节点会自动创建)
                  .retryPolicy(new ExponentialBackoffRetry(3 * 1000, 4))
                  .build();
              client.start();
      
          }
      
          /**
           * 创建节点 持久 临时 顺序 顺序临时
           */
          @Test
          public void testCreate() throws Exception {
              // 持久
              
              // 没指定数据,会自动保存ip
              String path = client.create().forPath("/app");
              // 指定数据
              String path = client.create().forPath("/app2", "hello world".getBytes());
      
              // 临时(在连接断开后销毁节点)
              String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app4");
      
              // 顺序(持久)
              client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/app5");
              // 顺序(临时)
              client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/app6");
      
      
              // 多级节点 creatingParentsIfNeeded()会在父节点不存在时创建父节点
              client.create().creatingParentsIfNeeded().forPath("/app7/node1/node2");
              System.out.println(path);
      
          }
      
      
          /**
           * 查询
           */
          @Test
          public void testGet() throws Exception {
              // 数据
              byte[] bytes = client.getData().forPath("/app");
              System.out.println(new String(bytes));
              // 子节点
              List<String> children = client.getChildren().forPath("/");
              System.out.println(children);
      
              // 节点状态信息
              Stat stat_info = new Stat();
              client.getData().storingStatIn(stat_info).forPath("/app");
              System.out.println(stat_info);
          }
      
          /**
           * 修改
           */
          @Test
          public void testSet() throws Exception {
              // 数据
              client.setData().forPath("/app","data changed".getBytes());
      
              // 根据版本号修改 (防止其他线程或客户端对同一数据在同一时间进行修改)
              Stat stat_info = new Stat();
              client.getData().storingStatIn(stat_info).forPath("/app");
              int version = stat_info.getVersion();
              client.setData().withVersion(version).forPath("/app","data changed again".getBytes());
          }
      
      
          /**
           * 删除
           */
          @Test
          public void testDelete() throws Exception {
              // 单个节点(有子节点的情况下,删除失败)
              client.delete().forPath("/app");
      
              // 包括子节点
              client.delete().deletingChildrenIfNeeded().forPath("/app7");
      
               // 保证必删除成功(网络抖动,超时等引起失败)
              client.delete().guaranteed().forPath("/app2");
      
              // 带回调函数
              client.delete().inBackground((client, event) -> System.out.println(event)).forPath("/app50000000003");
          }
      
          /**
           * 断开连接,释放资源
           */
          @Test
          public void testClose(){
              if (!Objects.isNull(client)){
                  client.close();
              }
          }
      
          /**
           * Watch 事件监听
           */
          @Test
          public void testWatcher() throws Exception {
              CuratorCache curatorCache = CuratorCache.builder(client,"/app").build();
      
              // 监听当前节点及其子节点
              curatorCache.listenable().addListener(new CuratorCacheListener() {
                  @Override
                  public void event(Type type, ChildData oldData, ChildData data) {
                      System.out.println("changed  +  " + type);
                  }
              });
              curatorCache.start();
              while (true){
      
              }
          }
      }
      

算题记录

LCP 24. 数字游戏 - 力扣(LeetCode)

收获:

​ 将一个数组中的数字变成同一个数,要使操作数最少,则最后变成的数字使这个数组的中位数;(操作为对当前数进行加一减一)

​ 求从0~i(i 属于数组长度范围内)这个范围内的数组的中位数

  • 借助于两个优先队列,一个存放较小者(lower),是大顶堆,一个存放较大者(upper),为小顶堆
  • 大顶堆的任意数均小于较大者,且upper.lengeh<=lower.length<= upper.length + 1
  • 奇数个,中位数便是lower的最大值(堆顶)

​ 2024/2/1 strind

标签:void,zookeeper,forPath,public,client,path,节点
From: https://www.cnblogs.com/strind/p/18002154

相关文章

  • zookeeper源码(07)leader、follower和observer
    Leader构造方法publicLeader(QuorumPeerself,LeaderZooKeeperServerzk)throwsIOException{this.self=self;this.proposalStats=newBufferStats();//获取节点间通信地址Set<InetSocketAddress>addresses;if(self.getQuorumListenOnAllI......
  • ZooKeeper's atomic broadcast protocol:Theory and practice 翻译
    ZooKeeper’satomicbroadcastprotocol:TheoryandpracticeZooKeeper的原子广播协议:理论和实践Andr´eMedeirosMarch20,2012Abstract摘要ApacheZooKeeperisadistributedcoordinationserviceforcloudcomputing,providingessentialsynchronizationandgrou......
  • 深入理解ZooKeeper分布式锁
    第1章:引言分布式系统,简单来说,就是由多台计算机通过网络相连,共同完成任务的系统。想象一下,咱们平时上网浏览网页、看视频,背后其实都是一大堆服务器在协同工作。这些服务器之间需要协调一致,保证数据的一致性和完整性,这就是分布式系统的挑战之一。在这种环境下,锁就显得尤为重要了......
  • Kafka 与 Zookeeper 关系
    Zookeeper为Kafka提供集群的管理。不仅保存着集群的Broker、Topic、Partition等元数据,还负责Broker故障发现、Leader选举、负载均衡等。Broker元数据管理在Zookeeper上会有一个专门用来进行Broker服务器列表记录的节点。每个Broker在启动时,都会到Zookeeper上......
  • Zookeeper集群 +Kafka集群 之(Kafka集群)
    Kafka集群 消息队列(中间件)类型与特性 #Kafka概述#为什么需要消息队列(MQ)主要原因是由于在高并发环境下,同步请求来不及处理,请求往往会发生阻塞。比如大量的请求并发访问数据库,导致行锁表锁,最后请求线程会堆积过多,从而触发toomanyconnection错误,引发雪崩效应。我......
  • Zookeeper集群 +Kafka集群 之 (Zookeeper集群)
    Zookeeper集群+Kafka集群 Zookeeper#Zookeeper定义Zookeeper是一个开源的分布式的,为分布式框架提供协调服务的Apache项目。#Zookeeper工作机制Zookeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受......
  • zookeeper源码(06)ZooKeeperServer及子类
    ZooKeeperServer实现了单机版zookeeper服务端功能,子类实现了更加丰富的分布式集群功能:ZooKeeperServer|--QuorumZooKeeperServer|--LeaderZooKeeperServer|--LearnerZooKeeperServer|--FollowerZooKeeperServer|--ObserverZooKeeperServer|-......
  • zookeeper
    一、概述特点一个leader多个fllower组成的集群集群中只要有半数以上节点存货,zk集群就能正常服务。所以zk适合安装奇数台服务器全局数据一致性,每隔server保存一份相同的数据副本,client无论连接到哪个Server,数据都是一致的跟新请求顺序执行,来自同一个client的更新请求按其发送......
  • hyperf 3.1安装和配置php-zookeeper扩展
    Hyperf提供了分布式系统的外部化配置支持,默认适配了:由携程开源的 ctripcorp/apollo,由 hyperf/config-apollo 组件提供功能支持。阿里云提供的免费配置中心服务 应用配置管理(ACM,ApplicationConfigManager),由 hyperf/config-aliyun-acm 组件提供功能支持。ETCDNac......
  • 移动护理系统-ZooKeeper单机和集群方式安装部署
    1.1zookeeper安装1.下载地址http://mirror.bit.edu.cn/apache/zookeeper/选择版本,我选择的是注意:下载新版本的时候需要下载带bin的,因为有时候会报错错误:找不到或无法加载主类org.apache.zookeeper.server.quorum.QuorumPeerMain你下载使用的zk是未编译的apache-zookeep......