dubbo 学习笔记 -- consumer端

来源:互联网 发布:linux shell echo off 编辑:程序博客网 时间:2024/05/22 06:12

1、客户端配置文件 consumer.xml

     

[html] view plaincopy
  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"      
  4.  xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"     
  5.  xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.  http://www.springframework.org/schema/beans/spring-beans.xsd        
  7.  http://code.alibabatech.com/schema/dubbo          
  8.  http://code.alibabatech.com/schema/dubbo/dubbo.xsd         ">      
  9. <!-- consumer application name -->     
  10. <dubbo:application name="Frame"  />         
  11. <!-- registry address, used for consumer to discover services -->      
  12. <dubbo:registry address="multicast://224.5.6.7:1234" />         
  13. <!-- which service to consume? -->   
  14.   
  15. <dubbo:reference id="helloService" interface="merchant.shop.service.IHelloService" />      
  16. </beans>  

这里dubbo的地址需要与下面服务端的一致
客服端只有action 和 service 的接口 ,没有service 的实现类

 

需要启动的文件有下面的两个:

[java] view plaincopy
  1. package com.sitech.comm.dubbo;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  4. public class Consumer {       
  5.     public static ClassPathXmlApplicationContext context = null;  
  6.      public static ClassPathXmlApplicationContext singleton() {  
  7.             if (context == null) {  
  8.                 context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});           
  9.                 context.start();    
  10.             }  
  11.              return context;  
  12.      };  
  13. }   
  14.    


 

[java] view plaincopy
  1. package com.sitech.comm.dubbo;  
  2.    
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6.   
  7. import com.sitech.comm.log.LogWritter;  
  8.    
  9. public class ConsumerInit extends HttpServlet {  
  10.    
  11.     public void init() throws ServletException {  
  12.         try {  
  13.             System.out.println("初始化dubbo客户端");  
  14.             Consumer.singleton();  
  15.         } catch (Exception e) {  
  16.             System.out.println("初始化dubbo客户端失败");  
  17.         }  
  18.     }  
  19.   
  20. }  


在 web.xml 中添加个启动:

[html] view plaincopy
  1. <servlet>  
  2.     <servlet-name>ConsumerInit</servlet-name>  
  3.     <servlet-class>com.sitech.comm.dubbo.ConsumerInit</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5.     </servlet>    


acton 中的使用方法:

[java] view plaincopy
  1. import javax.servlet.http.HttpServletRequest;  
  2. import javax.servlet.http.HttpServletResponse;  
  3.   
  4. import merchant.shop.service.IHelloService;  
  5.   
  6. import org.springframework.web.servlet.ModelAndView;  
  7. import org.springframework.web.servlet.mvc.AbstractController;  
  8.   
  9. import com.sitech.comm.dubbo.Consumer;  
  10.   
  11. public class TestAction extends AbstractController{  
  12.   
  13.     @Override  
  14.     protected ModelAndView handleRequestInternal(HttpServletRequest arg0,  
  15.             HttpServletResponse arg1) throws Exception {  
  16.         IHelloService helloService = (IHelloService) Consumer.singleton().getBean("helloService");  
  17.         helloService.sayHello();  
  18.         return null;  
  19.     }  
  20.   
  21. }  


sevice 接口如下:

[java] view plaincopy
  1. package merchant.shop.service;  
  2.   
  3. public interface IHelloService {  
  4.     public String sayHello();  
  5. }  

0 0
原创粉丝点击