一、實現思路
基於socket與serversocket網路程式設計
二、實現方案
1、rpc-server:
(1)、rpc-server-api
a.建立 isayhellow
public
inte***ce
isayhellow
b.建立 istudyhard
public
inte***ce
istudyhard
c.建立 rpcrequest
public
class
rpcrequest
implements
serializable
public
void
setclassname
(string classname)
public string getmethodname()
public
void
setmethodname
(string methodname)
public object[
]getparams()
public
void
setparams
(object[
] params)
}
(2)、rpc-server-provider
a.建立sayhellowimpl
public
class
sayhellowimpl
implements
isayhellow
}
b.建立studyhardimpl
public
class
studyhardimpl
implements
istudyhard
}
c.建立 rpchandler
public
class
rpchandler
catch
(ioexception e)
while
(true
)catch
(ioexception e)
objectinputstream objectinputstream = null;
objectoutputstream objectoutputstream = null;
trycatch
(ioexception e)
rpcrequest rpcrequest = null;
trycatch
(ioexception e)
catch
(classnotfoundexception e)
string classname = rpcrequest.
getclassname()
; string methodname = rpcrequest.
getmethodname()
; object[
] params = rpcrequest.
getparams()
; class <
?> aclass = null;
trycatch
(classnotfoundexception e)
class[
] types =
newclass
[params.length]
;for
(int i =
0; i
) method method = null;
trycatch
(nosuchmethodexception e)
trycatch
(instantiationexception e)
objectoutputstream =
newobjectoutputstream
(socket.
getoutputstream()
);objectoutputstream.
writeobject
(result)
; objectoutputstream.
flush()
;}catch
(illegalacces***ception e)
catch
(invocationtargetexception e)
finally
if(objectoutputstream!= null)
system.out.
println
("rpc呼叫結束------");
}}}}
public
class
catch
(ioexception e)
}}
2、rpc-client:
a. 建立 **類 rpcproxy
public
class
rpcproxy
implements
invocationhandler
}catch
(ioexception e)
objectoutputstream objectoutputstream = null;
objectinputstream objectinputstream = null;
trycatch
(ioexception e)
trycatch
(ioexception e)
trycatch
(classnotfoundexception e)
}catch
(ioexception e)
finally
catch
(ioexception e)}if
(null != objectoutputstream)
catch
(ioexception e)}}
return result;
}}
b.建立 訪問**客戶端 rpcproxyclient
public
class
rpcproxyclient
,new
rpcproxy()
);}}
public
class
}
三.工程**結構圖
RPC 手寫乙個RPC框架
基於上一節的內容rpc 準備階段,自己實現乙個基本的rpc框架。public class rpcserverframereg 註冊服務,就是將介面和實現類儲存到上邊定義的serviceholder中,param serviceinte ce 介面 param impl 介面的實現類 throws i...
手寫實現RPC 框架
乙個簡易的rpc框架,別的先不多說上github github gitee 註冊中心 zookeeper 使用curator 操作 通訊框架 netty 4.1.25版本 序列化 kryo 以下只寫了大體專案流程,以及展示部分 具體上方github 裡基本都寫了注釋 本rpc框架,有乙個統一的 框架...
從零手寫RPC
clientstub sereverstub 可以看作乙個 物件,遮蔽rpc呼叫過程中複雜的網路處理邏輯,使rpc透明化,使得呼叫遠端方法想呼叫本地方法一樣。server 服務端提供遠端服務。注 serverstub又叫skeleton。public inte ce ihellopublic cla...