`

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>
分享到:
评论

相关推荐

    Spring 实现远程访问详解——rmi

    1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,Spring支持两个传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    spring-remote-service-example:一个示例项目,用于展示如何使用 JSON 反序列化通过 HTTP 在隔离的 Spring 容器之间进行(远程)服务调用

    弹簧远程服务示例 一个示例项目,用于展示如何通过使用 JSON 反序列化在隔离的 Spring 容器之间通过 HTTP 进行(远程)服务调用。

    java面试笔试资料包括JAVA基础核心知识点深度学习Spring面试题等资料合集.zip

    RPC (Remote Procedure Call)即远程过程调用.doc Spring 面试问题 TOP 50(干货推荐收藏必备).doc springboot常见面试题.doc svn和git的区别及适用场景.doc ZooKeeper.doc 为什么分布式一定要有Redis.doc 分布式、...

    spring security3.2.0

    Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的...Transparent authentication context propagation for Remote Method Invocation and HttpInvoker (一个Spring远程调用协议)

    hessian-demo和hessian与spring整合demo.doc

    Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI...采用的是二进制RPC(Remote Procedure Call Protocol,远程过程调用协议)协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

    DWR.xml配置文件说明书(含源码)

    如果你想远程调用一个creator的静态方法,并且creator是new类型.因为调用远程bean的方法前DWR不会检测将要执行的方法是不是静态方法,如果是静态方法那么creator就不用创建.这种机制可以适用任何类型的creator,但new...

    dubbo_demo.zip

    需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。 RPC是一个泛化的概念,严格来说一切远程过程调用手段都属于RPC范畴。各种开发语言都有自己的RPC框架。Java中的RPC框架比较多,广泛使用的有RMI...

    java开源包1

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    redis各个注解使用demo方法

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI编写、支持网络、可基于内存亦可持久化的日志型、Key-Value,并提供多种语言的API。好了,其它的就不介绍了。 @Cacheable  此注解表明在...

    java开源包101

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包10

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包11

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包2

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包3

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包6

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包5

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包4

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包8

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

    java开源包7

    brap(Java远程调用框架 BRAP) 一个Java远程调用框架,它将原生Java对象序列化压缩装入HTTP中。它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准...

Global site tag (gtag.js) - Google Analytics