---
title: "Spring RMI"
author: "vineet"
date: "2014-05-12T18:12:11Z"
tags: []
categories: ["企业架构"]
slug: "spring-rmi"
draft: false
lastmod: "2014-05-12T18:12:11Z"
id: 2065490
url: "https://vineet.iteye.com/blog/2065490"
---

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

**1.使用RmiServiceExporter暴露服务**

 

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

 

   &lt;bean id="accountService" class="example.AccountServiceImpl"&gt;

Xml代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

1. &lt;!--其他属性，或者一个DAO对象？--&gt;
2. lt;/bean&gt;

 

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

 

Xml代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

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

 

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

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

![](http://dl.iteye.com/upload/attachment/0069/6992/f2d40731-0131-379a-9d65-2b5251609e04.gif)  
 **2.在客户端链接服务**

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

 

Java代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

1. public class SimpleObject {
2. private AccountService accountService;
3. public void setAccountService(AccountService accountService) {
4. this.accountService = accountService;
5. }
6. }

 

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

 

Xml代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

1. &lt;bean class="example.SimpleObject"&gt;
2. &lt;property name="accountService" ref="accountService"/&gt;
3. &lt;/bean&gt;
4. &lt;bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"&gt;
5. &lt;property name="serviceUrl" value="rmi://HOST:1199/AccountService"/&gt;
6. &lt;property name="serviceInterface" value="example.AccountService"/&gt;
7. &lt;/bean&gt;

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

 

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

![](http://dl.iteye.com/upload/attachment/0069/6994/bb5c8a71-cbb9-33e0-bf68-0679a34ce760.gif)

 

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

2)开发服务的实现类 

3)配置spring,配置实现类 

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

  a.远程调用服务名 

   b.提供服务的实现类 

  c.提供服务的接口 

  d.提供服务的端口 

具体的程序如下所示：

1）HelloWorld.java

 

Java代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

1. package com.ipi.rmi.test.service;
2. public interface HelloWorld
3. {
4. public String helloWorld();
5. public String sayHelloToSomeBody(String someBodyName);
6. }

 

2)HelloWorldImpl.java

 

Java代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

01. package com.ipi.rmi.test.service.impl;
02. import com.ipi.rmi.test.service.HelloWorld;
03. public class HelloWorldImpl implements HelloWorld
04. {
05. @Override
06. public String helloWorld()
07. {
08. return "Hello World!";
09. }
10. @Override
11. public String sayHelloToSomeBody(String someBodyName)
12. {
13. return "Hello World!" + someBodyName;
14. }
15. }

 

 3)Spring配置文件

 

Xml代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;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"
03. xsi:schemaLocation="
04. http://www.springframework.org/schema/beans
05. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
06. http://www.springframework.org/schema/context
07. http://www.springframework.org/schema/context/spring-context-3.0.xsd
08. http://www.springframework.org/schema/tx
09. 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"&gt;
12. &lt;!--服务端bean--&gt;
13. &lt;bean id="helloWorld" class="com.ipi.rmi.test.service.impl.HelloWorldImpl" scope="prototype"/&gt;
14. &lt;!-- 将类暴露成为一个RMI服务 --&gt;
15. &lt;bean id="springRmiTest" class="org.springframework.remoting.rmi.RmiServiceExporter"&gt;
16. &lt;!-- 服务类 --&gt;
17. &lt;property name="service" ref="helloWorld" /&gt;
18. &lt;!-- 服务名 --&gt;
19. &lt;property name="serviceName" value="helloWorldService" /&gt;
20. &lt;!-- 服务接口 --&gt;
21. &lt;property name="serviceInterface" value="com.ipi.rmi.test.service.HelloWorld" /&gt;
22. &lt;!-- 服务端口 --&gt;
23. &lt;property name="registryPort" value="9999" /&gt;
24. &lt;!-- 其他属性自己查看org.springframework.remoting.rmi.RmiServiceExporter的类,就知道支持的属性了--&gt;
25. &lt;/bean&gt;
26. &lt;/beans&gt;

 

 4）SpringRmiServer程序

 

Java代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

01. package com.ipi.rmi.test.server;
02. import org.springframework.context.ApplicationContext;
03. import org.springframework.context.support.ClassPathXmlApplicationContext;
04. public class SpringRmiServer
05. {
06. public static void main(String\[] args) {
07. //初始化工作只能运行一次;运行多次的话，会启动多个服务
08. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
09. System.out.println("Spring rmi 测试程序服务已启动");
10. }
11. }

 

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

 

**4 客户端配置与开发** 

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

 2) 配置spring 

    a.指定访问服务的地址 

    b.指定服务的接口 

1）为了在客户端有访问服务端的一个存根，所以客户端也应该有一个HelloWorld接口

2）客户端Spring配置文件如下所示：

Xml代码  [![收藏代码](http://ryxxlong.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;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"
03. xsi:schemaLocation="
04. http://www.springframework.org/schema/beans
05. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
06. http://www.springframework.org/schema/context
07. http://www.springframework.org/schema/context/spring-context-3.0.xsd
08. http://www.springframework.org/schema/tx
09. 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"&gt;
12. &lt;!--客户端--&gt;
13. &lt;bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"&gt;
14. &lt;property name="serviceUrl" value="rmi://127.0.0.1:9999/helloWorldService"/&gt;
15. &lt;property name="serviceInterface" value="com.ipi.rmi.test.service.HelloWorld"/&gt;
16. &lt;/bean&gt;
17. &lt;/beans&gt;

 客户端程序如附件中的SpringRmiClient.rar

 

需要注意的：1. 客户端必须要有实现类的接口(存根),这样才能访问后实现类的转型,引用与方法调用;

                  2.使用RMI时要注意开放防火墙相应端口;

                  3.RMI它使用 JRMP（Java Remote Messaging Protocol，Java 远程消息传递协议）作为其传输协议。当然，RMI 传输还涉及 Java 对象的序列化。 

       

**5.CORBA 和 RMI 的差异**

CORBA 运行在 IIOP 协议之上；RMI 使用 JRMP。

CORBA 是独立于语言的；RMI 是纯粹 Java 到 Java 的。

RMI 使用 JNDI 定位远程对象；CORBA 使用 CosNaming。

RMI 会将对象序列化；CORBA 则不然。

注：IIOP（Internet Inter-ORB Protocol(互联网内部对象请求代理协议)），它是一个用于CORBA 2.0及兼容平台上的协议。用来在CORBA对象请求代理之间交流的协议。Java中使得程序可以和其他语言的CORBA实现互操作性的协议。

 

有关RMI-IIOP可以参看文章：

[企业中的 RMI-IIOP 对在 IIOP 上运行 RMI 的介绍](http://www.ibm.com/developerworks/cn/java/j-rmi-iiop/)

 

- [![](http://dl2.iteye.com/upload/attachment/0069/6992/f2d40731-0131-379a-9d65-2b5251609e04-thumb.gif "点击查看原始大小图片")](http://dl2.iteye.com/upload/attachment/0069/6992/f2d40731-0131-379a-9d65-2b5251609e04.gif)
- 大小: 10.5 KB

<!--THE END-->

- [![](http://dl2.iteye.com/upload/attachment/0069/6994/bb5c8a71-cbb9-33e0-bf68-0679a34ce760-thumb.gif "点击查看原始大小图片")](http://dl2.iteye.com/upload/attachment/0069/6994/bb5c8a71-cbb9-33e0-bf68-0679a34ce760.gif)
- 大小: 9.9 KB

<!--THE END-->

- [SpringRmiServer.rar](http://dl.iteye.com/topics/download/83d96054-9ba3-3452-a960-dd27881ef410) (2.3 MB)
- 下载次数: 616

<!--THE END-->

- [SpringRmiClient.rar](http://dl.iteye.com/topics/download/224e0421-d38d-3878-97c9-ae7de28cc8e8) (2.2 MB)
- 下载次数: 587