dubbo 学习笔记

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

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=""xmlns:xsi=""xmlns:dubbo=""xsi:schemaLocation=""><!– Application name –><dubbo:application name="Frame" /><!– registry address, used for service to register itself –><dubbo:registry address="multicast://224.5.6.7:1234" /><!– expose this service through dubbo protocol, through port 20880 –><dubbo:protocol name="dubbo" port="20880" /><!– which service interface do we expose? –><dubbo:service interface="merchant.shop.service.IHelloService" ref="helloService" /><!– bean配置 –><bean id="helloService"class="merchant.shop.service.impl.HelloServiceImpl"></bean> </beans>

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

spring 配置文件: providerApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xsi:schemaLocation=" http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!– spring 与 ibatis 衔接 –><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation" value="provider-sql-map-config.xml"></property><property name="dataSource" ref="dataSource"/></bean><!– 数据源基本配置 –> <bean id="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName"><value>java:/comp/env/test</value></property></bean><!– 事务配置 –> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!– 声明式事务管理 –><bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"><property name="transactionManager" ref="transactionManager"></property><property name="transactionAttributes"><props><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="edit*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean> </beans>

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

package com.sitech.comm.dubbo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void init() throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});context.start();singleton();}public static ApplicationContext context = null; public static ApplicationContext singleton() {if (context == null) {context = new ClassPathXmlApplicationContext(new String[] {"providerApplicationContext.xml"});}return context; };} package com.sitech.comm.dubbo;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import com.sitech.comm.log.LogWritter;public class ProviderInit extends HttpServlet {public void init() throws ServletException {try {System.out.println("初始化dubbo服务端");Provider.init();} catch (Exception e) {System.out.println("初始化dubbo服务端失败");}}}

web.xml 中增加启动如下 :

<servlet><servlet-name>ProviderInit</servlet-name><servlet-class>com.sitech.comm.dubbo.ProviderInit</servlet-class><load-on-startup>1</load-on-startup></servlet>

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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

有事者,事竟成;破釜沉舟,百二秦关终归楚;苦心人,

dubbo 学习笔记

相关文章:

你感兴趣的文章:

标签云: