RPC是遠程過程調用的簡稱,廣泛應用在大規模分佈式應用中,作用是有助於系統的垂直拆分,使系統更易拓展。Java中的RPC框架比較多,各有特色,廣泛使用的有RMI、Hessian、Dubbo等。RPC還有一個特點就是能夠跨語言。
1、RMI(遠程方法調用)
JAVA自帶的遠程方法調用工具,不過有一定的侷限性,畢竟是JAVA語言最開始時的設計,後來很多框架的原理都基於RMI,RMI的使用如下:
對外接口
1 2 3 4 5 | <span style= "font-size:12px;" >public interface IService extends Remote { public String queryName(String no) throws RemoteException; }</span> |
服務實現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; // 服務實現 public class ServiceImpl extends UnicastRemoteObject implements IService { /** */ private static final long serialVersionUID = 682805210518738166L; /** * @throws RemoteException */ protected ServiceImpl() throws RemoteException { super (); } /* (non-Javadoc) * @see com.suning.ebuy.wd.web.IService#queryName(java.lang.String) */ @Override public String queryName(String no) throws RemoteException { // 方法的具體實現 System.out.println( "hello" + no); return String.valueOf(System.currentTimeMillis()); } } RMI客戶端 [java] view plain copy import java.rmi.AccessException; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; // RMI客戶端 public class Client { public static void main(String[] args) { // 註冊管理器 Registry registry = null ; try { // 獲取服務註冊管理器 registry = LocateRegistry.getRegistry( "127.0.0.1" , 8088 ); // 列出所有註冊的服務 String[] list = registry.list(); for (String s : list){ System.out.println(s); } } catch (RemoteException e) { } try { // 根據命名獲取服務 IService server = (IService) registry.lookup( "vince" ); // 調用遠程方法 String result = server.queryName( "ha ha ha ha" ); // 輸出調用結果 System.out.println( "result from remote : " + result); } catch (AccessException e) { } catch (RemoteException e) { } catch (NotBoundException e) { } } } RMI服務端 [java] view plain copy import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; // RMI服務端 public class Server { public static void main(String[] args) { // 註冊管理器 Registry registry = null ; try { // 創建一個服務註冊管理器 registry = LocateRegistry.createRegistry( 8088 ); } catch (RemoteException e) { } try { // 創建一個服務 ServiceImpl server = new ServiceImpl(); // 將服務綁定命名 registry.rebind( "vince" , server); System.out.println( "bind server" ); } catch (RemoteException e) { } } } |
2、Hessian(基於HTTP的遠程方法調用)
基於HTTP協議傳輸,採用的是二進制RPC協議,非常輕量級 ,且速度較快。在性能方面還不夠完美,負載均衡和失效轉移依賴於應用的負載均衡器,Hessian的使用則與RMI類似,區別在於淡化了Registry的角色,通過顯示的地址調用,利用HessianProxyFactory根據配置的地址create一個代理對象,另外還要引入Hessian的Jar包。
3、Dubbo(淘寶開源的基於TCP的RPC框架)
基於Netty的高性能RPC框架,是阿里巴巴開源的,總體原理如下:
4、Thrift 是由 Facebook 開源的一個 RPC 框架,現在已經掛在 apache.org 下了。主要的幾個好處:
1. 支持非常多語言,包括在 WEB 開發中很常用的 PHP,以及最重要的 C++/Python/Java 等 WEB後端常用語言,當然,還包括很 cool 的 Ruby、Erlang。
2. 完整的 RPC 框架實現,用腳本生成通訊相關的框架代碼,開發者只需要集中精力處理好 業務邏輯。比如搭建一個 Hello World Service 只需要幾分鐘。
3.擁有被 Facebook、Last.fm 等不少大規模應用驗證過的高性能和可用性。
5、當然,還有Hetty,它是一款構建於Netty和Hessian基礎上的高性能的RPC框架。