Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3279
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-27 20:16
文章分类

全部博文(2)

文章存档

2015年(2)

我的朋友

分类: Java

2015-03-27 20:39:06

转: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

先把zookeeper在本地给安装好,

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

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

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

2、服务提供者的工程

a、dubbo-demo-api  定义接口

[java] view plaincopy
  1. public interface IProcessData {  
  2.     public String deal(String data);  
  3. }  
b、dubbo-demo-provider 服务提供者



[java] view plaincopy
  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配置



[html] view plaincopy
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns=""    
  3.     xmlns:xsi="" xmlns:dubbo=""    
  4.     xsi:schemaLocation="    
  5.         /spring-beans.xsd    
  6.             
  7.         /dubbo.xsd    
  8.         ">    
  9.     
  10.         
  11.     <dubbo:application name="hello-world-app" />    
  12.     
  13.         
  14.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />    
  15.     
  16.         
  17.         
  18.     <dubbo:service interface="com.huangjie.dubbo_Service.service.IProcessData"    
  19.         ref="demoService" executes="10" />    
  20.     
  21.         
  22.         
  23.     
  24.         
  25.     <bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />    
  26.     
  27. beans>   

d、启动服务



[java] view plaincopy
  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、调用类


[java] view plaincopy
  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配置



[html] view plaincopy
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns=""    
  3.     xmlns:xsi="" xmlns:dubbo=""    
  4.     xsi:schemaLocation="    
  5.         /spring-beans.xsd    
  6.             
  7.         /dubbo.xsd    
  8.         ">    
  9.     
  10.         
  11.     <dubbo:application name="consumer-of-helloworld-app" />    
  12.     
  13.         
  14.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />    
  15.     <dubbo:consumer timeout="5000"/>    
  16.         
  17.     <dubbo:reference id="demoService" interface="com.huangjie.dubbo_Service.service.IProcessData" />    
  18. beans>   
4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行



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

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


工程下载路径:

阅读(250) | 评论(0) | 转发(0) |
0

上一篇:利用Java编写简单的WebService实例

下一篇:没有了

给主人留下些什么吧!~~