rpc是遠端過程呼叫,可做到像本地一樣呼叫遠端服務,是一種程序間通訊方式。
1.本地函式呼叫
本地函式即為常用的函式方法,通過傳入引數或者直接呼叫即可得出輸出,操作過程在同乙個程序中進行。
2.socket
socket可支援在不同程序間進行通訊,這需約定乙個通訊協議進行傳參和函式呼叫,返回結果。
3.rpc框架
rpc能夠像呼叫本地一樣呼叫遠端服務,使用者不需要關心其底層的實現。
rpc框架的要點
rpc框架中,我們關心的有如下幾個部分:
1.通訊協議(protocol)
協議要確定通訊雙方的格式(請求物件和響應物件)
協議物件rpcprotocol
請求物件rpcrequestpublic
class
rpcprotocol
implements
serializable
public
static rpcprotocol fromjson
(string json)
@override
public
boolean
equals
(object o)
private
boolean
islistequals
(list
thislist, list
thatlist)if(
(thislist == null && thatlist != null)
||(thislist != null && thatlist == null)
||(thislist.
size()
!= thatlist.
size()
))return thislist.
containsall
(thatlist)
&& thatlist.
containsall
(thislist);}
@override
public
inthashcode()
@override
public string tostring()
public string gethost()
public
void
sethost
(string host)
public
intgetport()
public
void
setport
(int port)
public list
getserviceinfolist()
public
void
setserviceinfolist
(list
serviceinfolist)
}
響應物件 rpcresponsepublic
class
rpcrequest
implements
serializable
public
void
setrequestid
(string requestid)
public string getclassname()
public
void
setclassname
(string classname)
public string getmethodname()
public
void
setmethodname
(string methodname)
public class<
?>
getparametertypes()
public
void
setparametertypes
(class<
?>
parametertypes)
public object[
]getparameters()
public
void
setparameters
(object[
] parameters)
public string getversion()
public
void
setversion
(string version)
}
序列話將物件資料資訊轉換為用於儲存或傳輸的資訊,當伺服器收到序列化後的資訊會對其進行反序列化讀取。public
class
rpcresponse
implements
serializable
public string getrequestid()
public
void
setrequestid
(string requestid)
public string geterror()
public
void
seterror
(string error)
public object getresult()
public
void
setresult
(object result)
}
kryo序列化
在協議格式和序列化方式確定前提下,還要用編碼器將請求物件轉化為便於傳輸的位元組流,而解碼器則將位元組流轉換為服務端應用能使用的格式。public
class
kryoserializer
extends
serializer
catch
(exception ex)
finally
catch
(ioexception e)
pool.
release
(kryo);}
}@override
public
object deserialize
(byte
bytes, class
clazz)
catch
(exception ex)
finally
catch
(ioexception e)
pool.
release
(kryo);}
}}
編碼器rpcencoder
解碼器public
class
rpcencoder
extends
messagetobyteencoder
@override
public
void
encode
(channelhandlercontext ctx, object in, bytebuf out)
throws exception
catch
(exception ex)}}
}
public
class
rpcdecoder
extends
bytetomessagedecoder
@override
public
final
void
decode
(channelhandlercontext ctx, bytebuf in, list
out)
throws exception
in.markreaderindex()
;int datalength = in.
readint()
;if(in.
readablebytes()
< datalength)
byte
data =
newbyte
[datalength]
; in.
readbytes
(data)
; object obj = null;
trycatch
(exception ex)
}}
RPC之Thrift框架基本介紹
rpc remote procedure call,遠端過程呼叫 是乙個計算機通訊協議,此協議允許程序間通訊。簡單來說,當機器 a 上的程序呼叫機器 b 上的程序時,a 上的呼叫程序被掛起,而 b 上的被呼叫程序開始執行。呼叫方可以通過引數將資訊傳送給被呼叫方,然後可以通過被呼叫方傳回的結果得到返回...
RPC 手寫乙個RPC框架
基於上一節的內容rpc 準備階段,自己實現乙個基本的rpc框架。public class rpcserverframereg 註冊服務,就是將介面和實現類儲存到上邊定義的serviceholder中,param serviceinte ce 介面 param impl 介面的實現類 throws i...
高效能非同步RPC框架 kiss rpc介紹和測試
1.輕量級,簡單易用。支援idl和手動編寫協議兩種方式。模擬函式式呼叫方式,更加符合rpc遠端呼叫邏輯,簡單,透明。易修改易使用,已有的 可以直接發布使用 資料格式支援向下相容 支援多值返回特性,支援超時機制,模擬grpc,thrift,dubbo快幾倍甚至 幾十倍。支援管道資料壓縮,動態資料壓縮,...