深入学习 Spring HttpInvoker
远程通信协议
RPC 远程过程调用,是一个计算机通信协议,允许一台计算机的程序,调用另一台计算机的子程序。如果涉及到的软件采用面向对象编程,亦可称为远程方法调用(如 Java RMI)
RMI 一般指Java RMI (Java Remote Method Invocation) Java远程方法调用,只适用于Java程序之间的通信。而且 RMI需要开设防火墙端口
Hessian 比较精简高效,可以跨语言使用,而且协议规范公开,可以针对任意语言开发对其协议的实现(Java C++ .net python…) 数据类型有限,处理复杂对象时,速度稍慢。
HttpInvoker 是Spring提供的一种基于HTTP的Java远程调用的方法
HttpInvoker 配置
服务端
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>accountExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>accountExporter</servlet-name>
<url-pattern>/remoting/AccountService</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml
<bean id="accountService" class="remoting.service.impl.AccountServiceImpl"/>
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<!--具体服务类型-->
<property name="service" ref="accountService"/>
<!--服务接口-->
<property name="serviceInterface" value="remoting.service.AccountService"/>
</bean>
或者如下配置
web.xml
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml
<bean id="accountService" class="remoting.service.impl.AccountServiceImpl"/>
<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="remoting.service.AccountService"/>
</bean>
客户端
客户端只包含Account、以及服务接口AccountService, 但是没有AccountService的具体实现。
remoting.xml
<bean id="accountService" class="org.springframework.remoting.httpinvoker.H