Spring中HttpInvoker远程方法调用总结

Spring为各种远程访问技术的集成提供了工具类。Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易。目前,Spring支持四种远程技术:

  • 远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
  • Spring的HTTP调用器。Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
  • Hessian。通过 HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
  • Burlap。 Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如 BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
  • JAX RPC。Spring通过JAX-RPC为远程Web服务提供支持。
  • JMS(待实现)
目前就用到过了HttpInvoker,所以对配置进行总结下,为下一次开发奠定基础。

首先分为远程调用两部分,一个服务端,另一个是客户端。
1、定义一个接口和接口的实现类,用于客户端发请求调用的
IRemoteService.java
public interface IRemoteService {
  public void startRmote();
}

RemoteServiceImpl.java
public class RemoteServiceImpl implements IRemoteService {

	@Override
	public void startRmote() {
		// TODO Auto-generated method stub
		System.out.println("this is remote --------------------------------");
	}

}

2、在web.xml下建立一个testRemote-servlet.xml文件。配置暴露给客户端的接口实现类信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean name="remote" class="com.frame.rmote.RemoteServiceImpl" />
	<bean name="/remoteservice"
		class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="remote" />
		<property name="serviceInterface" value="com.frame.rmote.IRemoteService" />
	</bean>
	
</beans>

3、在服务端的web.xml配置上客户端要访问的请求地址,
web.xml
       <servlet>  
        <servlet-name>testRemote</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
             <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
       <servlet-name>testRemote</servlet-name>  
       <url-pattern>/testRemoting/*</url-pattern>   
    </servlet-mapping>
服务端配置完毕


客户端配置:

1、在客户端定义一个与服务端一样的接口
IRemoteService.java
public interface IRemoteService {
  public void startRmote();
}

2、在applicationContext.xml中配置调用服务端的接口
	<bean id="iRemoteTest"
		class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost:8080/Frame/testRemoting/remoteservice" />
		<property name="serviceInterface" value="mf.newrise.test.IRemoteService" />
	</bean>


测试:
public class TestRemote {


    public static void main(String[] args) {

       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

       IRemoteService service = (IRemoteService) applicationContext.getBean("iRemoteTest")

        service.startRemote();

    }

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值