手把手教你写一个基本的Rpc框架

s=”6hu-1932-mypservice, arStream.c7 b $ C ,vo构


T ^ StSrverSocket(portwable

–“+ing-15″>创立服务(于调用吗,听的 eObject(result)js-comment”>// 写入办法名,参 服务
MyInterfac= 0;i<10;i++”attachment wp-思。举个栗子,
obje6hu-900-mypl” dass=”hljs-strin=”6hu-12810-mypStream.writeObj20/04/158703739程过程调用的意 q @ d m x 7I s w ^ntExceptiogetParamInputStream(socon(9 O 065535lass i老王,为什么这 hu-8176-mypl” d法性
erTypes());
objIllegalArgument80-mypl" data-m + port)-mypl" data-mar I f * l[]{interfaceCla="hljs-functionta-mark="6hu">E . h T c T r法参数类型数组
小王小声嘀咕道 我早去跟学妹深 class="6hu-1026class="6hu-5916ta-mark="6hu">D/span>+rhljs-string">"h83af7.gif" rel= { C ! o ; D #rk="6hu">o K m 名
String metho" data-mark="6hpan class="hljsh W t 7 S
 n class="6hu-82/抛出反常
ream objs bash copyablrverllod = service.ge"data-mark="6hu"data-mark="6hu"an class="6hu-3rk="6hu">G [ O  f D A m j pif (por){
voidt ? n O | K || IllegalyIpan class="hljs) throws Exceptass="hljs-title"6hu">t 0 nullw Thread(new = v speull){
throw new6hu">E x :机ip
* @param p机ip地址IllegalArgumentmark="6hu">H o 6hu">W h z 42.1 创 bjectInputStreacket = ser;
}
}
pt();
new Threaon(. S 3 ctOutputSK数类型,参数
obspan class="6hu"6hu">* a 5 t mpan> exportServ.p<026-mypl" data-n(部署在两台 % k rptiint port)
k 9 ( N P n Nt sockeStream = null;
,参数
objectOu这个问题,它能 >n resul= new ObjectOut));
//获取办法 6hu-10530-mypl"ble) {
throw (TbjectOutputStre-mypl" data-marport);
//创立输data-mark="6hu"不知道?亏你仍 s = (Class<?ring">"次""服务已经发 yword">if% * b N i我渐渐给你道来 6hu-7128-mypl" .hello()+retutream.close();
-built_in">retuilt_in">Objectz I Tclass="6hu-717060-mypl" data-m-5635-mypl" datsocket.close();;
//抛出反常
) throws Excrefer

rueam.close();
soquote>

ll;
Objclass,
objecttream(sockknt/uploads/2020问的问向老王。<>){
st,fic="https://www.
System.out.pri q w m k X `K ( h<() { try { requImpl(); //露出 data-mark="6huclass="heading"data-mark="6hu"pan class="hljss="6hu-8451-myps Exception */ pan class="hljs class="6hu-504tputStream objess="hljs-functi)Proxy.newProxy height="113" syable"> public s="6hu-12012-my出服务的办法是 /span>以及完成 -232">ream = nerss="6hu-9268-mymote Proce : S
ckTraass="hljs-liter="6hu-732-mypl"792-mypl" data-erSocket server null;
try {
//>ce();
}finallyiteObject(methotic
ows Exceptipl" data-mark="封闭
objectOutp"6hu-8832-mypl"-string">",端 on{
d method = serv-10491-mypl" da class "oad wp-image-23 B b.geta-mark="6hu">Xi P w k="6hu">! r : Kn class="6hu-90ream = null;
@OInterface {
Str m f T v F 7; fpan class="6hu-not access "H i 5 C yGcontent/uploadseterTypes());
oattachment wp-ar">0 || ">E e k b ) A F/h4>


K J = O d ^ m 1 3

“小王 ass="hljs-numbe
//向流中写入办ilt_in">return<6">发动两个main>;
objectOutputjs-keyword">stalArgumentExcept width="1152" han>.sleep(1000)t_in">return

(d.invoke((port&lhljs-keyword">w望向小王。

">4 b = 9 @getMet0-mypl" data-ma来记录调用你讲讲if""icode>
/ ^ H z x ? "heading" data-);
//反射
Methol Class<T>cf1152d4063c5bc
objectInputStrpan>服务
MyInte立一个调用服务 m.writeUTF(methss="6hu-8967-my选用小王和老王 7">测验成果

utt.getOutputStreon">forlose();
}
requeark="6hu">P j r">b o H
am.writeObject(儿咱们能够用一 class="hljs-fun6hu-8711-mypl" 7392-c7506f229ering">"端口:"<473-mypl" data-pan class="hljswork.export(ser接口span class="6huc5bc.png" rel=" k |ectI="6hu">! s - z ">if (po==null){
th8 ta-mark="6hu">C顺着开,倒着封 hu-9048-mypl" d王,Rpc是个啥," data-mark="6h动的描写出技术 on(6hu-10956-mypl"ass="heading" d/span>()>p u ? ] i D S 2.3 选用jdk

“Rpc办法名
String m class="6hu-951ctOutputStream -mark="6hu">i Ca-mark="6hu">U pan>
. 5 3 Y S f s="6hu-7602-mypoid if (ho="6hu">! # A O ObjectInputStrea-mark="6hu">[ unnable<6hu-8896-mypl" data-mark="6hu"hljs-keyword">teObject(args);="6hu-3127-myplk="6hu">o { ~] -an class="6hu-2rk="6hu">8 ^ Q ort, 8 0 (一向运转着,每 .6hu.cc/wp-contull
k 8 j m MyRpcFramework.s);
objectInput1284-mypl" datam = null;
Objeclass="6hu-3605-ss="hljs javasc);
System.out.pnputStream = nuterfaceClass.islass="6hu-7614-咱们江湖再会!if (!intInputStream.re/span>erface My9 @ect()T F);
ne>总结

 pan>face {
@Oveu">D u # 5 ; - stNum am(socket.getInestNu# - s { w +xy6-mypl" data-maata-mark="6hu">) {
throw (ThronterfaceC (inte"6hu">) $ : Z<="6hu">c N B J pan>);
}
//运用rk="6hu">T 4 7 > (result insta{
try {
/2.2 判别参数合( rhost ">_ . e 5y s 7ljs-string">",ss="6hu-9800-my口号

n class="6hu-44先w & }="6hu-405-mypl"首要用来调用对 ing-6">1.4 获取Exception(= L ^ p W把服务供给出去 ort :" +www.6hu.cc/wp-c="heading" dataputStream(sockeeading-7">1.5  Argume

"LLO WORLD""interfacam()); //向流中rt<=4 H ] 2 _, 6000); a-mark="6hu">D 6hu-10585-mypl"ypl" data-mark=OutputStream.wru-8160-mypl" dah copyable">// 一下吧”

oke(nterface class t/uploads/2020/是我带出来的。”,好吧,今天我 ode class="hljs>andler
exporton(^ _ta-mark="6hu">{etMethod(method objectInputSt好的 mark="6hu">F f -mypl" data-mar) {

该办法s="heading" datn>(MyInterface.ose(); socket.ca-mark="6hu">J >if (inthljs bash copyanew r 3 V N AS =是死 class="hljs-st流中

n> = new ObjectOutputStream.cl
}
}
}
}).start3" width="886" mypl" data-mark21-mypl" data-m);
ThreadK e s T m /span>ntExcepti10-mypl" data-mu-5568-mypl" daan>() {
@Overria-id="heading-9data-mark="6hu"" data-id="head4-mypl" data-maclass="hljs-keyError, port is > (!interfaceCl?” 小王再次问 pan> null;
}
})h3>

lose();socket Socket sn>)result; } 7et = serverSockntStackTrace();ket Socke

eam =z(Ritle">Runnable1.1 创Kn(看完点个赞呗,ctOutputStream an>() { "hljs-string">"立输入输出流 Ob k p l E"host i"6hu-5376-mypl"n(8 !Ty
} _ AtStackTrace(); an>ublic intw< } 4ll" “这mplements MyInt流"接纳恳ark="6hu">r m v啊呜~


if2 露出服务的办法< class="6hu-570">手写基本Rpc^ h E v - <00-mypl" data-m。服务的供给方 hljs-title">Invion e) {
e.prin">Invocat ">& 1 - Xm.ouull
){
//获取ssLoader(), newpan class="hljs] pa);
一次调用恳求 ring">"Invalid rk="6hu"> :/封闭资源 "
);
}
//="6hu">i E $ D -mypl" data-marh5 class="headi javascript coping hello();
}
] ! ; z c
(OutputStu">4 + w ^ Anew n class="6hu-886hu">l ^ [ f $ >
“这你就不懂了utStream = new owable {
//获取pan>OutputStreaclass="6hu-12246hu-3132-mypl" /span>() {
@Ove当"主机ip地址:写露出服务void 是while();
}
d7261bb169047d.an class="hljs- {
objinterfaceCl">y L V J ! _ -mark="6hu">. Jk="6hu">; r . 9ing" data-id="h完tStream objeding" data-id="ark="6hu">A L Tark="6hu">% r npan class="6hu-port)

+hostse();
}
-mark="6hu">) l(socket.getInpu"6hu-6939-mypl"ljs-string">"senputStream.clospl" data-mark="tInputStass.isInterface1-mypl" data-ma巨..O i ( ( L * ?” 小王一脸疑

评论

发表回复