1.win7安装Zookeeper 3.4.6(zookeeper-3.3.6.tar.gz)解压到d盘
2.进入到CONF目录下,将里面的.cfg文件重命名为zoo.cfg.
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just # example sakes. dataDir=D:\\zookeeper-3.4.6\\data dataLogDir=D:\\zookeeper-3.4.6\\log #dataDir=/tmp/zookeeper
从上面代码可以看到添加了两行。在本机里。zookeeper放在D盘里,然后就添加了dataDir及dataLogDir两个变量。与此同时在zookeeper文件目录下新建data及log两个文件夹,如果不创建,后面运行脚本是地会报错。
3.启动 bin/zkServer.cmd
4.下载dubbo源码 dubbo-admin-2.5.3.war 放置tomcat webapps下
5.启动tomcat,访问root/root
(创建interface 后,可以执行maven build了。 在项目上,右键-》runas-》 maven clean,执行完后,再执行 run as -》maven install)
2.服务端开发
pom.xml:
[java] view plain copy <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId> <artifactId>StudyDubboServer</artifactId> <version>0.1</version> <build/> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.study</groupId> <artifactId>StudyDubboApi</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <type>pom</type> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> </dependencies> </project> applicationProvider.xml [java] view plain copy (http://code.alibabatech.com/schema/dubbo/dubbo.xsd无效了,自行下载替换)
[java] view plain copy <?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="Provider_DubboHelloService" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" ></dubbo:registry> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!-- 要暴露的服务API接口 --> <dubbo:service interface="com.study.dubbo.demo.HelloServiceApi" ref="HelloService" /> <!-- 接口的实现,注意beanid 需要和服务接口的ref 一致 --> <bean id="HelloService" class="com.study.dubbo.demo.HelloServiceApiImpl" /> </beans>
ProviderMain
[java] view plain copy package com.study.api; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProviderMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" }); context.start(); System.out.println("按任意键退出"); System.in.read(); } } HelloServiceApiImpl
[java] view plain copy package com.study.dubbo.demo; public class HelloServiceApiImpl implements HelloServiceApi{ String myName=""; public String sayHello(String name){ myName = name; String ret="Hello, "+name+"!"; System.out.println( ret ); return ret; } public String getName ( ){ System.out.println( "Now name is:"+ myName+";" ); return myName; } }
3.客户端
pom.xml
[java] view plain copy <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId> <artifactId>StudyDubboCli</artifactId> <version>0.1</version> <build/> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.study</groupId> <artifactId>StudyDubboApi</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> </dependencies> </project> ConsumerMain
[java] view plain copy package com.study.api; import com.study.dubbo.demo.HelloServiceComsumer; public class ConsumerMain { public static void main(String[] args) throws Exception{ HelloServiceComsumer consumerService = new HelloServiceComsumer(); consumerService.getServiceObj(); String myFamily[]={ "Mom","Daddy","Honey","Pretty Girl"}; for(int i=0;i<myFamily.length;i++){ consumerService.sayHello(myFamily[i] ); consumerService.getName(); } System.out.println("按任意键退出"); System.in.read(); } } HelloServiceComsumer
[java] view plain copy package com.study.dubbo.demo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloServiceComsumer{ HelloServiceApi demoService; /* * 获取服务的Provider对象 */ public void getServiceObj(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationConsumer.xml" }); context.start(); demoService = (HelloServiceApi) context .getBean("HelloService"); } /* * 调用对象的sayHello 方法 */ public void sayHello( String name) { System.out.println(demoService.sayHello( name )); } /* * 调用对象的getName 方法 */ public String getName(){ String ret=demoService.getName( ); System.out.println("Now Provider Name is:"+ ret); return ret; } } applicationConsumer.xml
[java] view plain copy <?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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- consumer application name --> <dubbo:application name="Consumer_DubboHelloService" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry address="zookeeper://127.0.0.1:2181" ></dubbo:registry> <dubbo:consumer timeout="5000" /> <!-- 引用服务接口 --> <dubbo:reference id="HelloService" interface="com.study.dubbo.demo.HelloServiceApi"/> </beans>