Spring HTTP Invoker

转自http://stevex.blog.51cto.com/4300375/1353236      sarchitect 的BLOG



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());
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值