---
title: "分布式程序设计——RMI简单示例"
author: "zhoucl"
date: "2011-09-16T15:12:20Z"
tags: []
categories: ["企业架构"]
slug: "rmi"
draft: false
lastmod: "2011-09-16T15:12:20Z"
id: 1173506
url: "https://zhoucl.iteye.com/blog/1173506"
---

分布式程序设计现在用的地方可多了，但是自己一直没有去了解这方面的内容，今天兴致一来，就找了本书扫了一下盲，并且做了个简单的示例进行了实现，主要是通过java的RMI(Remote method Invoke)来做的，理论这里就不说了，直接从程序上稍作分析。

RMI应用程序编写步骤

1、定义一个Remote接口：(根据文件名称，读取文件内容)

```
public interface FileInterface extends Remote
{ 
    public String readFile(String fileName) throws RemoteException;
}
```

2、实现一个Remote接口

```
public class FileImpl extends UnicastRemoteObject implements FileInterface {
	
	protected FileImpl() throws RemoteException {
		super();
	}

	@Override
	public String readFile(String fileName) throws RemoteException {
		try {
			File file = new File(fileName);
			StringBuffer sbuffer = new StringBuffer();
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			String str ="";
			while((str = br.readLine()) != null ) {
				sbuffer.append(str + "\n");
			}
			
			return sbuffer.toString();
		} catch (Exception e) {
			System.out.println("FileImpl.readFile(): " + e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

}
```

3、编写Server端程序

```
public class FileServer {
	public static void main(String[] args) {
		int portNum = 9090;
		
		if(System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		
		try {
			LocateRegistry.createRegistry(portNum);
			FileInterface fi = new FileImpl();
			Naming.rebind("//127.0.0.1:" + portNum + "//FileServer", fi);
			System.out.println("Server Start...");
		} catch (Exception e) {
			System.out.println("FileServer: " + e.getMessage());
			e.printStackTrace();
		}
	}
}
```

4、编写Client端程序

```
public class FileClient {
	public static void main(String[] args) {
		if(args.length != 3) {
			System.out.println("Usage: java FileClient filename machinename port");
			System.exit(0);
		}
		
		try {
			String name = "//" + args[1] + ":" + args[2] +  "//FileServer";
			FileInterface fi = (FileInterface)Naming.lookup(name);
			String str = fi.readFile(args[0]);
			System.out.println(str);
		} catch (Exception e) {
			System.out.println("FileServer exception: " + e.getMessage());
			e.printStackTrace();
		}
	}
}
```

5、生成Stubs和Skeletons，启动服务端和客户端

由于在FileServer中使用的SecurityManager，因此自定义了一个security.policy文件，如下：

```
grant {
	permission java.security.AllPermission;
};
```

 执行过程，在源代码中存在于run.bat文件中：

javac org/clzps/distributed/FileInterface.java

javac org/clzps/distributed/FileImpl.java

rmic org.clzps.distributed.FileImpl

javac org/clzps/distributed/FileServer.java

javac org/clzps/distributed/FileClient.java

java -Djava.security.policy=org/clzps/distributed/security.policy org.clzps.distributed.FileServer

 

显示界面如下：

![](http://dl.iteye.com/upload/attachment/555256/e578e022-767d-338f-aac3-2d8d37a61c5b.jpg)

通过客户端访问如下：

java org.clzps.distributed.FileClient D:\\Test.txt 127.0.0.1 9090，如下所示：

![](http://dl.iteye.com/upload/attachment/555263/52250c00-cd88-3e90-8964-03cdeff5df25.jpg)