Java原生API操作ZooKeeper可参看:

​​Java原生API操作Zookeeper(一)​​

​​Java原生API操作Zookeeper(二)​​

相关内容:

​​基于Curator操作ZooKeeper(一)-基本操作​​

​​基于Curator操作ZooKeeper(二)-Watcher操作-补充TreeCache​​

​​基于Curator操作ZooKeeper(二)-Watcher操作​​

 

Curator整合Spring和Spring Boot方式都是差不多的。

在Spring项目中引入依赖:

<!-- <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>

</dependency> -->

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

增加applicationContext-xx.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<description>ZK与Spring整合,启动项目时建立与ZK的连接</description>
<!--ZK重试策略-->
<bean id="retryPolicy" class="org.apache.curator.retry.RetryNTimes">
<!--重试次数-->
<constructor-arg index="0" value="10"/>
<!--每次间隔ms-->
<constructor-arg index="1" value="5000"/>
</bean>

<!--ZK客户端-->
<bean id="client" class="org.apache.curator.framework.CuratorFrameworkFactory" factory-method="newClient"
init-method="start">
<!--ZK服务地址,集群使用逗号分隔-->
<constructor-arg index="0" value="192.168.220.136,192.168.220.137"/>
<!--session timeout会话超时时间-->
<constructor-arg index="1" value="10000"/>
<!--ConnectionTimeoutMs创建连接超时时间-->
<constructor-arg index="2" value="5000"/>
<!--重试策略-->
<constructor-arg index="3" ref="retryPolicy"/>
</bean>

<!--扩展注入ZK工具-->
<bean id="zkCurator" class="dongguabai.ZKCurator" init-method="init">
<constructor-arg index="0" ref="client"/>
</bean>
</beans>

ZK客户端:

package dongguabai;

import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Dongguabai
* @date 2018/10/18 15:12
*/
public class ZKCurator {

private static final Logger LOGGER = LoggerFactory.getLogger(ZKCurator.class);

//ZK客户端
private CuratorFramework client = null;

public ZKCurator(CuratorFramework client) {
this.client = client;
}

/**
* 初始化操作
*/
public void init(){
//使用命名空间
client = client.usingNamespace("testDgb");
}

/**
* 判断ZK是否连接
* @return
*/
public boolean isZKAlive(){
return client!=null && client.isStarted();
}
}

测试:

import dongguabai.ZKCurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 两景Controller
* @author Dongguabai
* @date 2018/10/9 16:35
*/
@RestController
@RequestMapping("twoScenes")
public class ZJTwoScenesController extends BaseController{
//todo 预留的两景Controller

@Autowired
private ZKCurator zkCurator;

@RequestMapping("/check")
public Object check(){
return ResultHelper.success(zkCurator.isZKAlive()?"已连接":"断开");
}
}