设为首页 收藏本站
查看: 1315|回复: 0

[经验分享] Dubbo zookeeper 初探

[复制链接]

尚未签到

发表于 2015-11-21 13:42:07 | 显示全部楼层 |阅读模式
  转:http://blog.iyunv.com/u011270461/article/details/12144623
  建议参考资料:
  
  http://blog.iyunv.com/lin_fs/article/details/7395307
  http://blog.iyunv.com/goliathray/article/details/8565801
  http://zy116494718.iyunv.com/blog/1830138
  http://agapple.iyunv.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.iyunv.com/wxwzy738/article/details/16330253
  这里的话讲述了两个工程一个工程是提供服务的,一个工程是调用服务的,因为dubbo是跟spring进行无缝连接的,故功能配置在spring的配置文件中,跟spring进行整合开发
  1、工程是以maven进行构建的,使用的jar包如下:
DSC0000.jpg


  2、服务提供者的工程
  a、dubbo-demo-api  定义接口
  

public interface IProcessData {
public String deal(String data);
}
b、dubbo-demo-provider 服务提供者  
  

public class ProcessDataImpl implements IProcessData {
/*
* @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String)
*/
@Override
public String deal(String data) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Finished:" + data;
}
}

c、applicationProvider.xml配置  
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>  
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;  
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:dubbo=&quot;http://code.alibabatech.com/schema/dubbo&quot;  
xsi:schemaLocation=&quot;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  
&quot;>  
<!-- Application name -->  
<dubbo:application name=&quot;hello-world-app&quot; />  
<!-- registry address, used for service to register itself -->  
<dubbo:registry address=&quot;zookeeper://127.0.0.1:2181&quot; />  
<!-- expose this service through dubbo protocol, through port 20880 -->  
<!--  
<dubbo:protocol name=&quot;dubbo&quot; port=&quot;20880&quot; />  
<dubbo:protocol name=&quot;dubbo&quot; port=&quot;9090&quot; server=&quot;netty&quot;  
client=&quot;netty&quot; codec=&quot;dubbo&quot; serialization=&quot;hessian2&quot; charset=&quot;UTF-8&quot;  
threadpool=&quot;fixed&quot; threads=&quot;100&quot; queues=&quot;0&quot; iothreads=&quot;9&quot; buffer=&quot;8192&quot;  
accepts=&quot;1000&quot; payload=&quot;8388608&quot; />  
-->  
<!-- Service interface   Concurrent Control  -->  
<dubbo:service interface=&quot;com.huangjie.dubbo_Service.service.IProcessData&quot;  
ref=&quot;demoService&quot; executes=&quot;10&quot; />  
<!-- Default Protocol -->  
<!--  
<dubbo:protocol server=&quot;netty&quot; />  
-->  
<!-- designate implementation -->  
<bean id=&quot;demoService&quot; class=&quot;com.huangjie.dubbo_Service.service.impl.ProcessDataImpl&quot; />  
</beans>

d、启动服务  
  

public class DubboProviderMain {  
public static void main(String[] args) throws Exception {  
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
new String[]{&quot;applicationProvider.xml&quot;});  
context.start();
System.out.println(&quot;Press any key to exit.&quot;);  
System.in.read();  
}  
}

3、服务调用者的工程  
  a、调用类
  

public class ConsumerThd implements Runnable {  
/*  
* @see java.lang.Runnable#run()
*/  
@Override  
public void run() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
new String[]{&quot;applicationConsumer.xml&quot;});  
context.start();  
IProcessData demoService = (IProcessData) context.getBean(&quot;demoService&quot;); // get  
// service  
// invocation  
// proxy  
String hello = demoService.deal(&quot;nihao&quot;); // do invoke!  
System.out.println(Thread.currentThread().getName() + &quot; &quot;+hello);  
}  
public static void main(String[] args) {
new Thread(new ConsumerThd()).start();
/**
* 输出结果:
* Thread-0 Finished:nihao
*/
}
}

b、applicationConsumer.xml配置  
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>  
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;  
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:dubbo=&quot;http://code.alibabatech.com/schema/dubbo&quot;  
xsi:schemaLocation=&quot;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  
&quot;>  
<!-- consumer application name -->  
<dubbo:application name=&quot;consumer-of-helloworld-app&quot; />  
<!-- registry address, used for consumer to discover services -->  
<dubbo:registry address=&quot;zookeeper://127.0.0.1:2181&quot; />  
<dubbo:consumer timeout=&quot;5000&quot;/>  
<!-- which service to consume? -->  
<dubbo:reference id=&quot;demoService&quot; interface=&quot;com.huangjie.dubbo_Service.service.IProcessData&quot; />  
</beans> 4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行  
DSC0001.jpg

这样整个调用就完成了,这样的好处是只要远程提供ip地址及端口号,以及对外调用的类,客户端就可以调用,客户端不必知道服务端的地址之类的
  而且服务端可以开多个zookeeper服务,这样如果其中一个zookeeper 服务死掉了,其他服务还能正常运行
  


  工程下载路径:http://download.iyunv.com/detail/wxwzy738/6553431
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141847-1-1.html 上篇帖子: zookeeper的介绍和部署 下篇帖子: Dubbo zookeeper 初探
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表