Dubbo zookeeper 初探

1.zookeeper

http://zookeeper.apache.org/ 官网下载,然后安装

启动时,需要将 zoo_sample.cfg 改成 zoo.cfg

[java]  view plain copy
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial   
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between   
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. do not use /tmp for storage, /tmp here is just   
  11. # example sakes.  
  12. dataDir=D:\\Zookeeper-3.4.5\\data  
  13. # the port at which the clients will connect  
  14. clientPort=2181  
  15. #  
  16. # Be sure to read the maintenance section of the   
  17. # administrator guide before turning on autopurge.  
  18. #  
  19. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  20. #  
  21. # The number of snapshots to retain in dataDir  
  22. #autopurge.snapRetainCount=3  
  23. # Purge task interval in hours  
  24. # Set to "0" to disable auto purge feature  
  25. #autopurge.purgeInterval=1  

2. dubbo-demo-api  定义接口

[java]  view plain copy
  1. public interface IProcessData {  
  2.     public String deal(String data);  
  3. }  

3.dubbo-demo-provider 服务提供者

[java]  view plain copy
  1. public class ProcessDataImpl implements IProcessData {  
  2.   
  3.     /*  
  4.      * @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String) 
  5.      */  
  6.     @Override  
  7.     public String deal(String data) {  
  8.         try {  
  9.             Thread.sleep(1000);  
  10.         } catch (InterruptedException e) {  
  11.             e.printStackTrace();  
  12.         }  
  13.         return "Finished:" + data;  
  14.     }  
  15. }  

provider.xml配置

[java]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://code.alibabatech.com/schema/dubbo  
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  8.         ">  
  9.   
  10.     <!-- Application name -->  
  11.     <dubbo:application name="hello-world-app" />  
  12.   
  13.     <!-- registry address, used for service to register itself -->  
  14.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  15.   
  16.     <!-- expose this service through dubbo protocol, through port 20880 -->  
  17.     <!--  
  18.     <dubbo:protocol name="dubbo" port="20880" />  
  19.       
  20.     <dubbo:protocol name="dubbo" port="9090" server="netty"  
  21.         client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8"  
  22.         threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192"  
  23.         accepts="1000" payload="8388608" />  
  24.         -->  
  25.     <!-- Service interface   Concurrent Control  -->  
  26.     <dubbo:service interface="com.bestpay.dubbo.provider.IProcessData"  
  27.         ref="demoService" executes="10" />  
  28.   
  29.     <!-- Default Protocol -->  
  30.     <!--  
  31.     <dubbo:protocol server="netty" />  
  32.     -->  
  33.   
  34.     <!-- designate implementation -->  
  35.     <bean id="demoService" class="com.xxx.dubbo.provider.ProcessDataImpl" />  
  36.   
  37. </beans>  

启动服务
[java]  view plain copy
  1. public class DubboProviderMain {  
  2.   
  3.     /** 
  4.      * @Title main 
  5.      * @Description TODO 
  6.      * @Author weizhi2018 
  7.      * @param args 
  8.      * @throws 
  9.      */  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  13.                 new String[]{"provider.xml"});  
  14.         context.start();  
  15.   
  16.         System.out.println("Press any key to exit.");  
  17.         System.in.read();  
  18.     }  
  19. }  

引用jar


3.dubbo-demo-consumer

[java]  view plain copy
  1. public class ConsumerThd implements Runnable {  
  2.   
  3.     /*  
  4.      * @see java.lang.Runnable#run() 
  5.      */  
  6.     @Override  
  7.     public void run() {  
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  9.                 new String[]{"consumer.xml"});  
  10.         context.start();  
  11.   
  12.         IProcessData demoService = (IProcessData) context.getBean("demoService"); // get  
  13.                                                                                 // service  
  14.                                                                                 // invocation  
  15.         // proxy  
  16.         String hello = demoService.deal("nihao"); // do invoke!  
  17.   
  18.         System.out.println(Thread.currentThread().getName() + " "+hello);  
  19.     }  
  20.   
  21. }  

consumer.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://code.alibabatech.com/schema/dubbo  
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  8.         ">  
  9.   
  10.     <!-- consumer application name -->  
  11.     <dubbo:application name="consumer-of-helloworld-app" />  
  12.   
  13.     <!-- registry address, used for consumer to discover services -->  
  14.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  15.     <dubbo:consumer timeout="5000"/>  
  16.     <!-- which service to consume? -->  
  17.     <dubbo:reference id="demoService" interface="com.xxx.dubbo.provider.IProcessData" />  
  18. </beans>  

4.dubbo-admin

下载dubbo-admin项目,部署到tomcat6下面,启动tomcat ,在浏览器打开:http://localhost:8080/dubbo-admin, 输入用户名/密码:root/root



原文地址:http://blog.csdn.net/u011270461/article/details/12144623

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值