利用Curator客户端操作Zookeeper(二)
Curator是Netfix公司开源的一套zookeeper客户端框架,Curator是对Zookeeper支持最好的客户端框架。Curator封装了大部分Zookeeper的功能,比如Leader选举、分布式锁等,减少了技术人员在使用Zookeeper时的底层细节开发工作。
2.引入curator
引入依赖
<!-- zookeeper --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.7.2</version> </dependency>
3.添加配置文件
curator.retryCount=5 curator.elapsedTimeMs=5000 curator.connectString=192.168.63.154:2181 curator.sessionTimeoutMs=60000 curator.connectionTimeoutMs=5000
4.代码编写 4.1 读取配置文件
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 connectString; private int sessionTimeoutMs; private int connectionTimeoutMs; }
4.2 初始化方法
zookeeper注入CuratorFramework对象并初始化参数
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 WrapperZK wrapperZK; @Bean(initMethod = "start") public CuratorFramework curatorFramework(){ return CuratorFrameworkFactory.newClient( wrapperZK.getConnectString(), wrapperZK.getSessionTimeoutMs(), wrapperZK.getConnectionTimeoutMs(), new RetryNTimes(wrapperZK.getRetryCount(),wrapperZK.getElapsedTimeMs()) ); } }
4.3 编写Zookeeper测试用例
import lombok.extern.slf4j.Slf4j; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.NodeCache; import org.apache.curator.framework.recipes.cache.NodeCacheListener; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @Slf4j @SpringBootTest public class BootZkClientApplicationTests { @Autowired CuratorFramework curatorFramework; @Test void createNode() throws Exception{ //添加持久节点 String path = curatorFramework.create().forPath("/curator-node"); //添加临时节点 //String path = curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curator-node","some-data".getBytes()); System.out.println(String.format("Curator create node :%s successfully.",path)); System.in.read(); } @Test void testGetData() throws Exception { byte[] bytes = curatorFramework.getData().forPath("/curator-node"); System.out.println(new String(bytes)); } @Test void testSetData() throws Exception { curatorFramework.setData().forPath("/curator-node","change!".getBytes()); byte[] bytes = curatorFramework.getData().forPath("/curator-node"); System.out.println(new String(bytes)); } @Test void testCreateWithParent() throws Exception { String pathWithParent="/node-parent/sub-node-1"; String path = curatorFramework.create().creatingParentsIfNeeded().forPath(pathWithParent); System.out.println(String.format("curator create node :%s successfully",path)); } @Test void testDelete() throws Exception { String pathWithParent="/node-parent"; curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(pathWithParent); } @Test void AddNodeListener() throws Exception { NodeCache nodeCache = new NodeCache(curatorFramework,"/curator-node"); nodeCache.getListenable().addListener(new NodeCacheListener() { @Override public void nodeChanged() throws Exception { log.info("{} path nodeChangee: ","/curator-node"); } }); nodeCache.start(); System.in.read(); } public void printNodeData() throws Exception { byte[] bytes =curatorFramework.getData().forPath("/curator-node"); log.info("data:{}",new String(bytes)); } }
至此,利用Curator客户端操作Zookeeper基本操作完成!
未完待续!