先搭建zookeeper环境
下载Zookeeper-3.4.6.tar.gz 地址http://www.apache.org/dist/zookeeper/
然后解压: 然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件
修改里面配置为:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
initLimit=10
# The number of ticks that can pass between
syncLimit=5
# the directory where the snapshot is stored.
dataDir=D://tools//zookeeper//tmp
dataLogDir=D://tools//zookeeper//logs
# the port at which the clients will connect
clientPort=2181
#autopurge.snapRetainCount=3
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
然后启动zookeeper 在安装目录的bin目录下 zkServer.cmd编写duboo简单demo
代码目录如下:
client
package duboo.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.HelloWorld; public class ConsumerThd implements Runnable { public void run() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationConsumer.xml" }); context.start(); HelloWorld helloWorld = (HelloWorld) context.getBean("demoService"); String hello = helloWorld.hello("duboo"); System.out.println(hello); } public static void main(String[] args) { new Thread(new ConsumerThd()).start(); } }
server
package cn; /** * Created by zhouq on 2016/3/21. */ public interface HelloWorld { public String hello(String name); }
package cn.impl; import cn.HelloWorld; public class HelloWorldImpl implements HelloWorld { public String hello(String name) { return "hello" + name; } }
package cn; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProviderMain { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" }); context.start(); System.out.println("Press any key to exit."); System.in.read(); } }
配置文件:
applicationConsumer.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="duboo-client" /> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference version="1.0" id="demoService" interface="cn.HelloWorld"/> </beans>
applicationProvider.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="duboo-server" /><!-- 注册地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service version="1.0" interface="cn.HelloWorld" ref="demoService" executes="10" /> <bean id="demoService" class="cn.impl.HelloWorldImpl" /> </beans>
转载于:https://my.oschina.net/u/582886/blog/646226