【5】Curator客户端的使用

1、Curator介绍

Curator是Netflix公司开源的一套zookeeper客户端框架,Curator是对Zookeeper支持最好的客户端框架。Curator封装了大部分Zookeeper的功能,比如Leader选举、分布式锁等,减少了技术人员在使用Zookeeper时的底层细节开发工作。

2、引入依赖

新建一个Springboot项目,并引入相关依赖

        <!---Curator-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.3.0</version>
        </dependency>
        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.0</version>
        </dependency>

完整依赖如下:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!---Curator-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.3.0</version>
        </dependency>

        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>

在application.properties中添加配置信息如下:

curator.retryCount=5
curator.elapsedTimeMs=5000
curator.connectionString=自己的服务器ip地址:2181
curator.sessionTimeoutMs=60000
curator.connectionTimeoutMs=4000

(奇怪的是在application.yaml中配置信息会显示无法解析,我还以为其他有问题然后花费了我很多找时间其他错误!,先留个坑,以后再看)

3、编写配置curator配置类

新建文件夹config,并新建WrapperZK类:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "curator")
public class WrapperZK {
    private int retryCount;

    private int elapsedTimeMs;

    private String connectionString;

    private int sessionTimeoutMs;

    private int connectionTimeoutMs;
}

新建CuratorConfig类:

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//引用配置类
@Configuration
public class CuratorConfig {

    @Autowired
    private WrapperZK wrapperZK;

    @Bean(initMethod = "start")
    public CuratorFramework curatorFramework(){
        return CuratorFrameworkFactory.newClient(
                wrapperZK.getConnectionString(),
                wrapperZK.getSessionTimeoutMs(),
                wrapperZK.getConnectionTimeoutMs(),
                new RetryNTimes(wrapperZK.getRetryCount(), wrapperZK.getElapsedTimeMs())
        );
    }
}

4、测试

注意:测试zookeeper时需要把服务器上对应的端口打开,并关闭防火墙!!
在测试类中进行测试如下:

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ZookeeperApplicationTests {

    @Autowired
    private CuratorFramework curatorFramework;

    @Test
    void contextLoads() throws Exception {
        //添加默认(持久)节点
        String path = curatorFramework.create().forPath("/curator-node");
        //添加临时序号节点
        //String path2 = curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curator-nodes", "messageDate".getBytes());
        System.out.println(String.format("curator create node :%s  successfully!", path));
        //		System.in.read();
    }

    //设置节点值
    @Test
    void setDate() throws Exception {
        curatorFramework.setData().forPath("/curator-node", "newMessage".getBytes());
        byte[] bytes = curatorFramework.getData().forPath("/curator-node");
        System.out.println("bytes = " + bytes);
    }

    @Test
    //获取节点值
    void getDate() throws Exception {
        String operatorObjStr = new String(curatorFramework.getData().forPath("/curator-node"));
        System.err.println("bttes = " + operatorObjStr);
    }

    
    @Test
    //创建多级节点
    void createWithParent() throws Exception {
        String pathWithParent = "/node-parent/sub-node-1";
        String path = curatorFramework.create().creatingParentContainersIfNeeded().forPath(pathWithParent);
        System.out.println(String.format("curator create node :%s success!", path));
    }

    @Test
    //删除节点
    void delete() throws Exception {
        String path = "/node-parent";
        //删除节点的同时一并删除子节点
        curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
    }
}

在这里插入图片描述
成功设置值并获取到值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值