从零开始手写dubborpc框架-1generic泛化调用_第1页
从零开始手写dubborpc框架-1generic泛化调用_第2页
从零开始手写dubborpc框架-1generic泛化调用_第3页
从零开始手写dubborpc框架-1generic泛化调用_第4页
免费预览已结束,剩余1页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、rpcrpc是基于netty实现的javarpc框架,类似于dubbo。主要用于个人学习,由渐入深,理解rpc的底层实现原理generic泛化调用说明泛化接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所有POJO匀用Map表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过GenericService调用所有服务实现。GenericServicebarService=(GenericService)applicationContext.getBean("barService");Objectresult=barService.$inv

2、oke("sayHello",newString"java.lang.String",newObject"World");实现思路客户端泛化调用个人感受不是很深,但是有一点,当没有服务端接口的时候,也就无法通过反射获取对应的方法等原始信息。所以需要额外提供一个接口,并且可以获取方法的相关属性。服务端本次基本没做处理。个人理解是客户使用的时候自行定义实现类。客户端实现接口/* 泛化调用接口* (1)接口直接使用dubbo的接口* 【应用场景】* 泛接口实现方式主要用于服务器端没有API接口及模型类元的情况,参数及返回值中的所有POJO均

3、用Map表示,通常用于框架集成,比如:实现一个通用的远程服务Mock框架,可通过实现GenericService接口处理所有服务请求。* 【服务端】* 服务端代码不需要做任何调整。* 客户端泛化调用进行相关调整即可。* 【客户端】* authorbinbin.hou* since0.1.2* /publicinterfaceGenericService/*Genericinvocation* parammethodMethodname,e.g.findPerson.Ifthereareoverriddenmethods,parameterinfois* required,e.g.findPer

4、son(java.lang.String)* paramparameterTypesParametertypes* paramargsArguments* returninvocationreturnvalue* throwsGenericExceptionpotentialexceptionthrownfromtheinvocation* /Object$invoke(Stringmethod,StringparameterTypes,Objectargs)throwsGenericException;默认实现默认的实现,其实和基于接口的动态代理非常的类似。这种实现只需要在用户指定为gene

5、ric的时候,使用这个实现即可。/* 泛化调用* authorbinbin.hou* since0.1.2*/publicclassGenericReferenceProxyimplementsGenericServiceprivatestaticfinalLogLOG=LogFactory.getLog(GenericReferenceProxy.class);/* 代理上下文* (1)这个信息不应该被修改,应该和指定的service紧密关联* since0.1.3* /privatefinalServiceContextproxyContext;/*远程调用接口since0.1.3*/pr

6、ivatefinalRemoteInvokeServiceremoteInvokeService;publicGenericReferenceProxy(ServiceContextproxyContext,RemoteInvokeServiceremoteInvokeService)xyContext=proxyContext;this.remoteInvokeService=remoteInvokeService;©OverrideSuppressWarnings("unchecked")publicObject$invoke(Stringme

7、thod,StringparameterTypes,Objectargs)throwsGenericException/构建基本调用参数finallongcreateTime=Times.systemTime();ObjectactualArgs=newObjectmethod,parameterTypes,args;DefaultRpcRequestrpcRequest=newDefaultRpcRequest();rpcRequest.serviceld(proxyContext.serviceld();rpcRequest.createTime(createTime);newArrayL

8、ist(););););rpcRequest.paramValues(actualArgs);List<String>paramTypeNames=Guavas.paramTypeNames.add("java.lang.String"paramTypeNames.add("Ljava.lang.String;"paramTypeNames.add("Ljava.lang.Object;"rpcRequest.paramTypeNames(paramTypeNames);rpcRequest.methodName(&quo

9、t;$invoke");rpcRequest.returnType(Object.class);/proxyContext中应该是属于当前service的对应信息。/每一次调用,对应的invoke信息应该是不通的,需要创建新的对象去传递信息/rpcRequest因为要涉及到网络间传输,尽可能保证其简洁性。DefaultRemotelnvokeContextcontext=newDefaultRemotelnvokeContext();context.request(rpcRequest);context.traceld(Ids.uuid32();context.retryTimes(

10、2);context.serviceProxyContext(proxyContext);context.remotelnvokeService(remotelnvokeService);/3,执行远程调用returnremotelnvokeService.remotelnvoke(context);)测试代码注册中心启动客户端?指定配置为generic使用GenericService接口直接调用publicstaticvoidmain(String口args)/服务配置信息ReferenceConfig<GenericService>config=ClientBs.newInst

11、ance();config.serviceId(ServiceIdConst.GENERIC);config.serviceInterface(GenericService.class);config.subscribe(true);config.registerCenter(ServiceIdConst.REGISTER_CENTER);config.generic(true);GenericServicegenericService=config.reference();genericService.$invoke("hello",newString"name

12、",newObject口"123");服务端?测试代码这个FooGenericService实现非常简单,只是输出对应的参数信息publicstaticvoidmain(Stringargs)/启动服务ServiceBs.getInstance().register(ServiceIdConst.GENERIC,newFooGenericService().registerCenter(ServiceIdConst.REGISTER_CENTER).expose();服务端日志信息INFO2019-11-0122:53:12,316nioEventLoopGrou

13、p-3-1c.g.h,r,s.h.RpcServerHandler.channelRead0-Serverchannelreadstart:502b73fffec4485c-000019fc-00000002-bd2c76df8b24bcd4-e2e8065aINFO2019-11-0122:53:12,317nioEventLoopGroup-3-1c.g.h,r,s.h.RpcServerHandler.channelRead0-Serverreceivechannel502b73fffec4485c-000019fc-00000002-bd2c76df8b24bcd4-e2e8065ar

14、equest:DefaultRpcRequestseqId='4afb085e10b94063ad4b6e46aa617fcd',createTime=1572619992279,serviceId='generic',methodName='$invoke',paramTypeNames=java.lang.String,Ljava.lang.String;,Ljava.lang.Object;,paramValues=hello,Ljava.lang.String;11e7ee7,Ljava.lang.Object;c69f46,return

15、Type=classjava.lang.ObjectINFO2019-11-0122:53:12,319nioEventLoopGroup-3-1c.g.h.r.c,s.g.i.FooGenericService.$invoke-Genericmethod:helloINFO2019-11-0122:53:12,319nioEventLoopGroup-3-1c.g.h.r.c,s.g.i.FooGenericService.$invoke-GenericparameterTypes:nameINFO2019-11-0122:53:12,320nioEventLoopGroup-3-1c.g.h.r.c,s.g.i.FooGenericService.$invoke-Genericargs:123INFO2019-11-0122:53

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论