一、HttpInvoker是常用的Java同构系统之间方法调用实现方案,是众多Spring项目中的一个子项目。顾名思义,它通过HTTP通信即可实现两个Java系统之间的远程方法调用,使得系统之间的通信如同调用本地方法一般。

  二、他有点类似于Java的服务远程调用RMI,但是两个基于的协议不一样。RMI是直接服务器直接的调用,需要防火墙单独放开端口。而HttpInvoker是基于http协议进行远程方法调用的。需要容器支持。

  三、RMI的实现过程参考:https://www.cnblogs.com/ll409546297/p/8948185.html

  四、我这里中间件使用的是jetty来嵌入启动的,可以参考jetty的启动方式:https://www.cnblogs.com/ll409546297/p/9338837.html

  五、HttpInvoker实现例子:

  1、服务端:

  1)提供远程调用的借口和实现类

package com.pinnet.remote;

public interface IRemoteService {

    String show();
}
package com.pinnet.remote.impl;

import com.pinnet.remote.IRemoteService;

public class RemoteServiecImpl implements IRemoteService {

    public String show() {
System.out.println("show");
return "show";
}
}

  2)服务端bean的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="remoteService" class="com.pinnet.remote.impl.RemoteServiecImpl"/>
<bean name="/remoteService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="remoteService"/>
<property name="serviceInterface" value="com.pinnet.remote.IRemoteService"/>
</bean>
</beans>

  3)有人会问相对于RMI怎么没有注册端口,因为这里是基于http协议,所以,是使用web服务的容器接口

  2、客户端:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="remoteService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8090/remoteService"/>
<property name="serviceInterface" value="com.pinnet.remote.IRemoteService"/>
</bean>
</beans>

  端口:是服务端的容器端口,接口是公用的

  3、测试:

public static void main(String[] args) {
ApplicationContext client = new ClassPathXmlApplicationContext("spring-httpinvoker-client.xml");
IRemoteService remoteService = (IRemoteService) client.getBean("remoteService");
String show = remoteService.show();
System.out.println(show);
}

  服务端:

  

  客户端:

  

  六、HttpInvoker源码分析(来至:https://charpty.com

  1、服务端

  1)服务端主入口由HttpInvokerServiceExporter实现,它的工作大致流程如下

  

  2)HttpInvokerServiceExporter实现了HttpRequestHandler,这使得其拥有处理HTTP请求的能力,按照Spring MVC的架构,它将被注册到HandlerMappingBeanNameMapping中,这设计到Spring MVC如何处理请求,可以关注我的相关文章。服务端的重要任务就是读取并解析RemoteInvocation,再返回RemoteInvocationResult,剩下的都只是标准IO流的读写。

  2、客户端
  1)客户端的实现也很好理解,主入口为HttpInvokerProxyFactoryBean, 和Spring用到的众多设计相同,该类的结构使用了模板设计方法,该类提供实现了几个模板方法,整体逻辑由父类HttpInvokerClientInterceptor的实现,主要流程如下
  

  2)我们最关心的是当我们调用接口的方法时,HttpInvoker是如何做到调用到远方系统的方法的,其实HttpInvokerProxyFactoryBean最后返回的是一个代理类(Cglib Proxy或者Jdk Proxy),我们调用接口的任何方法时,都会先执行HttpInvokerClientInterceptorinvoke()方法。

public Object invoke(MethodInvocation methodInvocation) throws Throwable {
if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
return "HTTP invoker proxy for service URL [" + this.getServiceUrl() + "]";
} else {
RemoteInvocation invocation = this.createRemoteInvocation(methodInvocation);
RemoteInvocationResult result = null; try {
result = this.executeRequest(invocation, methodInvocation);
} catch (Throwable var5) {
throw this.convertHttpInvokerAccessException(var5);
} try {
return this.recreateRemoteInvocationResult(result);
} catch (Throwable var6) {
if (result.hasInvocationTargetException()) {
throw var6;
} else {
throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() + "] failed in HTTP invoker remote service at [" + this.getServiceUrl() + "]", var6);
}
}
}
}

  六、本博客后面源码部分,部分来至https://charpty.com

 

spring之HttpInvoker的更多相关文章

  1. Java学习之路-Spring的HttpInvoker学习

    Hessian和Burlap都是基于HTTP的,他们都解决了RMI所头疼的防火墙渗透问题.但当传递过来的RPC消息中包含序列化对象时,RMI就完胜Hessian和Burlap了. 因为Hessian和 ...

  2. Spring中HttpInvoker远程方法调用总结

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

  3. 使用Spring的HttpInvoker

    Spring开发团队意识到RMI服务和基于HTTP的服务(例如Hessian和Burlap)之间的空白.一方面,RMI使用Java标准的对象序列化机制,但是很难穿透防火墙.另一方面,Hessian和B ...

  4. Spring远程调用技术<3>-Spring的HTTP Invoker

    前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙.  另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invke ...

  5. cxf+spring+数字签名开发webservice(二)

    场景         上一章中的webservice接口,因为现场正式环境的项目与外部单位网络不通,是通过前置机与外部进行数据交换,所以我们将webservice部署在前置机,在使用HttpURLCo ...

  6. Spring 4 bak

    IOC (参考<Spring企业开发>.<Spring实战 第三版  第四版>) IoC概述 1.           控制反转 2.依赖注入   控制反转:大多数情况下,想要 ...

  7. Dubbo和Spring Cloud微服务架构'

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  8. 你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  9. 听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    转自:https://baijiahao.baidu.com/s?id=1600174787011483381&wfr=spider&for=pc 微服务架构是互联网很热门的话题,是互 ...

随机推荐

  1. 配置tomcat远程debug

    Linux系统中在编辑catalina.sh文件,修改JAVA_OPTS的变量值为如下即可. JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS -Xdebug -Xrunjd ...

  2. Javascript 中 Array的 sort()和 compare()方法

    Javascript 中 Array的 sort()方法其实是把要排序的内容转化为string(调用 toString()), 然后按照字符串的第一位 ascii 码先后顺序进行比较,不是数字. 我们 ...

  3. 汇编试验四:[bx] 和 loop 的使用

    预备知识: 段前缀的使用: ffff:0~ffff:b 和 0020:0~0020:b 的数据: 一次循环的复制效果: 但是,这种方式DS的数据得修改: Source Code: assume cs: ...

  4. Python常用库之一:Numpy

    Numpy支持大量的维度数组和矩阵运算,对数组运算提供了大量的数学函数库! Numpy比Python列表更具优势,其中一个优势便是速度.在对大型数组执行操作时,Numpy的速度比Python列表的速度 ...

  5. CSS兼容性问题总结及解决方法

    css兼容问题 兼容问题 1.文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白 ...

  6. C#string byte[] base64位互相转换

    byte表示字节,byte[]则表示存放一系列字节的数组 1个字符=2个字节(byte) 1个字节=8个比特(bit) 网速上所说的1M其实是指1兆的小b,1M= 1024b/8 = 128kb 下面 ...

  7. python入门基础:文件的读写

    文件的读写操作运用广泛,无论是何种语言都会涉及到文件的输入输出. 下面简单的总结一下文件的读写: 1:open()函数 f = open('workfile', 'w') 函数 open()返回文件的 ...

  8. C#串口通讯

    本文提供一个用C#实现串口通讯实例,亲自编写,亲测可用! 开发环境:VS2008+.net FrameWork3.5(实际上2.0应该也可以) 第一步 创建一个WinForm窗体,拉入一些界面元素 重 ...

  9. Many-to-many relationships in EF Core 2.0 – Part 1: The basics

    转载这个系列的文章,主要是因为EF Core 2.0在映射数据库的多对多关系时,并不像老的EntityFramework那样有原生的方法进行支持,希望微软在以后EF Core的版本中加入原生支持多对多 ...

  10. jQuery mouseove和mouseout事件不断触发

    关于锋利的jQuery第三章结尾提示图片效果(鼠标放在图片上会出现一个大图跟随鼠标移动)实现时mouseove和mouseout事件不断触发的问题 html <ul class="bo ...