【Zookeeper教程】2.Zookeeper数据模型以及与Java整合

Zookeeper数据模型

Zookeeper的数据模型是基于树节点(Znode)key/value形式。

节点存储类型:

1.临时

2.临时顺序

3.持久

4.持久顺序

节点操作API:

create - 在树形结构的位置中创建节点

delete - 删除一个节点

exists - 测试节点在指定位置上是否存在

get data - 从节点上读取数据

set data - 往节点写入输入

get chilren - 检索一个节点的子节点列表

sync - 等待传输数据

本文将采用curator+ zookeeper +springboot 来整合,实现zookeeper节点新增、查看例子。

curator简介

curator是Netflix公司开源的zookeeper java客户端,原生zookeeper api对代码不太友好,curator在原生zookeeper api封装,方便使用。

简单版本Java示例

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot 单元测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
		<!-- zookeeper start -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>4.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>4.0.1</version>
		</dependency>
		<!-- zookeeper end -->

Java版示例

@Test
public void test() throws Exception {
    // 客户端连接
    CuratorFramework client = CuratorFrameworkFactory.builder()
        //连接地址  集群用,隔开
        .connectString("192.168.126.156:2181")
        .connectionTimeoutMs(50000)
        //会话超时时间
        .sessionTimeoutMs(50000)
        //设置重试机制
        .retryPolicy(new ExponentialBackoffRetry(1000, 3))
        //设置命名空间 在操作节点的时候,会以这个为父节点
        .namespace("terry")
        .build();
    // 开启连接
    client.start();
    // 1、创建节点:默认存储当前客户端的ip地址
    String path = client.create().forPath("/user");
    // 2.查询节点内容
    byte[] bytes = client.getData().forPath("/user");

    System.out.println("查询节点内容:" + new String(bytes));
}

客户端打印输出

查询节点内容:192.168.126.1

使用Zookeeper图像客户端可以查看到数据

image-20220517105919058

与SpringBoot集成

pom.xml 使用的依赖跟java示例一样。

application.yml

zookeeper:
  curator:
    ip: 192.168.126.156:2181
    sessionTimeOut: 50000
    sleepMsBetweenRetry: 1000
    maxRetries: 3
    namespace: terry
    connectionTimeoutMs: 50000

创建ZookeeperConfig配置类

import lombok.Data;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "zookeeper.curator")
@Data
public class ZookeeperConfig {

   /**
    * 集群地址
    */
   private String ip;

   /**
    * 连接超时时间
    */
   private Integer connectionTimeoutMs;
   /**
    * 会话超时时间
    */
   private Integer sessionTimeOut;

   /**
    * 重试机制时间参数
    */
   private Integer sleepMsBetweenRetry;

   /**
    * 重试机制重试次数
    */
   private Integer maxRetries;

   /**
    * 命名空间(父节点名称)
    */
   private String namespace;

   /**
    * curator 客户端
    * @return
    * @throws Exception
    */
   @Bean("curatorClient")
   public CuratorFramework curatorClient() throws Exception {
      // 开启客户端,并连接
      CuratorFramework client = CuratorFrameworkFactory.builder()
         //连接地址  集群用,隔开
         .connectString(ip)
         .connectionTimeoutMs(connectionTimeoutMs)
         //会话超时时间
         .sessionTimeoutMs(sessionTimeOut)
         //设置重试机制
         .retryPolicy(new ExponentialBackoffRetry(sleepMsBetweenRetry, maxRetries))
         //设置命名空间 在操作节点的时候,会以这个为父节点
         .namespace(namespace)
         .build();
      client.start();
      return client;
   }
}

创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * zookeeper 客户端
 * @author terry
 * @version 1.0
 * @date 2022/4/18 11:38
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

创建测试客户端

import com.terry.App;
import lombok.extern.java.Log;
import org.apache.curator.framework.CuratorFramework;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * zookeeper 客户端测试
 * @author terry
 * @version 1.0
 * @date 2022/4/25 23:01
 */
@SpringBootTest(classes = App.class)
@Log
public class Client {

    @Autowired
    private CuratorFramework client;

    @Test
    public void test() throws Exception {
        // 1、创建节点:默认存储当前客户端的ip地址
        String path = client.create().forPath("/user");
        // 2.查询节点内容
        byte[] bytes = client.getData().forPath("/user");

        System.out.println("查询节点内容:" + new String(bytes));
    }
}

打印输出

查询节点内容:192.168.126.1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

terrybg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值