Spring HTTP Invoker使用介绍

来源:互联网 发布:槽钢抗弯强度计算软件 编辑:程序博客网 时间:2024/05/23 13:56

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源码中的注释说明:

/* <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)

<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访问到对应服务。

<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属性为服务器端配置的服务类型。

<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即可,没有特殊要求:

@Service("helloService")public class HelloServiceImpl implements HelloService {    @Override    public String hello() {         return "Hello Stevex, I am invoked by Spring HTTP Invoker!";    }}public interface HelloService {    public String hello();}


  • 客户端实现:

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

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());    }}
  • 客户端配置:

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


  • 服务端实现:

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


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

本文出自 “力量来源于赤诚的爱!” 博客,请务必保留此出处http://stevex.blog.51cto.com/4300375/1353236

0 0