Spring2提供的Remote包学习笔记

本文介绍如何使用Spring2框架发布和管理多种类型的远程服务,包括RMI服务、基于HTTP的Hessian服务、Burlap服务及HTTP调用器暴露的服务,并提供了详细的服务器端和客户端配置示例。
原文出自:http://www.blogjava.net/xmatthew/archive/2008/11/02/238208.html    作者:xmatthew

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值