加载中...

zookeeper

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

posted @   strind  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示