您的位置:首页 > 其它

Zookeeper开源客户端框架Curator的简单使用

2017-01-23 16:20 363 查看

    Curator最初由Netflix的Jordan Zimmerman开发, Curator提供了一套Java类库, 可以更容易的使用ZooKeeper.

    所谓ZooKeeper技巧(ZooKeeper Recipes),也可以称之为解决方案, 或者叫实现方案, 是指ZooKeeper的使用方法, 比如分布式的配置管理, Leader选举等

    Curator作为Apache ZooKeeper天生配套的组件。ZooKeeper的Java开发者自然而然的会选择它在项目中使用。

官网链接:http://curator.apache.org/

 

提供的功能组件

1.Framework 提供了一套高级的API, 简化了ZooKeeper的操作。 它增加了很多使用ZooKeeper开发的特性,可以处理ZooKeeper集群复杂的连接管理和重试机制

 

2.Client 是ZooKeeper客户端的一个替代品, 提供了一些底层处理和相关的工具方法

 

3.Recipes 实现了通用ZooKeeper的recipe, 该组件建立在Framework的基础之上

 

4.Utilities 各种工具类

 

5.Errors 异常处理, 连接, 恢复等.

 

6.Extensions curator-recipes包实现了通用的技巧,这些技巧在ZooKeeper文档中有介绍。为了避免是这个包(package)变得巨大, recipes/applications将会放入一个独立的extension包下。并使用命名规则curator-x-name.

 

Curator 编译好的类库被发布到Maven Center中。Curator包含几个artifact. 你可以根据你的需要在你的项目中加入相应的依赖。对于大多数开发者来说,引入curator-recipes这一个就足够了.

依赖:

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.6.0</version>
</dependency>

 

接下来是我实战中用到一些代码,(需要说明的是这里用到了zookeeper共享锁,按天执行的锁,并不是很完善,通过节点路径来控制)

示例代码:

@Component("userTask")
public class UserTask extends BaseTask {

private static Logger logger = LoggerFactory.getLogger(UserTask .class);

@Autowired
private UserService userService;

@Resource
protected ZooKeeperClient zkClient;

public static String LOCK_NODE = "/data/lockPs" + new DateTime().toString("yyyy-MM-dd");

@Override
public void doTask() {
InterProcessMutex lock = new InterProcessMutex(zkClient.getClient(), "/data/lock");

try {
if (lock.acquire(2000, TimeUnit.MILLISECONDS)) {
List<String> list = zkClient.getChildren(LOCK_NODE);
if (list.size() == 0) {
zkClient.create(LOCK_NODE, CreateMode.PERSISTENT);
//lock.acquire();
logger.info("开始执行任务:" + new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
try {
userService.execute();
} catch (Exception e) {
logger.error("执行任务中抛错",e);
} finally {
logger.info("结束执行任务:" + new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
}
}
lock.release();
}
} catch (Exception e) {
logger.error("zkclient/执行任务抛错:",e);
}
}
}

 

另外需要说明是用到了封装好的zookeeper的客户端,这里在介绍一下,

获取zookeeper的客户端:

//1.方式一
class ZooKeeperClient{
private CuratorFramework client;

public ZooKeeperClient(String zkAddress) {
client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
client.getCuratorListenable().addListener(new NodeEventListener());
client.start();
}
...
}

//2.方式二
class ZooKeeperClient{
private CuratorFramework client;

public ZooKeeperClient(String connectString, int sessionTimeout, String parent) throws Exception {
zkClient = new CuratorZookeeperClient(connectString, sessionTimeout, sessionTimeout, zNodeWatcher, new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
zkClient.start();//must,but anytime before zookeeper operation
zkClient.blockUntilConnectedOrTimedOut(); //first connection should be successful
}
...
}

 

推荐看:

https://yq.aliyun.com/articles/43537

http://shift-alt-ctrl.iteye.com/blog/1981751

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: