本文主要介绍 Dubbo+Zookeeper 实现webservice
1:windows 下安装Zookeeper
1. 概述
ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
2. 安装&配置
在apache的官方网站提供了好多镜像下载地址,然后找到对应的版本,目前最新的是3.3.6
下载地址:
http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz
Windows下安装
把下载的zookeeper的文件解压到指定目录
D:\machine\zookeeper-3.3.6>
修改conf下增加一个zoo.cfg
内容如下:
# The number of milliseconds of each tick 心跳间隔 毫秒每次
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting anacknowledgement
syncLimit=5
# the directory where the snapshot isstored. //镜像数据位置
dataDir=D:\\data\\zookeeper
#日志位置
dataLogDir=D:\\logs\\zookeeper
# the port at which the clients willconnect 客户端连接的端口
clientPort=2181
注:如果启动有报错提示cfg文件有错误,可以用zoo_sample.cfg内内容替代也是可以的
进入到bin目录,并且启动zkServer.cmd,这个脚本中会启动一个Java进程
D:\machine\zookeeper-3.3.6>cd bin
D:\machine\zookeeper-3.3.6\bin>
D:\machine\zookeeper-3.3.6\bin >zkServer.cmd
启动后jps可以看到QuorumPeerMain的进程
D:\machine\zookeeper-3.3.6\bin >jps
启动客户端运行查看一下
D:\machine\zookeeper-3.3.6\bin>zkCli.cmd-server 127.0.0.1:2181
这个时候zookeeper已经安装成功了,
2:创建Dubbo项目 一共2个项目
一:创建者 provider
1:首先创建一个接口类
package com.dubbo;
public interface DemoService {
String sayHello(String name);
}
2:实现他的接口
package com.dubbo;
public class DemoServiceImpl implements DemoService{
public String sayHello(String name) {
return "Hello :"+name;
}
}
3创建配置文件
applicationContext.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 --> // 广播方法有很多,这里指采用Zookeeper 端口名 可以在Zookeeper 的zoo.cfg 里面配置
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.dubbo.DemoService" ref="demoService" />
<!-- 创建接口实现类的bean -->
<bean id="demoService" class="com.dubbo.DemoServiceImpl" />
</beans>
4 main 方法开启线程 读取配置 并 采用输入流的阻塞 来维持服务, 内容如下
package provider;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml"
});
context.start();
System.in.read();
}
}//到这里 创建者已经完成了
二:消费者 consumer
1 创建消费者接口
package com.dubbo;
public interface DemoService {
String sayHello(String name);
}
(消费者 这里就不需要创建接口实现类了,因为他的实现类是从 创建者 调用的,如果写了 实现类 那还叫WebService? 嘿嘿)
2 创建 application.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用Zookeeper广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.dubbo.DemoService"/>
</beans>
(严重问题! 大家一定要记住, 消费者 和创建者 的 包名,以及接口名, 必须相同!必须相同!必须相同! 重要的事情说三遍!)
3 利用main方法创建线程读取配置文件调用 服务
package com.alibaba.dubbo.demo.pp;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.DemoService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String arg=demoService.sayHello("tom");
System.out.println(arg);
System.in.read();
}
}
下面是demo的资源链接 http://download.csdn.net/detail/qq_22313643/9716387
最后补充一点:要下载控制台 dubbo-admin的时候 如果JDK1.8以上的 要下专门的 jdk1.8的 dubbo-admin ,否则启动不起来。
如果项目里 有dubbo 服务,启动项目前务必先启动 zookeeper 否则项目起不来