浏览代码

ipu-zk测试代码

liutong3 6 年之前
父节点
当前提交
6db555cc9e

+ 2 - 0
.gitignore

@ -12,3 +12,5 @@
12 12
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
13 13
hs_err_pid*
14 14
15
.idea/
16
.project

+ 55 - 0
ipu-zk-example/ipu-zk-example.iml

@ -0,0 +1,55 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4
    <output url="file://$MODULE_DIR$/target/classes" />
5
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
    <content url="file://$MODULE_DIR$">
7
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10
      <excludeFolder url="file://$MODULE_DIR$/target" />
11
    </content>
12
    <orderEntry type="inheritedJdk" />
13
    <orderEntry type="sourceFolder" forTests="false" />
14
    <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
15
    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
16
    <orderEntry type="library" name="Maven: com.ai.ipu:ipu-zk:3.1-SNAPSHOT" level="project" />
17
    <orderEntry type="library" name="Maven: org.apache.zookeeper:zookeeper:3.4.9" level="project" />
18
    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.6.1" level="project" />
19
    <orderEntry type="library" name="Maven: org.slf4j:slf4j-log4j12:1.6.1" level="project" />
20
    <orderEntry type="library" name="Maven: log4j:log4j:1.2.16" level="project" />
21
    <orderEntry type="library" name="Maven: jline:jline:0.9.94" level="project" />
22
    <orderEntry type="library" name="Maven: io.netty:netty:3.10.5.Final" level="project" />
23
    <orderEntry type="library" name="Maven: org.apache.curator:curator-framework:2.8.0" level="project" />
24
    <orderEntry type="library" name="Maven: org.apache.curator:curator-client:2.8.0" level="project" />
25
    <orderEntry type="library" name="Maven: org.apache.curator:curator-recipes:2.8.0" level="project" />
26
    <orderEntry type="library" name="Maven: com.101tec:zkclient:0.10" level="project" />
27
    <orderEntry type="library" name="Maven: com.google.guava:guava:16.0.1" level="project" />
28
    <orderEntry type="library" name="Maven: com.ai.ipu:ipu-basic:3.1-SNAPSHOT" level="project" />
29
    <orderEntry type="library" name="Maven: com.ai.ipu:ipu-common:3.1-SNAPSHOT" level="project" />
30
    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.3.8" level="project" />
31
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.2" level="project" />
32
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.1" level="project" />
33
    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
34
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.1" level="project" />
35
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
36
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.2" level="project" />
37
    <orderEntry type="library" name="Maven: org.json:json:20131018" level="project" />
38
    <orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
39
    <orderEntry type="library" name="Maven: xml-apis:xml-apis:1.0.b2" level="project" />
40
    <orderEntry type="library" name="Maven: jaxen:jaxen:1.1.1" level="project" />
41
    <orderEntry type="library" name="Maven: jdom:jdom:1.0" level="project" />
42
    <orderEntry type="library" name="Maven: xerces:xercesImpl:2.6.2" level="project" />
43
    <orderEntry type="library" name="Maven: xom:xom:1.0" level="project" />
44
    <orderEntry type="library" name="Maven: xerces:xmlParserAPIs:2.6.2" level="project" />
45
    <orderEntry type="library" name="Maven: xalan:xalan:2.6.0" level="project" />
46
    <orderEntry type="library" name="Maven: com.ibm.icu:icu4j:2.6.1" level="project" />
47
    <orderEntry type="library" name="Maven: org.jsoup:jsoup:1.7.2" level="project" />
48
    <orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" />
49
    <orderEntry type="library" name="Maven: org.apache.poi:poi:3.9" level="project" />
50
    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.5" level="project" />
51
    <orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.38" level="project" />
52
    <orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
53
    <orderEntry type="library" name="Maven: com.ai.wade:wade-data:1.0" level="project" />
54
  </component>
55
</module>

+ 61 - 0
ipu-zk-example/src/main/java/com/ai/ipu/example/zookeeper/PushServerPathWatcher.java

@ -0,0 +1,61 @@
1
package com.ai.ipu.example.zookeeper;
2
3
4
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7
8
9
import com.ai.ipu.zk.listener.AbstractEventWatcher;
10
11
/**
12
 * 节点操作类
13
 * 对于PushMgmt <--> zookeeper集群而言,是 1 v 1 的连接
14
 *
15
 *
16
 * @author kevin
17
 *
18
 */
19
public class PushServerPathWatcher extends AbstractEventWatcher<PathChildrenCacheEvent>{
20
    private static final Logger LOG = LoggerFactory.getLogger(PushServerPathWatcher.class);
21
22
    /**
23
     * 节点添加,适用于TreeNodeCache、PathParentCache
24
     *
25
     * @param event 触发事件
26
     */
27
    @Override
28
    public void addListener(PathChildrenCacheEvent event) {
29
        if(event.getData() == null)
30
            return;
31
        LOG.debug("PushServerPathWatcher-add:path=["+event.getData().getPath()+"],value=["+new String(event.getData().getData())+"]");
32
    }
33
34
    /**
35
     * 节点删除,适用于PathParentCache、TreeNodeCache
36
     *
37
     * @param event 触发事件
38
     */
39
    @Override
40
    public void removeListener(PathChildrenCacheEvent event) {
41
        if(event.getData() == null)
42
            return;
43
        LOG.debug("PushServerPathWatcher-remove:path=["+event.getData().getPath()+"],value=["+new String(event.getData().getData())+"]");
44
    }
45
46
    /**
47
     * 节点修改,适用于PathParentCache、TreeNodeCache
48
     *
49
     * 只有当 Push-Server 的 http 端口修改时,才会触发相应操作;
50
     *
51
     * @param event 触发事件
52
     */
53
    @Override
54
    public void changeListener(PathChildrenCacheEvent event) {
55
        //空数据不予处理
56
        if(event.getData() == null)
57
            return;
58
59
        LOG.debug("PushServerPathWatcher-modify:path=["+event.getData().getPath()+"],value=["+new String(event.getData().getData())+"]");
60
    }
61
}

+ 84 - 0
ipu-zk-example/src/main/java/com/ai/ipu/example/zookeeper/PushServerTreeWatcher.java

@ -0,0 +1,84 @@
1
package com.ai.ipu.example.zookeeper;
2
3
import com.ai.ipu.zk.listener.AbstractEventWatcher;
4
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
5
import org.apache.zookeeper.data.Stat;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
9
10
/**
11
 * 节点操作类
12
 * 对于PushMgmt <--> zookeeper集群而言,是 1 v 1 的连接
13
 *
14
 *
15
 * @author kevin
16
 *
17
 */
18
public class PushServerTreeWatcher extends AbstractEventWatcher<TreeCacheEvent>{
19
    private static final Logger LOG = LoggerFactory.getLogger(PushServerTreeWatcher.class);
20
21
    /**
22
     * 节点添加,适用于TreeNodeCache、PathParentCache
23
     *
24
     * @param event 触发事件
25
     */
26
    @Override
27
    public void addListener(TreeCacheEvent event) {
28
        //不需要关注的目录跳过
29
        if(skip(event)) return;
30
        LOG.debug("PushServerTreeWatcher-add:path=["+event.getData().getPath()+"],value=["+ new String(event.getData().getData())+"]");
31
    }
32
    /**
33
     * 节点删除,适用于PathParentCache、TreeNodeCache
34
     *
35
     * @param event 触发事件
36
     */
37
    @Override
38
    public void removeListener(TreeCacheEvent event) {
39
        //空数据不予处理
40
        if(event.getData() == null)
41
            return;
42
43
        if (event.getData().getData() == null) {
44
            LOG.debug("PushServerTreeWatcher-remove:path=["+event.getData().getPath()+"],value=[]");
45
        } else {
46
            LOG.debug("PushServerTreeWatcher-remove:path=["+event.getData().getPath()+"],value=["+ new String(event.getData().getData())+"]");
47
        }
48
    }
49
50
    /**
51
     * 节点修改,适用于PathParentCache、TreeNodeCache
52
     *
53
     * 只有当 Push-Server 的 http 端口修改时,才会触发相应操作;
54
     *
55
     * @param event 触发事件
56
     */
57
    @Override
58
    public void changeListener(TreeCacheEvent event) {
59
        //空数据不予处理
60
        if(event.getData() == null)
61
            return;
62
63
        LOG.debug("PushServerTreeWatcher-modify:path=["+event.getData().getPath()+"],value=["+ new String(event.getData().getData())+"]");
64
    }
65
    /**
66
     * 不需要要处理的事件
67
     *
68
     * @param event
69
     * @return
70
     * @throws
71
     */
72
    private boolean skip(TreeCacheEvent event) {
73
        //空数据不予处理
74
        if(event.getData() == null)
75
            return true;
76
77
        //只处理叶子节点(PushServer的值)
78
        Stat stat = event.getData().getStat();
79
        if(stat.getNumChildren() != 0)
80
            return true;
81
82
        return false;
83
    }
84
}

+ 57 - 0
ipu-zk-example/src/main/java/com/ai/ipu/example/zookeeper/ZookeeperExample.java

@ -0,0 +1,57 @@
1
package com.ai.ipu.example.zookeeper;
2
3
import com.ai.ipu.zk.ICuratorClient;
4
import com.ai.ipu.zk.ZkClientFactory;
5
import com.ai.ipu.zk.util.OperType;
6
import org.junit.Before;
7
import org.junit.Test;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
11
public class ZookeeperExample {
12
    private static final Logger LOG = LoggerFactory.getLogger(ZookeeperExample.class);
13
    private static final String PATH = "test";
14
    private static final String PATH2 = "test/test2";
15
    private static final String TYPE = "/myzktest";
16
    private static final boolean VERSIONCTL = true;
17
    private static final boolean PERSISTENT = true;
18
    private ICuratorClient client;
19
20
    @Before
21
    public void setBefore(){
22
        try {
23
            client = (ICuratorClient) ZkClientFactory.getZkClient("push");
24
            setUserDefine();
25
            client.addPathCacheListener(OperType.Define, PATH, true, new PushServerPathWatcher());
26
            client.addTreeCacheListener(OperType.Define, PATH, true, new PushServerTreeWatcher());
27
        } catch (Exception e) {
28
            LOG.error(e.getMessage(), e);
29
        }
30
    }
31
32
    private void setUserDefine() {
33
        OperType.Define.setType(TYPE);
34
        OperType.Define.setPersistent(PERSISTENT);
35
        OperType.Define.setVersionCtrl(VERSIONCTL);
36
    }
37
38
    @Test
39
    public void testSetValue() throws Exception{
40
        LOG.debug(Boolean.toString(client.setValue(OperType.Define, PATH, "myData1")));
41
    }
42
43
    @Test
44
    public void testSetValue2() throws Exception{
45
        LOG.debug(Boolean.toString(client.setValue(OperType.Define, PATH2, "myData1")));
46
    }
47
48
    @Test
49
    public void testGetValue() throws Exception {
50
        LOG.debug(client.getValue(OperType.Define, PATH2));
51
    }
52
53
    @Test
54
    public void deleteNode() throws Exception {
55
        LOG.debug(Boolean.toString(client.delete(OperType.Define, PATH2)));
56
    }
57
}

+ 21 - 0
ipu-zk-example/src/main/resources/ipu-zk.xml

@ -0,0 +1,21 @@
1
<?xml version = '1.0' encoding = 'UTF-8'?>
2
<zookeepers>
3
    <zookeeper name="push">
4
        <!-- 连接主机 -->
5
        <config name="connHosts" value="47.105.160.21:2101,47.105.160.21:2102,47.105.160.21:2103"/>
6
        <!-- 认证名和密码 -->
7
        <config name="auth" value="ipu:ipu321"/>
8
        <!-- 身份验证方式,world,auth,digest,ip -->
9
        <config name="acl" value="digest"/>
10
        <!-- 版本信息 -->
11
        <config name="version" value="3.0"/>
12
        <!-- 会话超时时间-->
13
        <config name="sessionTimeout" value="30000"/>
14
        <!-- 连接超时时间-->
15
        <config name="connectTimeout" value="3000"/>
16
        <!-- 重连超时时间-->
17
        <config name="reconnectTimeout" value="1000"/>
18
        <!-- 重连次数-->
19
        <config name="reconnectCount" value="3"/>
20
    </zookeeper>
21
</zookeepers>

+ 8 - 0
ipu-zk-example/src/main/resources/log4j.properties

@ -0,0 +1,8 @@
1
log4j.rootLogger=error, console
2
log4j.logger.org.apache.com.ai.ipu.zookeeper=error
3
log4j.logger.com.ai.ipu.example=debug
4
5
log4j.appender.console=org.apache.log4j.ConsoleAppender
6
log4j.appender.console.target=System.out
7
log4j.appender.console.layout=org.apache.log4j.PatternLayout
8
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n