RPC框架一,RMI远程调用实例
网上找了好久关于RMI调用的实例,大多都是本地调用的,远程调用的示例很少,所以自己整理一版。
首先 从server端开始:
服务端###############
具体步骤:
1,写个RMI接口 MyRmiService.java
2,写个RMI接口实现 MyRmiServiceImpl.java
3,写个RMI服务端程序 RmiSeverTest.java(main方法,注意此类需要和RMI接口实现在同个包下,或者子包中,原因是接口实现的无参构造器是protected权限)
具体代码:
MyRmiService.java--------------------------------------------------------- package com.rmi.service; import java.rmi.Remote; import java.rmi.RemoteException; public interface MyRmiService extends Remote { public String sayHello () throws RuntimeException, RemoteException; }
MyRmiServiceImpl.java----------------------------------------------------------------------------------
package com.rmi.service.impl; import com.rmi.service.MyRmiService; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class MyRmiServiceImpl extends UnicastRemoteObject implements MyRmiService { private static final long serialVersionUID = 1L; protected MyRmiServiceImpl() throws RemoteException { super(); } @Override public String sayHello() throws RemoteException { return "Hello world"; } } RmiSeverTest.java----------------------------------------------------------------------
package com.rmi.service.impl; import com.rmi.service.MyRmiService; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; public class RmiSeverTest { private static final String START_FAIL = "MyRmiService start error"; private static final String START_SUCC = "MyRmiService start success"; private static final int RMI_SERVER_PORT = 1099; private static final String RMI_SERVER_NAME ="rmi://10.206.66.166:1099/sayHello"; private static final String START_INFO =",waste time(ms) "; public static void main(String[] args) { boolean runSucc = true; //创建服务 long begintime = System.currentTimeMillis(); try { LocateRegistry.createRegistry(RMI_SERVER_PORT); // 以下是向LocateRegistry注册(绑定/重绑定)RMI Server实现。 MyRmiService myRmiService = new MyRmiServiceImpl(); // 通过java 名字服务技术,可以讲具体的RMI Server实现绑定一个访问路径。注册到LocateRegistry中 Naming.rebind(RMI_SERVER_NAME, myRmiService); } catch (Exception e) { runSucc = false; e.printStackTrace(); } long endtime = System.currentTimeMillis(); System.out.println((runSucc?START_SUCC:START_FAIL)+START_INFO +(endtime -begintime)); } }
启动main方法,则客户端正常启动了
现在我们就可以远程调用了,但是在写客户端代码之前我们需要准备客户端代理,
远程客户端############
生成客户端代理class MyRmiServiceImpl_Stub.class----->具体步骤如下:
当前项目的target\classes目录下(C:\Users\zxx\IdeaProjects\testProject\target\classes)
执行cmd命令到控制台
在控制台执行命令 rmic com.rmi.service.MyRmiServiceImpl 生成MyRmiServiceImpl_Stub.class文件
命令行提示如下:
将项目打包为rmi-sub-1.0.0.jar (去除其他文件,仅保留必须的MyRmiService.class,MyRmiServiceImpl_Stub.class两个文件)
客户端导入此jar包,开始书写客户端调用类 ClientRmiTest.java
package com.rmi.test; import com.rmi.service.MyRmiService; import javax.naming.NamingException; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; public class ClientRmiTest { public static void main(String[] args) throws NamingException, RemoteException, NotBoundException, MalformedURLException { System.out.println( ((MyRmiService) Naming.lookup("rmi://10.206.66.166/sayHello")).sayHello()); } }
执行main方法远程调用RMI服务