Dubbo zookeeper 初探

2023-11-07

原文地址:http://blog.csdn.net/wxwzy738/article/details/16330383


转:http://blog.csdn.net/u011270461/article/details/12144623

建议参考资料:

http://blog.csdn.net/lin_fs/article/details/7395307

http://blog.csdn.net/goliathray/article/details/8565801

http://zy116494718.iteye.com/blog/1830138

http://agapple.iteye.com/blog/1292473

http://www.open-open.com/news/view/1442a5c

http://code.alibabatech.com/wiki/display/dubbo/Home

http://code.alibabatech.com/wiki/display/dubbo/User+Guide-zh

先把zookeeper在本地给安装好,

安装方法参考:http://blog.csdn.net/wxwzy738/article/details/16330253

这里的话讲述了两个工程一个工程是提供服务的,一个工程是调用服务的,因为dubbo是跟spring进行无缝连接的,故功能配置在spring的配置文件中,跟spring进行整合开发

1、工程是以maven进行构建的,使用的jar包如下:


2、服务提供者的工程

a、dubbo-demo-api  定义接口

  1. public interface IProcessData {  
  2.     public String deal(String data);  
  3. }  
b、 dubbo-demo-provider 服务提供者
  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. }  

c、applicationProvider.xml配置
  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.huangjie.dubbo_Service.service.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.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />    
  36.     
  37. </beans>   

d、 启动服务
  1. public class DubboProviderMain {    
  2.     public static void main(String[] args) throws Exception {    
  3.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
  4.                 new String[]{"applicationProvider.xml"});    
  5.         context.start();  
  6.     
  7.         System.out.println("Press any key to exit.");    
  8.         System.in.read();    
  9.     }    
  10. }   

3、服务调用者的工程

a、调用类

  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[]{"applicationConsumer.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.     public static void main(String[] args) {  
  22.         new Thread(new ConsumerThd()).start();  
  23.         /** 
  24.          * 输出结果: 
  25.          * Thread-0 Finished:nihao 
  26.          */  
  27.     }  
  28. }   

b、applicationConsumer.xml配置
  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.huangjie.dubbo_Service.service.IProcessData" />    
  18. </beans>   
4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行


这样整个调用就完成了,这样的好处是只要远程提供ip地址及端口号,以及对外调用的类,客户端就可以调用,客户端不必知道服务端的地址之类的

而且服务端可以开多个zookeeper服务,这样如果其中一个zookeeper 服务死掉了,其他服务还能正常运行


工程下载路径:http://download.csdn.net/detail/wxwzy738/6553431


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Dubbo zookeeper 初探 的相关文章

  • Flink运行时之批处理程序生成计划

    批处理程序生成计划 DataSet API所编写的批处理程序跟DataStream API所编写的流处理程序在生成作业图 JobGraph 之前的实现差别很大 流处理程序是生成流图 StreamGraph 而批处理程序是生成计划 Plan
  • Red Hat Linux 命令Crontab的使用方法

    Red Hat Linux 命令Crontab的使用方法1 cron是一个linux下的定时执行工具 可以在无需人工干预的情况下运行作业 由于Cron 是Linux的内置服务 但它不自动起来 可以用以下的方法启动 关闭这个服务 sbin s
  • Pytorch推出fx,量化起飞

    本文首发于公众号 没事来逛逛 Pytorch1 8 发布后 官方推出一个 torch fx 的工具包 可以动态地对 forward 流程进行跟踪 并构建出模型的图结构 这个新特性能带来什么功能呢 别的不说 就模型量化这一块 炼丹师们有福了
  • Linux下怎么让挂起的(suspend or stopped)进程恢复执行(resume)

    今天在linux安装app的时候 安装的进度长时间停止不前 于是我使用Ctrl Z 打断了安装 然后又运行了一遍安装的命令 这个时候 提示了警告 说这个安装已经安排了但是现在的状态是suspend的 一时间 我想要通过PID把这个进程差掉
  • Windows下 的MySQL安装、配置以及中文字符集编码设置

    一 MySQL安装 第一步 双击程序包 会弹出如下图 第二步 点击Next之后 出现如下图 第三步 选完自己安装的版本 点击Next 第四步 点击Next之后 出现如下图 第五步 点击两次Next之后 显示如下图 根据自己需求改动 一般情况
  • 软件设计师笔记公告(备考攻略)

    软件设计师笔记公告 上午题 下午题 先上成绩 今天软考成绩出来了 很高兴我一次拿下 非常感谢b站up主zst 2001 zst 2001主页链接 我一次性通过很大程度上是因为zst 2001的帮助 于此同时 你们能看到我这个笔记也要感谢up
  • Python 标准库之 xml.etree.ElementTree 简介

    文章来源 https www cnblogs com ifantastic archive 2013 04 12 3017110 html 简介 Element类型是一种灵活的容器对象 用于在内存中存储结构化数据 注意 xml etree

随机推荐