dubbo 学习笔记 -- provider端

服务端的配置文件:    provider.xml

[html] 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"     
  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. <!-- Application name -->      
  10. <dubbo:application name="Frame"  />         
  11. <!-- registry address, used for service to register itself -->      
  12. <dubbo:registry address="multicast://224.5.6.7:1234" />         
  13. <!-- expose this service through dubbo protocol, through port 20880 -->      
  14. <dubbo:protocol name="dubbo" port="20880" />         
  15. <!-- which service interface do we expose? -->      
  16. <dubbo:service interface="merchant.shop.service.IHelloService" ref="helloService" />         
  17.     <!-- bean配置 -->  
  18.     <bean id="helloService"  
  19.         class="merchant.shop.service.impl.HelloServiceImpl">  
  20.     </bean>    
  21. </beans>   


此处interface的地址要与consumer端的一致,所以在服务端工程中新建了和客户端工程一样的路径来保存service

spring 配置文件 : providerApplicationContext.xml

[html] 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"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.   
  6.     <!-- spring 与 ibatis 衔接 -->  
  7.     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  8.         <property name="configLocation" value="provider-sql-map-config.xml"></property>  
  9.         <property name="dataSource" ref="dataSource"/>  
  10.     </bean>  
  11.     <!-- 数据源基本配置 -->   
  12.     <bean id="dataSource"  
  13.         class="org.springframework.jndi.JndiObjectFactoryBean">  
  14.         <property name="jndiName">  
  15.             <value>java:/comp/env/test</value>  
  16.         </property>  
  17.     </bean>  
  18.     <!-- 事务配置 -->  
  19.      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  20.     <property name="dataSource" ref="dataSource"></property>  
  21.     </bean>  
  22.   
  23. <!-- 声明式事务管理 -->  
  24.     <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">  
  25.         <property name="transactionManager" ref="transactionManager"></property>  
  26.         <property name="transactionAttributes">  
  27.             <props>  
  28.                 <prop key="add*">PROPAGATION_REQUIRED</prop>              
  29.                 <prop key="edit*">PROPAGATION_REQUIRED</prop>  
  30.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>  
  31.             </props>  
  32.         </property>  
  33.     </bean>   
  34. </beans>  

服务端需要启动的两个文件如下 :

[java] view plain copy
 
  1. package com.sitech.comm.dubbo;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;    
  4.   
  5. public class Provider {         
  6.     public static void init() throws Exception {           
  7.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});           
  8.         context.start();    
  9.         singleton();  
  10.     }  
  11.     public static ApplicationContext context = null;  
  12.      public static ApplicationContext singleton() {  
  13.             if (context == null) {  
  14.                 context = new ClassPathXmlApplicationContext(new String[] {"providerApplicationContext.xml"});           
  15.             }  
  16.              return context;  
  17.      };  
  18. }   
[java] view plain copy
 
  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 ProviderInit extends HttpServlet {  
  10.    
  11.     public void init() throws ServletException {  
  12.         try {  
  13.             System.out.println("初始化dubbo服务端");  
  14.             Provider.init();  
  15.         } catch (Exception e) {  
  16.             System.out.println("初始化dubbo服务端失败");  
  17.         }  
  18.     }  
  19.   
  20. }  


web.xml 中增加启动如下 :

[html] view plain copy
 
  1. <servlet>  
  2.         <servlet-name>ProviderInit</servlet-name>  
  3.         <servlet-class>  
  4.             com.sitech.comm.dubbo.ProviderInit  
  5.         </servlet-class>  
  6.         <load-on-startup>1</load-on-startup>  
  7.     </servlet>  


consumer客户端就可以远程调用另一个工程的服务了

这里出问题,一般都是配置文件的问题,如数据库的连接,spring 与 ibatis 衔接 

原文地址:https://www.cnblogs.com/toSeeMyDream/p/5801309.html