Dubbo Zookeeper 初探
2. 服务提供者的工程
a. dubbo-demo-api 定义接口
|
1
2
3
|
public interface IProcessData { public String deal(String data);} |
b. dubbo-demo-provider 服务提供者
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class ProcessDataImpl implements IProcessData { /* * @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String) */ @Override public String deal(String data) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "Finished:" + data; }} |
c. applicationProvider.xml配置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <!-- Application name --> <dubbo:application name="hello-world-app" /> <!-- registry address, used for service to register itself --> <!-- expose this service through dubbo protocol, through port 20880 --> <!-- <dubbo:protocol name="dubbo" port="20880" /> <dubbo:protocol name="dubbo" port="9090" server="netty" client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8" threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192" accepts="1000" payload="8388608" /> --> <!-- Service interface Concurrent Control --> <dubbo:service interface="com.huangjie.dubbo_Service.service.IProcessData" ref="demoService" executes="10" /> <!-- Default Protocol --> <!-- <dubbo:protocol server="netty" /> --> <!-- designate implementation --> <bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" /> </beans> |
d. 启动服务
|
1
2
3
4
5
6
7
8
9
10
|
public class DubboProviderMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"applicationProvider.xml"}); context.start(); System.out.println("Press any key to exit."); System.in.read(); } } |
3. 服务调用者的工程
a. 调用类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public class ConsumerThd implements Runnable { /* * @see java.lang.Runnable#run() */ @Override public void run() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"applicationConsumer.xml"}); context.start(); IProcessData demoService = (IProcessData) context.getBean("demoService"); // get // service // invocation // proxy String hello = demoService.deal("nihao"); // do invoke! System.out.println(Thread.currentThread().getName() + " "+hello); } public static void main(String[] args) { new Thread(new ConsumerThd()).start(); /** * 输出结果: * Thread-0 Finished:nihao */ }} |
b. applicationConsumer.xml配置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <!-- consumer application name --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- registry address, used for consumer to discover services --> <dubbo:consumer timeout="5000"/> <!-- which service to consume? --> <dubbo:reference id="demoService" interface="com.huangjie.dubbo_Service.service.IProcessData" /> </beans> |
4. 最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行

这样整个调用就完成了,这样的好处是只要远程提供ip地址及端口号,以及对外调用的类,客户端就可以调用,客户端不必知道服务端的地址之类的。
而且服务端可以开多个zookeeper服务,这样如果其中一个zookeeper 服务死掉了,其他服务还能正常运行。
