标签:java rmi webservice 分布式
RMI就是远程方法调用的简写。顾名思义,就是让一台机器上的对象调用另外一个机器上的对象。RMI的用法非常简单,首先是服务端定义一个接口(接口要扩展Remote接口),再实现这个接口(要扩展UnicastRemoteObject),再绑定到Naming静态类中。客户端通过Naming获取一个远程对象,就可以像普通的对象一样调用远程对象了。RMI中有个Stub类,它的作用就是代理服务器的接口对象,负责将方法的调用转换成网络请求发送给服务器,再从服务器返回对象进行解码。在JDK1.5中,Stub类会自动生成。// 第一步设计接口。
public interface Hello extends Remote {
public String hello() throws RemoteException;
}
// 第二步实现接口。
public class HelloImpl extends UnicastRemoteObject implements Hello {
public String hello() {
return "hello";
}
}
// 第三步开启服务。
public class Server {
public static void main(String[] argv) {
Hello hello = new HelloImpl();
Registry registry = LocateRegistry.getRegistry(8888);
registry.rebind("hello", hello);
}
}
// 第四步使用。
public class Client {
public static void main(String[] argv) {
Hello hello = Naming.lookup("rmi://127.0.0.1:8888/hello");
String s = hello.hello();
}
}
标签:java rmi webservice 分布式
原文地址:http://blog.csdn.net/caipeichao2/article/details/38497363