`
iluoxuan
  • 浏览: 577237 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring rmi远程调用

    博客分类:
  • java
 
阅读更多

1:以前用jmi发布服务,实现分布式的一种方式,远程调用,据说rmi的性能是最好的,维护起来有点麻烦,现在改Hessian

各种远程调用的比较:

http://www.cnblogs.com/jifeng/archive/2011/07/20/2111183.html

spring 文档中关于远程调用的

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/remoting.html#remoting-caucho-protocols

转:http://ryxxlong.iteye.com/blog/1563359

使用Spring的RMI支持,你可以通过RMI基础设施透明的暴露你的服务。设置好Spring的RMI支持后,你会看到一个和远程EJB接口类似的配置,只是没有对安全上下文传递和远程事务传递的标准支持。当使用RMI调用器时,Spring对这些额外的调用上下文提供了钩子,你可以在此插入安全框架或者定制的安全证书。

1.使用RmiServiceExporter暴露服务

 

   使用RmiServiceExporter,我们可以把AccountService对象的接口暴露成RMI对象。可以使用 RmiProxyFactoryBean 或者在传统RMI服务中使用普通RMI来访问该接口。RmiServiceExporter 显式地支持使用RMI调用器暴露任何非RMI的服务。当然,我们首先需要在Spring容器中设置我们的服务:

 

   <bean id="accountService" class="example.AccountServiceImpl">

Xml代码  收藏代码
  1.      
  2. <!--其他属性,或者一个DAO对象?-->  
  3.   
  4. lt;/bean>  

 

 然后我们要使用RmiServiceExporter来暴露我们的服务:

 

Xml代码  收藏代码
  1. <bean class="org.springframework.remoting.rmi.RmiServiceExporter">  
  2.     <!-- 不一定要与要输出的bean同名-->  
  3.     <property name="serviceName" value="AccountService"/>  
  4.     <property name="service" ref="accountService"/>  
  5.     <property name="serviceInterface" value="example.AccountService"/>  
  6.     <!--  默认为1199-->  
  7.     <property name="registryPort" value="1199"/>  
  8. </bean>  

 

   本例中,服务绑定在 rmi://HOST:1199/AccountService。在客户端我们将使用这个URL来链接到服务。

     RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。 



 2.在客户端链接服务

我们的客户端是一个使用AccountService来管理account的简单对象:

 

Java代码  收藏代码
  1. public class SimpleObject {  
  2.   
  3.     private AccountService accountService;  
  4.   
  5.     public void setAccountService(AccountService accountService) {  
  6.         this.accountService = accountService;  
  7.     }  
  8. }  

 

 为了把服务连接到客户端上,我们将创建一个单独的Spring容器,包含这个简单对象和链接配置位的服务:

 

Xml代码  收藏代码
  1. <bean class="example.SimpleObject">  
  2.     <property name="accountService" ref="accountService"/>  
  3. </bean>  
  4.   
  5. <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  6.     <property name="serviceUrl" value="rmi://HOST:1199/AccountService"/>  
  7.     <property name="serviceInterface" value="example.AccountService"/>  
  8. </bean>  

 客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。

 

通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。


 

3.服务端配置与开发 
1)开发服务的接口 

2)开发服务的实现类 

3)配置spring,配置实现类 

4)配置spring,注册rmi服务,其中需要说明 

  a.远程调用服务名 

   b.提供服务的实现类 

  c.提供服务的接口 

  d.提供服务的端口 

具体的程序如下所示:

1)HelloWorld.java

 

Java代码  收藏代码
  1. package com.ipi.rmi.test.service;  
  2.   
  3. public interface HelloWorld  
  4. {     
  5.     public String helloWorld();  
  6.   
  7.     public String sayHelloToSomeBody(String someBodyName);  
  8.   
  9. }  

 

2)HelloWorldImpl.java

 

Java代码  收藏代码
  1. package com.ipi.rmi.test.service.impl;  
  2.   
  3. import com.ipi.rmi.test.service.HelloWorld;  
  4.   
  5. public class HelloWorldImpl implements HelloWorld  
  6. {  
  7.   
  8.     @Override  
  9.     public String helloWorld()  
  10.     {  
  11.         return "Hello World!";  
  12.     }  
  13.   
  14.     @Override  
  15.     public String sayHelloToSomeBody(String someBodyName)  
  16.     {  
  17.         return "Hello World!" + someBodyName;    
  18.     }  
  19.   
  20. }  

 

 3)Spring配置文件

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  3.     xsi:schemaLocation="  
  4. http://www.springframework.org/schema/beans   
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  6. http://www.springframework.org/schema/context   
  7. http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  8. http://www.springframework.org/schema/tx   
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
  10. http://www.springframework.org/schema/aop    
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  12.    <!--服务端bean-->   
  13.     <bean id="helloWorld" class="com.ipi.rmi.test.service.impl.HelloWorldImpl" scope="prototype"/>  
  14.     
  15.   <!-- 将类暴露成为一个RMI服务 -->  
  16.   <bean id="springRmiTest" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  17.         <!-- 服务类 -->  
  18.         <property name="service" ref="helloWorld" />  
  19.         <!-- 服务名 -->  
  20.         <property name="serviceName" value="helloWorldService" />  
  21.         <!-- 服务接口 -->  
  22.         <property name="serviceInterface" value="com.ipi.rmi.test.service.HelloWorld" />  
  23.         <!-- 服务端口 -->  
  24.         <property name="registryPort" value="9999" />  
  25.         <!-- 其他属性自己查看org.springframework.remoting.rmi.RmiServiceExporter的类,就知道支持的属性了-->  
  26.     </bean>  
  27. </beans>  

 

 4)SpringRmiServer程序

 

Java代码  收藏代码
  1. package com.ipi.rmi.test.server;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class SpringRmiServer  
  7. {  
  8.     public static void main(String[] args) {  
  9.         //初始化工作只能运行一次;运行多次的话,会启动多个服务  
  10.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  11.         System.out.println("Spring rmi 测试程序服务已启动");  
  12.     }  
  13. }  

 

服务器端程序如附件中的SpringRmiServer.rar

 

4 客户端配置与开发 

 1) 将服务端的接口进行打包(注:不需要实现类,只是客户端访问服务端的一个存根) 

 2) 配置spring 

    a.指定访问服务的地址 

    b.指定服务的接口 

1)为了在客户端有访问服务端的一个存根,所以客户端也应该有一个HelloWorld接口
2)客户端Spring配置文件如下所示:
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  3.     xsi:schemaLocation="  
  4. http://www.springframework.org/schema/beans   
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  6. http://www.springframework.org/schema/context   
  7. http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  8. http://www.springframework.org/schema/tx   
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
  10. http://www.springframework.org/schema/aop    
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  12.     <!--客户端-->   
  13.     <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">   
  14.         <property name="serviceUrl" value="rmi://127.0.0.1:9999/helloWorldService"/>   
  15.         <property name="serviceInterface" value="com.ipi.rmi.test.service.HelloWorld"/>   
  16.     </bean>   
  17. </beans>  
 客户端程序如附件中的SpringRmiClient.rar
 
需要注意的:1. 客户端必须要有实现类的接口(存根),这样才能访问后实现类的转型,引用与方法调用;
                  2.使用RMI时要注意开放防火墙相应端口;
                  3.RMI它使用 JRMP(Java Remote Messaging Protocol,Java 远程消息传递协议)作为其传输协议。当然,RMI 传输还涉及 Java 对象的序列化。
分享到:
评论

相关推荐

    spring RMI 远程接口调用

    Spring RMI(Remote Method Invocation)远程接口调用是Spring框架提供的一个特性,它允许你在分布式环境中调用对象的方法,使得应用程序能够跨越网络边界操作远程对象。这个技术在大型企业级应用中尤其有用,因为它...

    RMI远程调用

    **RMI远程调用详解** 远程方法调用(Remote Method Invocation,简称RMI)是Java平台提供的一种机制,它允许一个Java对象调用另一个在不同Java虚拟机(JVM)上的对象的方法。RMI是Java分布式计算的核心技术,主要...

    spring远程调用简单实例

    Spring支持多种远程调用协议,如RMI(Remote Method Invocation)、Hessian、 Burlap以及HTTP Invoker等。这些协议允许我们跨越网络边界,像调用本地方法一样调用远程服务。 在本实例中,我们关注的是HTTP Invoker...

    三种方式实现java远程调用(rmi),绝对可用

    在提供的压缩包文件中,"三种方式(原始方式_spring_jndi)实现java远程调用(rmi)"包含了相关的示例代码,帮助开发者理解并实践这三种RMI实现方法。在MyEclipse或其他Java开发环境中导入这些代码,可以进行调试和...

    java spring+rmi 的远程调用例子

    这个压缩包文件“Spring+RMI”很可能包含了实现这样一个远程调用示例的所有必要组成部分,包括配置文件、接口定义、服务器端实现以及客户端调用代码。 首先,我们来理解一下Java RMI。RMI是Java提供的一种原生的...

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

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

    spring rmi 源码

    Spring RMI(Remote Method Invocation)...通过分析服务端和客户端的源码,我们可以深入理解如何在Spring框架下实现和使用RMI远程调用,这对于任何希望在分布式系统中利用Spring的开发者来说都是一份宝贵的参考资料。

    Spring RMI小例子

    Spring RMI(Remote Method Invocation)是Java平台上的远程方法调用技术,结合Spring框架,能够帮助开发者轻松地创建分布式应用程序。在这个"Spring RMI小例子"中,我们将深入理解Spring如何简化RMI的使用,以及...

    Spring RMI

    - **透明性**:远程调用与本地调用在语法上无差异,提高了开发效率。 - **自动异常处理**:Spring RMI会自动处理RMI相关的异常,如网络故障、远程对象不存在等。 - **事务支持**:Spring可以为RMI调用提供事务管理,...

    使用spring远程调用服务端接口实现WebService功能

    这就是Spring框架实现远程调用服务端接口以实现WebService功能的基本流程。由于HttpInvoker基于HTTP,它天生具备良好的网络穿透能力,适合于分布式系统中的跨网络通信。同时,由于使用了Java序列化,它的性能相对较...

    Spring远程调用使用http方式

    在IT行业中,Spring框架是Java开发中的一个核心组件,它提供了丰富的功能,...不过,需要注意的是,HTTP远程调用可能会引入额外的网络延迟,因此在性能敏感的场景下,可能需要考虑其他的远程调用技术,如RMI或gRPC。

    spring rmi应用

    然而,需要注意的是,虽然Spring RMI简化了远程调用,但在网络延迟、并发控制、安全性等方面仍需考虑。例如,使用SSL进行安全传输,或者通过负载均衡器来分发请求,提高系统的可用性和可扩展性。 总之,Spring RMI...

    SpringRMI小例子

    在这个"SpringRMI小例子"中,我们将深入探讨如何利用Spring框架来增强RMI的功能,使其更加灵活和易于管理。 首先,我们需要理解Spring框架在RMI中的角色。Spring提供了对RMI的高级抽象,通过其`org.springframework...

    Spring-RMI (RMI调用, HTTP调用)

    而Spring-RMI(Remote Method Invocation)和HTTP调用是Spring框架中两种不同的远程通信机制,它们使得分布式系统间的交互变得简单易行。 **Spring-RMI** RMI是Java中的一种远程方法调用技术,允许对象在不同JVM...

    spring RMI简单例子

    Spring RMI(Remote Method Invocation)是Java平台上的远程方法调用技术,结合Spring框架的特性,使得在分布式系统中调用远程服务变得更加便捷。在这个简单的例子中,我们将深入理解Spring RMI的工作原理以及如何...

    RMI远程调用DEMO....

    Java Remote Method Invocation (RMI) ...虽然现代的Java开发中,RMI可能不是首选的分布式计算技术(例如,Spring框架的远程服务支持更为流行),但对于理解分布式系统原理和Java网络编程,RMI仍然是一个重要的学习点。

    spring RMI 实用分享

    远程接口定义了可供远程调用的方法,远程对象实现了这些接口,并在服务器端运行,而RMIServer则负责处理客户端的请求并调用远程对象的方法。 在Spring框架中,使用RMI变得更加简单。Spring提供了...

    spring rmi 小例子

    1. **接口定义**:在RMI中,我们需要定义一个公共接口,该接口声明了可被远程调用的方法。这个接口应该标记为`@Remote`,例如: ```java public interface MyRemoteService extends Remote { String doSomething...

    spring rmi 多接口配置 调用

    而RMI(Remote Method Invocation,远程方法调用)是Java中用于分布式计算的技术,使得运行在不同JVM上的对象可以互相调用方法。本篇文章将深入探讨如何在Spring框架中配置和调用RMI的多个接口。 首先,我们需要...

    Spring-RMI.rar_spring rmi

    4.3 错误处理:远程调用可能会遇到网络中断、超时等问题,应适当地处理异常。 总结,Spring整合RMI提供了一种方便、灵活的方式来实现分布式系统中的远程方法调用。通过"SpringRMIClient"和"SpringRMIServer"示例,...

Global site tag (gtag.js) - Google Analytics