http://mirrors.hust.edu.cn/apache/zookeeper/下载,我的版本是 3.4.5。
解压到 D:\zookeeper-3.4.5
配置
到目录conf 下创建 zoo.cfg 文件,默认就是加载这个文件,文件内容 我直接copy 的sample里面的
zoo.cfg 的内容
# 心跳检查的时间 2秒
tickTime=2000
# 初始化时 连接到服务器端的间隔次数,总时间10*2=20秒
initLimit=10
# ZK Leader 和follower 之间通讯的次数,总时间5*2=10秒
syncLimit=5
# 存储内存中数据库快照的位置,如果不设置参数,更新事务日志将被存储到默认位置。
dataDir=D:\zk\tmp\zookeeper
# 错误日志的存放位置
dataLogDir=D:\zk\logs\zookeeper
# ZK 服务器端的监听端口
clientPort=2181
运行
然后 cd 到bin 目录下 执行zkServer.cmd 就启动成功了。利用jps命令行命令可以验证zookeeper是否启动成功。
建立WEB项目,引入相应Jar文件。
配置web.xml文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>dubbo-servicedisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>
WEB-INF/dubbo-provider.xml
param-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
写服务类:
接口:
public interface IDemoService {
/**
*
*
* @Function: com.test.dubbo.service.IDemoService.sayHello
* @Description:
*
* @param name
* @return name string
*
* @version:v1.0
* @author:cjq
* @date:2015-4-30 下午5:45:30
*
* Modification History:
* Date Author Version Description
* -----------------------------------------------------------------
* 2015-4-30 cjq v1.0.0 create
*/
public String sayHello(String name);
}
实现:
public class DemoServiceImpl implements IDemoService {
/*
* (non-Javadoc)
*
* @see com.test.dubbo.service.IDemoService#sayHello(java.lang.String)
*/
public String sayHello(String name) {
return "Hello Dubbo,Hello " + name;
}
}
注册服务:
新建dubbo-provider.xml文件,文件内容为:
<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="dubbo-service" />
<dubbo:registry address="zookeeper://xxxx:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.test.dubbo.service.IDemoService"
ref="demoService" />
<bean id="demoService" class="com.test.dubbo.service.impl.DemoServiceImpl" />
beans>
部署启动服务:
新建WEB或者普通JAVA工程,引入Dubbo服务注册项目中的jar文件。
需要导入一个服务接口(从上个程序中导入接口的jar并且导入到此项目中。)
配置web.xml文件(和上工程一样),编写调用配置文件dubbo-consumer.xml文件,内容如下:
<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="dubbo-service-consumer" />
<dubbo:registry address="zookeeper://xxxx:2181" />
<dubbo:reference id="demoService" interface="com.test.dubbo.service.IDemoService"/>
beans>
编写测试代码:
/**
* Copyright (C) 2015
*
*
* @className:com.test.dubbo.consumer.ConsumerTest
* @description:TODO
*
* @version:v1.0.0
* @author:cjq
*
* Modification History:
* Date Author Version Description
* -----------------------------------------------------------------
* 2015-4-30 cjq v1.0.0 create
*
*
*/
package com.test.dubbo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.dubbo.service.IDemoService;
public class ConsumerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"file:D:/javaworkspace/dubbo-service-consumer/WebContent/WEB-INF/dubbo-consumer.xml");
context.start();
IDemoService demoService = (IDemoService) context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println(hello);
}
}
调用测试代码,执行结果为:
如果你在做分布式系统的话,不妨用一下这个技术。