深入学习 Spring HttpInvoker

本文深入探讨了Spring的HttpInvoker,解释了其作为基于HTTP的Java远程调用方式的工作原理。内容涵盖HttpInvoker的远程通信协议,包括RPC、RMI和Hessian的对比,以及HttpInvoker的配置和服务端、客户端的执行流程分析。文章强调了HttpInvoker使用Java序列化带来的版本兼容问题,并提供了参考文档链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

深入学习 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.HttpInvokerProxyFactoryBean">
    <!--服务URL--&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值