服务端的配置文件: provider.xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="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 ">
- <!-- 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
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans 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>
服务端需要启动的两个文件如下 :
[java] view plain copy
- 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;
- };
- }
[java] view plain copy
- 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 中增加启动如下 :
[html] view plain copy
- <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 衔接
时间: 2024-10-17 13:49:32