Spring.Remote远程调用

转载他人的

Spring2 针对远程访问服务,提供的一个remote包。其的的是提供一套统一的远程服务发布功能。
先来看一下Spring2支持那些远程服务功能:
    1. RMI服务 
    2. Hessian或者Burlap通过HTTP远程调用服务 
    3. HTTP调用器暴露服务 

下面用一个例子,来看一下Spring2 是怎样对这些服务进行统一的封装和管理。

先看一下服务器端的源代码

public   interface  IBookService {

    Book getById(String id);

}

public   class  Book {

    
public  String name;
    
public  String id;
    
public  String author;

}
    
public   class  BookService  implements  IBookService {

    
public  Book getById(String id) {
        
return  BookStore.getById(id);
    }

}    


客户端源代码

public   class  BookQueryService {
  
private  IBookService bookService;
  
public   void  setAccountService(IBookService bookService) {
    
this .bookService  =  bookService;
  }
  
  
public  Book getBookById(String id) {
      
return  bookService.getById(id);
  }
}

// 客户端调用示例

public   static   void  main(String[] args) {

  ClassPathXmlApplicationContext context;
    context 
=   new   ClassPathXmlApplicationContext( " applicationContext.xml " );
    BookQueryService bookQueryService 
=  (BookQueryService) context.getBean( " bookQueryService " );
    Book book 
=  bookQueryService.getBookById( " 1 " );
}


使用Spring2 发布 RMI服务示例

服务器端配置:
< bean  id ="bookService"  class ="com.xmatthew.spring.remote.BookService" >
</ bean >

< bean  class ="org.springframework.remoting.rmi.RmiServiceExporter" >
    
<!--  does not necessarily have to be the same name as the bean to be exported  -->
    
< property  name ="serviceName"  value ="bookService" />
    
< property  name ="service"  ref ="bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
    
< property  name ="registryPort"  value ="1800" />
</ bean >

客户端配置:

< bean  class ="com.xmatthew.spring.remote.client.BookQueryService" >
    
< property  name ="bookService"  ref ="bookService" />
</ bean >

< bean  id ="bookService"  class ="org.springframework.remoting.rmi.RmiProxyFactoryBean" >
    
< property  name ="serviceUrl"  value ="rmi://localhost:1800/bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >


 使用Spring2 发布 基于Http的Hessian服务示例
 注: Hessian提供一种基于HTTP的二进制远程协议。它是由Caucho创建的,可以在 http://www.caucho.com 找到更多有关Hessian的信息。
 
 首为使用Hessian,需要为其配置Spring 的 DispatcherServlet
 把下面的配置加入到web.xml中

  < servlet >
    
< servlet-name > remoting </ servlet-name >
    
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
    
< load-on-startup > 1 </ load-on-startup >
</ servlet >

< servlet-mapping >
    
< servlet-name > remoting </ servlet-name >
    
< url-pattern > /remoting/* </ url-pattern >
</ servlet-mapping >
服务器端配置:
< bean  id ="bookService"  class ="com.xmatthew.spring.remote.BookService" >
</ bean >

< bean  name ="/bookService"  class ="org.springframework.remoting.caucho.HessianServiceExporter" >
  
< property  name ="service"  ref ="bookService" />
  
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >

客户端配置:

< bean  class ="com.xmatthew.spring.remote.client.BookQueryService" >
    
< property  name ="bookService"  ref ="bookService" />
</ bean >

< bean  id ="bookService"  class ="org.springframework.remoting.caucho.HessianProxyFactoryBean" >
    
< property  name ="serviceUrl"  value ="http://localhost:8080/bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >


使用Spring2 发布 基于Http的Burlap服务示例

 Burlap,它是一个基于XML的Hessian替代方案。它的配置方法和上述Hessian的一样。只要把 Hessian 换成 Burlap 就行了。
 服务器端使用:
     org.springframework.remoting.caucho.BurlapServiceExporter 发布服务
 客户端使用:
     org.springframework.remoting.caucho.BurlapProxyFactoryBean

使用Spring2 发布 基于HTTP调用器暴露服务

和使用自身序列化机制的轻量级协议Burlap和Hessian相反,Spring HTTP调用器使用标准Java序列化机制来通过HTTP暴露业务.
但其配置与Burlap和Hessian很相近

服务器端配置:
< bean  id ="bookService"  class ="com.xmatthew.spring.remote.BookService" >
</ bean >

< bean  name ="/bookService"  class ="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >
  
< property  name ="service"  ref ="bookService" />
  
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >

客户端配置:

< bean  class ="com.xmatthew.spring.remote.client.BookQueryService" >
    
< property  name ="bookService"  ref ="bookService" />
</ bean >

< bean  id ="bookService"  class ="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
    
< property  name ="serviceUrl"  value ="http://localhost:8080/bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >

猜你喜欢

转载自tangguolong99.iteye.com/blog/2086515