代码改变世界

Spring中HttpInvoker远程方法调用总结

2017-08-02 19:48  tlnshuju  阅读(464)  评论(0编辑  收藏  举报

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

眼下,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,所以对配置进行总结下,为下一次开发奠定基础。

首先分为远程调用两部分。一个服务端。还有一个是client。
1、定义一个接口和接口的实现类,用于client发请求调用的
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文件。

配置暴露给client的接口实现类信息

<?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配置上client要訪问的请求地址,
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>
服务端配置完成


client配置:

1、在client定义一个与服务端一样的接口
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();

    }

}