Skip to content

Commit 405cf13

Browse files
author
88171985
committed
update
0 parents  commit 405cf13

14 files changed

Lines changed: 571 additions & 0 deletions

File tree

pom.xml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>zookeeper-dubbo</groupId>
8+
<artifactId>zookeeper-dubbo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.7</source>
17+
<target>1.7</target>
18+
</configuration>
19+
</plugin>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-compiler-plugin</artifactId>
23+
<configuration>
24+
<source>1.6</source>
25+
<target>1.6</target>
26+
</configuration>
27+
</plugin>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<configuration>
32+
<source>1.6</source>
33+
<target>1.6</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<configuration>
40+
<source>1.6</source>
41+
<target>1.6</target>
42+
</configuration>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
47+
48+
<dependencies>
49+
<!--zk-->
50+
<dependency>
51+
<groupId>org.apache.zookeeper</groupId>
52+
<artifactId>zookeeper</artifactId>
53+
<version>3.4.9</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.101tec</groupId>
57+
<artifactId>zkclient</artifactId>
58+
<version>0.10</version>
59+
</dependency>
60+
61+
<!-- dubbo -->
62+
<dependency>
63+
<groupId>com.alibaba</groupId>
64+
<artifactId>dubbo</artifactId>
65+
<version>2.4.9</version>
66+
</dependency>
67+
<!-- spring -->
68+
<dependency>
69+
<groupId>org.springframework</groupId>
70+
<artifactId>spring-context</artifactId>
71+
<version>3.2.8.RELEASE</version>
72+
<scope>runtime</scope>
73+
</dependency>
74+
<!-- junit -->
75+
<dependency>
76+
<groupId>junit</groupId>
77+
<artifactId>junit</artifactId>
78+
<version>4.11</version>
79+
</dependency>
80+
81+
</dependencies>
82+
83+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dubbo.helloworld;
2+
3+
import org.springframework.context.support.ClassPathXmlApplicationContext;
4+
5+
/**
6+
* 消费者消费服务
7+
*/
8+
public class ConsumerAction {
9+
public static void main(String[] args) {
10+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/helloDubbo/consumer.xml");
11+
DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
12+
String hello = demoService.sayHello("KKys!"); // 执行远程方法
13+
System.out.println(hello); // 显示调用结果
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dubbo.helloworld;
2+
3+
/**
4+
* 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
5+
*/
6+
public interface DemoService {
7+
String sayHello(String name);
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dubbo.helloworld;
2+
3+
/**
4+
* 在服务提供方实现接口:(对服务消费方隐藏实现)
5+
*/
6+
public class DemoServiceImpl implements DemoService {
7+
@Override
8+
public String sayHello(String name) {
9+
return "Hello dubbo : " + name;
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dubbo.helloworld;
2+
3+
import org.springframework.context.support.ClassPathXmlApplicationContext;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* 启动服务提供方
9+
*/
10+
public class StartProvider {
11+
public static void main(String[] args) throws IOException {
12+
System.out.println("启动");
13+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/helloDubbo/provider.xml");
14+
context.start();
15+
System.in.read(); // 按任意键退出
16+
}
17+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package zookeeper.CreateGroup;
2+
3+
import org.apache.zookeeper.WatchedEvent;
4+
import org.apache.zookeeper.Watcher;
5+
import org.apache.zookeeper.ZooKeeper;
6+
7+
import java.io.IOException;
8+
import java.util.concurrent.CountDownLatch;
9+
10+
/**
11+
* 连接的观察者,封装了zk的创建等
12+
* Created by yaosheng on 2017/1/17.
13+
*
14+
*/
15+
public class ConnectionWatcher implements Watcher {
16+
private static final int SESSION_TIMEOUT = 5000;
17+
18+
protected ZooKeeper zk = null;
19+
private CountDownLatch countDownLatch = new CountDownLatch(1);
20+
21+
public void process(WatchedEvent event) {
22+
Event.KeeperState state = event.getState();
23+
24+
if(state == Event.KeeperState.SyncConnected){
25+
countDownLatch.countDown();
26+
}
27+
}
28+
29+
/**
30+
* 连接资源
31+
* @param hosts
32+
* @throws IOException
33+
* @throws InterruptedException
34+
*/
35+
public void connection(String hosts) throws IOException, InterruptedException {
36+
zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
37+
countDownLatch.await();
38+
}
39+
40+
/**
41+
* 释放资源
42+
* @throws InterruptedException
43+
*/
44+
public void close() throws InterruptedException {
45+
if (null != zk) {
46+
try {
47+
zk.close();
48+
} catch (InterruptedException e) {
49+
throw e;
50+
}finally{
51+
zk = null;
52+
System.gc();
53+
}
54+
}
55+
}
56+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package zookeeper.CreateGroup;
2+
3+
import org.apache.zookeeper.*;
4+
5+
import java.io.IOException;
6+
import java.util.concurrent.CountDownLatch;
7+
8+
/**
9+
* Created by yaosheng on 2017/1/16.
10+
*/
11+
12+
public class CreateGroup implements Watcher{
13+
14+
private static final int SESSION_TIMEOUT = 1000;//会话延时
15+
16+
private ZooKeeper zk = null;
17+
18+
private CountDownLatch countDownLatch = new CountDownLatch(1);//同步计数器
19+
20+
21+
/**
22+
* 这里我们使用了同步计数器CountDownLatch,在connect方法中创建执行了zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);之后,
23+
* 下边接着调用了CountDownLatch对象的await方法阻塞,因为这是zk客户端不一定已经完成了与服务端的连接,在客户端连接到服务端时会触发观察者调用process()方法,
24+
* 我们在方法里边判断一下触发事件的类型,完成连接后计数器减一,connect方法中解除阻塞。
25+
还有两个地方需要注意:这里创建的znode的访问权限是open的,且该znode是持久化存储的。
26+
*
27+
*/
28+
@Override
29+
public void process(WatchedEvent event) {
30+
if(event.getState() == Event.KeeperState.SyncConnected){
31+
countDownLatch.countDown();//计数器减一
32+
}
33+
}
34+
35+
/**
36+
* 创建zk对象
37+
* 当客户端连接上zookeeper时会执行process(event)里的countDownLatch.countDown(),计数器的值变为0,则countDownLatch.await()方法返回。
38+
* @param hosts
39+
* @throws IOException
40+
* @throws InterruptedException
41+
*/
42+
public void connect(String hosts) throws IOException, InterruptedException {
43+
zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
44+
countDownLatch.await();//阻塞程序继续执行
45+
}
46+
47+
/**
48+
* 创建group
49+
*
50+
* @param groupName 组名
51+
* @throws KeeperException
52+
* @throws InterruptedException
53+
*/
54+
public void create(String groupName) throws KeeperException, InterruptedException {
55+
String path = "/" + groupName;
56+
String createPath = zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE/*允许任何客户端对该znode进行读写*/, CreateMode.PERSISTENT/*持久化的znode*/);
57+
System.out.println("Created " + createPath);
58+
}
59+
60+
/**
61+
* 关闭zk
62+
* @throws InterruptedException
63+
*/
64+
public void close() throws InterruptedException {
65+
if(zk != null){
66+
try {
67+
zk.close();
68+
} catch (InterruptedException e) {
69+
throw e;
70+
}finally{
71+
zk = null;
72+
System.gc();
73+
}
74+
}
75+
}
76+
77+
public static void main(String[] args){
78+
79+
}
80+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package zookeeper.CreateGroup;
2+
3+
import org.apache.zookeeper.KeeperException;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
10+
/**
11+
* Created by yaosheng on 2017/1/16.
12+
*/
13+
public class CreateGroupTest {
14+
private static String hosts = "121.42.8.85:2181";
15+
private static String groupName = "zoo";
16+
17+
private CreateGroup createGroup = null;
18+
/**
19+
* init
20+
* @throws InterruptedException
21+
* @throws KeeperException
22+
* @throws IOException
23+
*/
24+
@Before
25+
public void init() throws KeeperException, InterruptedException, IOException {
26+
createGroup = new CreateGroup();
27+
createGroup.connect(hosts);
28+
}
29+
30+
@Test
31+
public void testCreateGroup() throws KeeperException, InterruptedException {
32+
createGroup.create(groupName);
33+
}
34+
35+
/**
36+
* 销毁资源
37+
*/
38+
@After
39+
public void destroy() {
40+
try {
41+
createGroup.close();
42+
createGroup = null;
43+
System.gc();
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package zookeeper.DeleteGroup;
2+
3+
import org.apache.zookeeper.KeeperException;
4+
import zookeeper.CreateGroup.ConnectionWatcher;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Created by yaosheng on 2017/1/17.
10+
* zk.delete(path,version)方法的第二个参数是znode版本号,如果提供的版本号和znode版本号一致才会删除这个znode,
11+
* 这样可以检测出对znode的修改冲突。通过将版本号设置为-1,可以绕过这个版本检测机制,无论znode的版本号是什么,都会直接将其删除。
12+
*/
13+
public class DeleteGroup extends ConnectionWatcher {
14+
public void delete(String groupName) {
15+
String path = "/" + groupName;
16+
17+
try {
18+
List<String> children = zk.getChildren(path, false);
19+
20+
for(String child : children){
21+
zk.delete(path + "/" + child, -1);
22+
}
23+
zk.delete(path, -1);//版本号为-1,
24+
} catch (KeeperException e) {
25+
e.printStackTrace();
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)