1.RPC概念:
Remote procedure call protocal,远程过程调用协议,一般用来实现部署在不同机器上的系统之间的方法调用,
使得程序能够像访问本地系统资源一样,通过网络传输去访问远端系统资源 (RPC协议其实是一个规范)
主流的RPC协议有:Dubbo、Thrif、RMI、Webservice、Hessain (远程过程调用框架)
网络协议和网络IO对于调用端和服务端来说是透明(动态代理);
参考:https://www.jianshu.com/p/5b90a4e70783
2.RMI概念:
Remote Method Invocation ,远程方法调用,可以认为是RPC的java版本,使用的是JRMP(Java Remote Messageing Protocol),
JRMP是专门为java定制的通信协议,所以它是纯java的分布式解决方案
3.实现一个RMI程序步骤:
1》创建远程接口,并且继承java.rmi.Remote接口并抛出网络传输异常(throws RemoteException)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.karat.cn.rmi; 2 3 import java.rmi.Remote; 4 import java.rmi.RemoteException; 5 6 public interface ISayHello extends Remote{ 7 8 public String sayHello(String name) throws RemoteException; 9 }
2》创建一个实现类实现远程接口,并且继承:UnicastRemoteObject,实现类的构造方法抛出网络传输异常
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.karat.cn.rmi; 2 3 import java.rmi.RemoteException; 4 import java.rmi.server.UnicastRemoteObject; 5 6 7 public class SayHelloImpl extends UnicastRemoteObject implements ISayHello{ 8 9 public SayHelloImpl() throws RemoteException { 10 } 11 12 public String sayHello(String name) throws RemoteException { 13 return "Hello "+name; 14 } 15 }
3》创建服务器程序(服务端):createRegistry方法注册远程对象
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.karat.cn.rmi; 2 3 import java.net.MalformedURLException; 4 import java.rmi.AlreadyBoundException; 5 import java.rmi.Naming; 6 import java.rmi.RemoteException; 7 import java.rmi.registry.LocateRegistry; 8 9 public class HelloServer { 10 11 public static void main(String[] args) { 12 try { 13 ISayHello hello=new SayHelloImpl(); 14 15 LocateRegistry.createRegistry(8888); 16 17 Naming.bind("rmi://localhost:8888/sayHello",hello); 18 19 System.out.println("server start success"); 20 } catch (RemoteException e) { 21 e.printStackTrace(); 22 } catch (MalformedURLException e) { 23 e.printStackTrace(); 24 } catch (AlreadyBoundException e) { 25 e.printStackTrace(); 26 } 27 } 28 }
4》创建客户端程序(消费端)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.karat.cn.rmi; 2 3 import java.net.MalformedURLException; 4 import java.rmi.Naming; 5 import java.rmi.NotBoundException; 6 import java.rmi.RemoteException; 7 8 9 public class HelloClient { 10 11 public static void main(String[] args){ 12 try { 13 ISayHello hello=(ISayHello) Naming.lookup("rmi://localhost:8888/sayHello"); 14 System.out.println(hello); 15 System.out.println(hello.sayHello("hello, AAAA")); 16 } catch (NotBoundException e) { 17 e.printStackTrace(); 18 } catch (MalformedURLException e) { 19 e.printStackTrace(); 20 } catch (RemoteException e) { 21 e.printStackTrace(); 22 } 23 } 24 }