zookeeper客户端框架-基于Apache Curator框架的ZooKeeper使用详解

一个配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd        
     http://code.alibabatech.com/schema/dubbo        
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="XXXXXX"></dubbo:application>
    <!-- 使用multicast广播注册中心暴露服务地址 -->   
    <dubbo:registry protocol="zookeeper" address="10.xxx.5.158:2181,10.xxx.5.157:2181,10.xxx.5.159:2181" id="XXXXXXRegistry3" client="curator" check="false"/>

    <dubbo:reference id="processDubboService" interface="com.xxxxxx.process.api.ProcessDubboService" check="false"/>
    
    <dubbo:service group="a01" interface="com.xxxxxx.xxxxx.external.service.xxxxxDubboService"  ref="xxxxxDubboService" />
    
    <dubbo:reference id="registryService" interface="com.alibaba.dubbo.registry.RegistryService" check="false" stastics="false"/>
    <!-- 用dubbo协议 -->
    <dubbo:protocol name="dubbo"/>
    <!-- 设置超时时间30秒 ,调用失败不重新调用-->
    <dubbo:consumer timeout="120000" retries="0"/>

    <!-- 提供待遇服务实现 -->
    <import resource="classpath:resource/spring-interfaceToBefit-include.xml"/>

    <!-- 提供受理校验服务实现 -->
    <import resource="classpath:resource/spring-interfaceToAcceptance-include.xml"/>

    <!-- 提供查询服务实现 -->
    <import resource="classpath:resource/spring-xxxxx-include.xml"/>

    <!-- 受理查询接口 -->
    <import resource="classpath:resource/spring/spring-publicDubboAcceptanceInterface.xml"/>

    <!-- UCM统一存储  -->
    <dubbo:reference id="dubboUcmService" interface="com.xxxxxx.dubbo.ucm.service.DubboUcmService" check="false" />

    <!-- xxxx校验接口 -->
    <dubbo:reference group="xx01" id="xxxpublicDubboAcceptance" interface="com.xxxxxx.interfaceToAcceptance.service.PublicDubboAcceptance" check="false"/>

    <!-- xxxx校验接口 -->
    <dubbo:reference group="xx21" id="xxxpublicDubboAcceptance" interface="com.xxxxxx.interfaceToAcceptance.service.PublicDubboAcceptance" check="false"/>

</beans>

其中,发现一个:

client="curator"
 
Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连、反复注册Watcher和NodeExistsException异常等等。
Curator的maven依赖

     <!-- 对zookeeper的底层api的一些封装 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <!-- 封装了一些高级特性,如:Cache事件监听、选举、分布式锁、分布式Barrier -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>

一 简介

Apache Curator是一个比较完善的ZooKeeper客户端框架,通过封装的一套高级API 简化了ZooKeeper的操作。通过查看官方文档,可以发现Curator主要解决了三类问题:

  • 封装ZooKeeper client与ZooKeeper server之间的连接处理
  • 提供了一套Fluent风格的操作API
  • 提供ZooKeeper各种应用场景(recipe, 比如:分布式锁服务、集群领导选举、共享计数器、缓存机制、分布式队列等)的抽象封装

    Curator主要从以下几个方面降低了zk使用的复杂性:

    • 重试机制:提供可插拔的重试机制, 它将给捕获所有可恢复的异常配置一个重试策略,并且内部也提供了几种标准的重试策略(比如指数补偿)
    • 连接状态监控: Curator初始化之后会一直对zk连接进行监听,一旦发现连接状态发生变化将会作出相应的处理
    • zk客户端实例管理:Curator会对zk客户端到server集群的连接进行管理,并在需要的时候重建zk实例,保证与zk集群连接的可靠性
    • 各种使用场景支持:Curator实现了zk支持的大部分使用场景(甚至包括zk自身不支持的场景),这些实现都遵循了zk的最佳实践,并考虑了各种极端情况

基本API

创建会话

使用静态工程方法创建
 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); //1 重试策略:初试时间为1s 重试10次
 CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.128.129:2181", 5000, 5000, retryPolicy);

其中RetryPolicy为重试策略,第一个参数为baseSleepTimeMs初始的sleep时间,用于计算之后的每次重试的sleep时间。第二个参数为maxRetries,最大重试次数。

使用Fluent风格api创建
 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.128.129:2181")
                .sessionTimeoutMs(5000)  // 会话超时时间
                .connectionTimeoutMs(5000) // 连接超时时间
                .retryPolicy(retryPolicy)
                .namespace("base") // 包含隔离名称
                .build();
        client.start();

可以参考:

https://www.cnblogs.com/erbing/p/9799098.html

https://www.jianshu.com/p/db65b64f38aa

原文地址:https://www.cnblogs.com/moonsoft/p/13856735.html