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配置文件
配置如下,主要修改路径地址(参考:http://blog.csdn.net/morning99/article/details/40426133)
# 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同步通信时限 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。
4.dataDir:数据文件目录 Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。
5.dataLogDir:日志文件目录 Zookeeper保存日志文件的目录。
6.clientPort:客户端连接端口 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
7.服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口) 这个配置项的书写格式比较特殊,规则如下:
server.N=YYY:A:B eg: server.0=233.34.9.144:2008:6008 server.1=233.34.9.145:2008:6008
Zookeeper配置参数详解:http://blog.csdn.net/poechant/article/details/6650249
3、配置pom.xml(Provider与Consumer配置一致)
<dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.45</version> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.2-alpha</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies>
结构如下图(和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"?> <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 "> <!-- 具体的实现bean --> <bean id="demoService" class="com.zd.dubbo.service.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="xixi_provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.zd.dubbo.service.DemoService" ref="demoService" /> </beans>
目录结构
我理解的是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"?> <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="hehe_consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.zd.dubbo.service.DemoService" /> </beans>
然后先启动Provider再启动Consumer,结果如下图:
1、Dubbo采用Spring配置方式,加入Schema即可,如下
但是可能报错:
Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
解决方案:
在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!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有