您的位置:首页 > 理论基础 > 计算机网络

Spring中HttpInvoker远程方法调用总结

2016-06-08 19:18 543 查看
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();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: