Spring HTTP Invoker一种JAVA远程方法调用框架实现,原理与JDK的RMI基本一致,所以我们先跟其它JAVA远程方法调用实现做下简单比较。

  • RMI:使用JRMP协议(基于TCP/IP),不允许穿透防火墙,使用JAVA系列化方式,使用于任何JAVA应用之间相互调用。

  • Hessian:使用HTTP协议,允许穿透防火墙,使用自己的系列化方式,支持JAVA、C++、.Net等跨语言使用。

  • Burlap: 与Hessian相同,只是Hessian使用二进制传输,而Burlap使用XML格式传输(两个产品均属于caucho公司的开源产品)。

  • Spring HTTP Invoker: 使用HTTP协议,允许穿透防火墙,使用JAVA系列化方式,但仅限于Spring应用之间使用,即调用者与被调用者都必须是使用Spring框架的应用。

为什么使用Spring HTTP Invoker?我们可以看下Spring源码中的注释说明:

1
2
3
4
5
/* <p><b>HTTP invoker is the recommended protocol for Java-to-Java remoting.</b>
* It is more powerful and more extensible than Hessian and Burlap, at the
* expense of being tied to Java. Nevertheless, it is as easy to set up as
* Hessian and Burlap, which is its main advantage compared to RMI.
*/

Spring一定希望大家尽量使用它的产品,但实际项目中我们还是要根据需求来决定选择哪个框架,下面我们来看看Spring HTTP Invoker的使用。

既然通过是HTTP请求调用,那么客户端肯定需要一个代理用于帮忙发送HTTP请求,帮忙做对象系列化和反系列化等,Spring框架中的HttpInvokerServiceExporter类处理这些杂事;而服务器端需要一个HTTP请求处理器,帮忙处理HTTP请求已经对象系列化和反系列化工作,Spring框架中的HttpInvokerServiceExporter类就是干这活的,对于Sun JRE 6 的HTTP Server,Spring还提供了SimpleHttpInvokerServiceExporter类供选择。

  • 服务端配置:

  1. 服务声明:

    在Spring配置文件中声明一个HttpInvokerServiceExporter类的bean,共三部分:

    --服务名称(如helloExporter)

    --服务类型(如com.stevex.demo.HelloService)

    --服务实现类,一般引用其它bean(如helloService)

    1
    2
    3
    4
    5
    6
    <bean name="helloExporter"
            class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="service" ref="helloService"></property>
            <property name="serviceInterface" value="com.stevex.demo.HelloService">
            </property>
        </bean>

  2. 服务URL关联:

    在web.xml中声明一个与服务与服务名称同名的Servlet(当然这个Servlet类Spring已经提供即HttpRequestHandlerServlet,这家伙的作用就是直接把强求扔给同名的bean),然后声明servlet-mapping将其map到指定URL,这样客户就可以通过这个URL访问到对应服务。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <servlet>
            <servlet-name>helloExporter</servlet-name>
            <servlet-class>
                org.springframework.web.context.support.HttpRequestHandlerServlet
            </servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>helloExporter</servlet-name>
            <url-pattern>/remoting/HelloService</url-pattern>
        </servlet-mapping>

  • 客户端配置:

在spring bean配置文件中创建一个类HttpInvokerProxyFactoryBean的bean,指定serviceUrl属性为服务器端的服务提供的URL,serviceInterface属性为服务器端配置的服务类型。

1
2
3
4
5
6
7
<bean id="remoteHelloService"
        class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl"
            value="http://localhost:8080/demo/remoting/HelloService" />
        <property name="serviceInterface"
            value="com.stevex.demo.HelloService" />
    </bean>

  • 服务端实现:

因为服务端需要提供HTTP请求服务,而且是基于Servlet的,所以服务端需要跑在如Tomcat这样的Servlet Web容器上;服务类只要是普通的POJO即可,没有特殊要求:

1
2
3
4
5
6
7
@Service("helloService")
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello() { 
        return "Hello Stevex, I am invoked by Spring HTTP Invoker!";
    }
}

1
2
3
public interface HelloService {
    public String hello();
}

  • 客户端实现:

因为客户端依赖服务端的服务类,所以需要设置类路径依赖,可以将class文件(或者jar包)拷贝到客户端。

1
2
3
4
5
6
7
8
9
10
11
public class HelloClient {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:http-invoker-app-context.xml");
        ctx.refresh();
        HelloService helloService = ctx.getBean("remoteHelloService",
                HelloService.class);
                                                                          
        System.out.println(helloService.hello());
    }
}

全部搞定后,将服务器先跑起来,然后运行客户端程序就可以看到调用结果了,不知道性能如何,有空测试测试!

附件:http://down.51cto.com/data/2364013

本文转自sarchitect 51CTO博客,原文链接:http://blog.51cto.com/stevex/1353236,如需转载请自行联系原作者

Spring HTTP Invoker使用介绍相关推荐

  1. Spring Boot Actuator 使用介绍

    Spring Boot Actuator 使用介绍 初识 Actuator 原生端点 应用配置类 度量指标类 操作控制类 近期在看<Spring Cloud 微服务实战>,由于时间过去几年 ...

  2. Spring入门篇——第6章 Spring AOP的API介绍

    第6章 Spring AOP的API介绍 主要介绍Spring AOP中常用的API. 6-1 Spring AOP API的Pointcut.advice概念及应用 映射方法是sa开头的所有方法 如 ...

  3. Spring Http Invoker使用简介

    为什么80%的码农都做不了架构师?>>>    一.Spring HTTP Invoker简介 Spring HTTP invoker 是 spring 框架中的一个远程调用模型,执 ...

  4. Spring中的IOC介绍

    Spring中的IOC介绍 IOC(控制反转) IOC介绍 IOC是什么 IOC能做什么 Spring容器管理对象 1.maven管理依赖 2.给定容器的配置文件 3.IOC容器管理对象 4.通过容器 ...

  5. 我的Java Web之路 - Spring(1)- 介绍

    文章目录 介绍 建模 生产.装配/组装的思想 再谈容器 Spring工作模式 Spring的好处 总结 介绍 Spring在目前Java和Java Web开发里面简直就是神一样的存在,就是无处不在.这 ...

  6. 批处理框架spring batch基础知识介绍

    Table of Contents spring batch简介 Spring Batch架构介绍 Spring Batch核心概念介绍 什么是Job 什么是JobInstance 什么是JobPar ...

  7. 【Spring框架】Spring中的DI介绍

    Spring中的DI介绍 DI(Dependency Injection),即"依赖注入",组件之间依赖关系由容器在运行期决定,即容器动态的将某个依赖关系注入到组件当中.它是是 S ...

  8. (转载)spring jar包详细介绍

    spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-moc ...

  9. 《SpringCloud超级入门》Spring Boot Starter的介绍及使用《七》

    目录 Spring Boot Starter项目创建 自动创建客户端 使用 Starter 使用注解开启 Starter 自动构建 使用配置开启 Starter 自动构建 配置 Starter 内容提 ...

最新文章

  1. [转]使用 .NET Framework 2.0 在您的应用程序中支持证书
  2. 1001 字符串“水”题(二进制,map,哈希)
  3. 20211108 微分跟踪器
  4. SIGIR 2021 | 基于用户偏好感知的虚假新闻检测
  5. springboot+shiro框架中上传到服务器的图片不能查看,访问404
  6. 把Sql数据转换为业务数据的几种方法
  7. Bailian2930 加减乘除【水题】
  8. 一个程序通过窗体句柄控制另一个窗体
  9. 【CSON原创】CSS的障眼法:利用border实现图片的翻转
  10. 2022华为软件精英挑战赛-总结
  11. 创业/商业计划书10大禁忌
  12. 大胜凭德--入行选领导(转载分析)
  13. 法大大“实槌”获评《互联网周刊》“2019年度特别创新TOP50”
  14. 使用kind安装单机版k8s学习环境
  15. ML之LIME:可解释性之LIME/SP-LIME的简介、原理、使用方法、经典案例之详细攻略
  16. 程序员画手WLOP个人网站
  17. win7查看隐藏文件夹
  18. redis中的increment()方法遇到的问题记录
  19. Ins照片墙下载工具:4K Stogram for Mac
  20. 只能输入英文数字和下划线和横线的正则表达式

热门文章

  1. linux下JDK的安装
  2. RocketMQ(六):namesrv再探
  3. 整理oracle 树形查询
  4. DNS部署(四)之lvs+keepalived+bind架构高可用负载均衡DNS系统
  5. 从零开始学OpenDaylight(碳版本)之三:Hello示例
  6. Android 广播内容全知道 | 掘金技术征文
  7. 使用pt-online-schema-change 修复主从数据表数据不一致
  8. LeetCode - Department Highest Salary
  9. tomcat启动报错
  10. 5G 信令流程 — 5GC 的移动性管理(MM,Mobility Management)