一、实例搭建
1、搭建框架前先下载Zookeeper(http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz)
2、解压Zookeeper到指定文件目录,在bin目录下双击zkServer.cmd(Windows),启动Zookeeper服务,正常应该是如下图所示,错误则看第三步
3、若启动失败,则在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=F:\Work\Zookeeper\data
#日志位置
dataLogDir=F:\Work\Zookeeper\logs
# the port at which the clients willconnect 客户端连接的端口
clientPort=2181
参数详解:
1.tickTime:CS通信心跳数
Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。tickTime以毫秒为单位。
2.initLimit:LF初始通信时限
集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
3.syncLimit:LF同步通信时限
4.dataDir:数据文件目录
5.dataLogDir:日志文件目录
6.clientPort:客户端连接端口
7.服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
server.N=YYY:A:B
eg:
server.0=233.34.9.144:2008:6008
server.1=233.34.9.145:2008:6008
3、配置pom.xml(Provider与Consumer配置一致)
<dependencies>
<dependency>
<!-- dubbo -->
<!-- spring -->
<!-- zookeeper --><dependency>
Provider方:
结构如下图(和Consumer方类似)
4、具体类的编写(和Consumer方一致)
在model下新建一个User类,但是由于使用Dubbo,所以一定要实现序列化Serializable类
public class User implements Serializable{
private static final long serialVersionUID = -1009733312893309388L;
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
然后在service下新建一个DemoService接口(和Consumer方一致),impl下新建DemoServiceImpl实现接口
public interface DemoService {
String sayHello(String name);
public List<User> getUsers();
}
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
public List<User> getUsers() {
List<User> list = new ArrayList<User>();
User u1 = new User();
u1.setName("jack");
u1.setAge(20);
u1.setSex("女");
User u2 = new User();
u2.setName("tom");
u2.setAge(21);
u2.setSex("男");
User u3 = new User();
u3.setName("rose");
u3.setAge(19);
u3.setSex("男");
list.add(u1);
list.add(u2);
list.add(u3);
return list;
}
}
然后provider下新建一个Provider类,实现在Zookeeper中注册
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application.xml"});
context.start();
try {
System.in.read();// 为保证服务一直开着,利用输入流的阻塞来模拟
} catch (IOException e) {
e.printStackTrace();
}
}
}
5、application.xml的配置
<?xml version="1.0" encoding="UTF-8"?><!-- 使用zookeeper注册中心暴露服务地址 -->
Consumer方:
目录结构
我理解的是Provider方在Zookeeper注册,暴露服务地址以及DemoService接口,然后Consumer方就可以调用其暴露出来的接口,具体实现由Provider完成,Consumer方只需要拥有与Provider方一致的接口,调用接口方法就实现远程调用。
主要贴出与Provider不同的代码,其他与其类似或一致的就不贴了。
1、consumer下新建Consumer类
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "application.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); //
String hello = demoService.sayHello("tom"); //调用sayHello方法
System.out.println(hello);
//获取用户列表
List<User> list = demoService.getUsers();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、application.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <!-- 使用zookeeper注册中心暴露服务地址 -->
然后先启动Provider再启动Consumer,结果如下图:
二、常见问题
1、Dubbo采用Spring配置方式,加入Schema即可,如下
但是可能报错:
Multiple annotations found at this line:
解决方案:
在maven下载的dubbo.jar(路径:C:\Users\Administrator\.m2\repository\com\alibaba\dubbo\2.5.3)解压文件中可以找到dubbo.xsd(搜索查找即可)
然后Window-->Preferences-->XML-->XML Catalog-->Add-->Catalog Entry
由于Uri Location的路径中不能包含 .,所以我将其重新拷贝到另一个地方了,一定要修改Key,配置如下:
然后右键项目,选择Validate!