首页 » PHP教程 » php同事技巧_高可用可拓展的RPC框架 koalasRPC

php同事技巧_高可用可拓展的RPC框架 koalasRPC

访客 2024-11-20 0

扫一扫用手机浏览

文章目录 [+]

1:为什么要写这个RPC

市情上常见的RPC框架很多,grpc,motan,dubbo等,但是随着越来越多的元素加入,繁芜的架构设计等成分似使得这些框架和spring一样,虽然号称是轻量级,但是用起来却是让我们很蹩脚,大量的配置,繁杂的API设计,实在,我们根本用不上这些东西!


我也算得上是在很多个互联网企业厮杀过,见过很多很多的内部RPC框架,有些精良的设计让我非常讴歌,有一天我溘然想着,为什么不对这些设计原型进行聚合归类,自己搞一套【轻量级】RPC框架呢,碍于事情缘故原由,一贯没有韶光倒腾出空,十一期间事情空隙,说搞就搞吧,落地不易,很多细节性问题,比如tcp中怎么办理大量的wait-time,如何做到thrift和netty的兼容等等大量细节的优化,希望源码对大家对认识RPC框架起到推进的浸染。
东西越写越多,有各种问题欢迎随时拍砖

php同事技巧_高可用可拓展的RPC框架 koalasRPC

2:为什么叫koalas

php同事技巧_高可用可拓展的RPC框架 koalasRPC
(图片来自网络侵删)

树袋熊英文翻译,希望考拉RPC给那些不太喜好动手自己去造轮子的人供应可靠的RPC利用环境

3:技能栈

thrift 0.8.0 spring-core-4.2.5,spring-context-4.2.5,spring-beans-4.2.5 log4j,slf4j org.apache.commons(v2.0+) io.netty4 fastJson zookeeper 点评cat(V3.0.0+ 做数据大盘统计上报等利用,可不配置) AOP,反射代理等

4:关于技能选型

序列化篇 稽核了很多个序列化组件,个中包括jdk原生,kryo、hessian、protoStuff,thrift,json等,终极选择了Thrift,缘故原由如下 原生JDK序列化反序列化效率堪忧,其序列化内容太过全面kryo和hessian,json相对来说比原生JDK强一些,但是对跨措辞支持一样平常,以是舍弃了,终极想在protoBuf和Thrift协议里面选择一套框架,这俩框架很相通,支持跨措辞,须要静态编译等等。
但是protoBuf不带RPC做事,本着供应多套做事端模式(thrift rpc,netty)的情形下,终极选择了Thrift协议。
IO线程模型篇 原生socket可以仿照出大略的RPC框架,但是对付大规模并发,哀求吞吐量的系统来说,也就算得上是一个demo级别的,以是BIO肯定是不考虑了,NIO的模型在序列化技能选型的时候已经说了,Thrift本身支持很多个io线程模型,同步,异步,半同步异步等(SimpleServer,TNonblockingServer,THsHaServer,TThreadedSelectorServer,TThreadPoolServer),个中吞吐量最高切实其实定是半同步半异步的IO模TThreadedSelectorServer了,详细缘故原由大家可自行google,这次不做多的阐述,选择好了模型之后,创造thrift切实其实便是神器一样的存在,再一想,对付做事端来说,IO模型怎么能少得了Netty啊,以是下决心也要支持Netty,但是很遗憾Netty目前没有对Thrift的序列化解析,拆包粘包的处理,但是有protoBuf,和http协议的封装,怎么办,自己在netty上写对thrift的支持呗,虽然事情量大了一些,但是一想netty不便是干这个事儿的嘛- -!
做事创造 支持集群的RPC框架里面,像dubbo,或者是其他三方框架,对做事创造都进行的封装,那么自研RPC的话,做事创造就要自己来写了,那么大略小巧随意马虎上手的zookeeper肯定是首选了。

5:安装教程

考拉RPC确保精简,轻量的原则,只须要zk做事器进行做事创造(后续版本做事管理可能须要Datasource),对付zookeeper的各个环境安装教程请自行google,不在本安装教程内特意解释 如果须要cat的数据大盘功能,想更方便的查看做事的调用情形,须要安装cat做事,至于cat的安装就更大略了,便是war包扔在tomcat里面运行,然后配置一些参数即可,当然你也可以不接入cat,单独的作为RPC框架来利用。
CAT接入参考:https://www.oschina.net/p/cat-dianping

二:利用解释

1:前期准以及依赖

maven依赖

<dependency> <groupId>koalas.rpc</groupId> <artifactId>com.Koalas.rpc</artifactId> <version>Koalas-1.0-SNAPSHOT</version> </dependency>

关于私服的引用问题,记得全局文件不要把全局的依赖都代理掉,由于这么做只能从aliyun的私服高下载项目,由于koalas-rpc中的Cat依赖只在美团点评的私有仓库中存在,这么做会下载依赖失落败,以是不要暴力的设置下面的代理做法。

<mirror> <id>nexus-aliyun</id> <mirrorOf></mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>

精确的做法是将代理去掉,直接按照作者在pom.xml文件中给定的依赖仓库地址就可以了。

首先须要编写自己的thrift idl文件了,这里多说一句,在群里的小伙伴曾经说过idl文件编写不熟习,有可能出错 这里顺带说一嘴,thrift的ldl文件和写java的要求体和service险些没有任何差异,熟能生巧,上手之后非常大略 这里推举几篇thrift的文章,有兴趣可以看一看 https://blog.csdn.net/lk10207160511/article/details/50450541, https://blog.csdn.net/hrn1216/article/details/51306395 下面截图为测试的thrift文件

namespace java thrift.serviceinclude 'WmCreateAccountRequest.thrift'include 'WmCreateAccountRespone.thrift'service WmCreateAccountService { WmCreateAccountRespone.WmCreateAccountRespone getRPC(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest1(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest2(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest3(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest4(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest5(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest6(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest7(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest8(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest9(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest10(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest11(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest); WmCreateAccountRespone.WmCreateAccountRespone koaloasTest12(1:WmCreateAccountRequest.WmCreateAccountRequest wmCreateAccountRequest);}namespace java thrift.domain/ 测试类/struct WmCreateAccountRequest { 1:i32 source, 2:i32 accountType, 3:i64 partnerId, 4:i32 partnerType, 5:string partnerName, 6:i32 poiFlag,}namespace java thrift.domain/ 测试类/struct WmCreateAccountRespone { 1:i32 code, 2:string message,}

编译器须要大家去下载对应的版本 windows和linux下不同的编译器,下载地址http://archive.apache.org/dist/thrift/0.8.0/ 下载0.8.0版本即可,0.8.0版本是很老的版本了,但是相对稳定,后续会把thirft版本升级。
如果上面地址下载不下来或者失落效,可以上作者的网盘高下载zip包,上面有win版本和mac,linux版本的0.8.0的thrift编译器,链接: https://pan.baidu.com/s/1JpLqVbmokTOe30nU_TznWw提取码: ntye, 编译上面三个文件 thrift -gen java WmCreateAccountService.thrift, thrift -gen java WmCreateAccountRequest.thrift, thrift -gen java WmCreateAccountRespone.thrift 在当前目录下会天生3个java文件 这三个文件分别是要求体,返回体,和做事类,就这么大略 Ok作为开拓者而言,所有的准备事情都结束了。
下面就开始进入实际开拓~

2:xml配置办法

1. 客户端同步调用

首先在你的xml里面配置一下引用

<?xml version=\公众1.0\"大众 encoding=\"大众UTF-8\"大众?><beans xmlns=\"大众http://www.springframework.org/schema/beans\"大众 xmlns:xsi=\"大众http://www.w3.org/2001/XMLSchema-instance\公众 xmlns:koalas=\"大众http://www.koalas.com/schema/ch\"大众 xsi:schemaLocation=\公众http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.koalas.com/schema/ch http://www.koalas.com/schema/ch.xsd\"大众><koalas:client id=\公众wmCreateAccountService1\"大众 serviceInterface=\公众thrift.service.WmCreateAccountService\"大众 zkPath=\公众127.0.0.1:2181\公众/></beans>

首先引用koalas的自定义schema,xmlns:koalas和xsi:schemaLocation, 个中serviceInterface为thrift自动天生的java类,zkPath为zk的做事地址,默认是同步调用,接下来便是在java里面的远程调用了。

package thrift.service;import org.apache.thrift.TException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import thrift.domain.WmCreateAccountRequest;import thrift.domain.WmCreateAccountRespone;@Service(\公众testService\公众)public class TestService { @Autowired WmCreateAccountService.Iface wmCreateAccountService; public void getRemoteRpc() throws TException { WmCreateAccountRequest request= new WmCreateAccountRequest ( ); //request.setSource ( 10 ); request.setAccountType ( 1 ); request.setPartnerId ( 1 ); request.setPartnerType ( 1 ); request.setPartnerName ( \公众你好\"大众 ); request.setPoiFlag ( 1 ); WmCreateAccountRespone respone = wmCreateAccountService.getRPC ( request); System.out.println (respone); }}

就这么大略一个高性能的RPC框架就出身了。
WmCreateAccountService是thrift自动天生的,作为利用者而言不须要做任何事情,只须要在spring bean中注入xxx.Iface即可。

2. 客户端异步调用

刚刚我们看了客户真个同步调用办法,下面我们一起来看看异步的利用办法, 首先在你的xml里面配置一下引用

<?xml version=\公众1.0\"大众 encoding=\公众UTF-8\公众?><beans xmlns=\公众http://www.springframework.org/schema/beans\"大众 xmlns:xsi=\"大众http://www.w3.org/2001/XMLSchema-instance\"大众 xmlns:koalas=\"大众http://www.koalas.com/schema/ch\"大众 xsi:schemaLocation=\"大众http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.koalas.com/schema/ch http://www.koalas.com/schema/ch.xsd\公众><koalas:client id=\公众wmCreateAccountService2\"大众 serviceInterface=\公众thrift.service.WmCreateAccountService\"大众 zkPath=\公众127.0.0.1:2181\"大众 async=\公众true\"大众/></beans>

和同步的差异async=true,代表异步利用,接下来便是在java里面的异步远程调用了

package thrift.service;import client.async.KoalasAsyncCallBack;import org.apache.thrift.TException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import thrift.domain.WmCreateAccountRequest;import thrift.domain.WmCreateAccountRespone;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;@Service(\"大众testService\"大众)public class TestService2 { @Autowired WmCreateAccountService.AsyncIface wmCreateAccountService; public void getRemoteRpc() throws TException{ KoalasAsyncCallBack<WmCreateAccountRespone, WmCreateAccountService.AsyncClient.getRPC_call> koalasAsyncCallBack = new KoalasAsyncCallBack<> (); WmCreateAccountRequest request= new WmCreateAccountRequest ( ); request.setAccountType ( 1 ); request.setPartnerId ( 1 ); request.setPartnerType ( 1 ); request.setPartnerName ( \"大众你好啊\"大众 ); request.setPoiFlag ( 1 ); wmCreateAccountService.getRPC ( request ,koalasAsyncCallBack); Future<WmCreateAccountRespone> future= koalasAsyncCallBack.getFuture (); try { //to get other things System.out.println (future.get ()); } catch (InterruptedException e) { e.printStackTrace (); } catch (ExecutionException e) { e.printStackTrace (); } }}

这次调用getRpc方法不会壅塞等待server同步结果了。
而是可以去干一些自己的其他事情,然后在调用future.get ()来得到返回resopne,当然future.get ()支持最大等待韶光的,超时之后会抛出TimeOutException,当然这仅仅是client超时而已不会影响server的实行结果。

3. 做事端实现

<?xml version=\公众1.0\"大众 encoding=\公众UTF-8\公众?><beans xmlns=\公众http://www.springframework.org/schema/beans\"大众 xmlns:xsi=\"大众http://www.w3.org/2001/XMLSchema-instance\"大众 xmlns:context=\"大众http://www.springframework.org/schema/context\公众 xmlns:koalas=\公众http://www.koalas.com/schema/ch\"大众 xsi:schemaLocation=\公众http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.koalas.com/schema/ch http://www.koalas.com/schema/ch.xsd\公众> <koalas:server id=\"大众WmCreateAccountService\"大众 serviceInterface=\公众thrift.service.WmCreateAccountService\"大众 serviceImpl=\"大众wmCreateAccountServiceImpl\"大众 port=\公众8001\"大众 zkpath=\"大众127.0.0.1:2181\"大众/></beans>

做事端只须要指定暴露的端口,zk做事地址和做事端实现即可。

@Servicepublic class WmCreateAccountServiceImpl implements WmCreateAccountService.Iface { @Override public WmCreateAccountRespone getRPC(WmCreateAccountRequest wmCreateAccountRequest) throws TException { WmCreateAccountRespone wmCreateAccountRespone = new WmCreateAccountRespone (); wmCreateAccountRespone.setCode ( 1 ); wmCreateAccountRespone.setMessage ( \"大众你好\公众 ); if(new Random ( ).nextInt ( 5 )>100){ throw new RuntimeException ( \"大众测试缺点\"大众 ); } System.out.println ( \"大众getRPC start ....\"大众 + wmCreateAccountRequest + \公众------\公众 + atomicInteger.incrementAndGet () ); return wmCreateAccountRespone; }}

只须要实现xxxx.Iface即可

3:表明配置办法

有的小伙伴会以为配置xml有点麻烦,koalas-rpc也供应了纯表明的利用办法

1. 客户端调用

xml中的配置

<?xml version=\"大众1.0\公众 encoding=\"大众UTF-8\公众?><beans xmlns=\"大众http://www.springframework.org/schema/beans\"大众 xmlns:xsi=\公众http://www.w3.org/2001/XMLSchema-instance\公众 xmlns:koalas=\公众http://www.koalas.com/schema/ch\"大众 xsi:schemaLocation=\公众http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.koalas.com/schema/ch http://www.koalas.com/schema/ch.xsd\"大众><koalas:annotation package=\"大众thrift.annotation.client.impl\"大众/></beans>

一个扫描标签就行了,如果你在spring bean里想通过调用rpc远程做事,那么扫描一下就行了

java中利用

@Service(\公众testServiceSync\"大众)public class TestServiceSync { @KoalasClient(zkPath = \"大众127.0.0.1:2181\"大众,readTimeout = 50001000) WmCreateAccountService.Iface wmCreateAccountService; public void getRemoteRpc() throws TException { WmCreateAccountRequest request= new WmCreateAccountRequest ( ); //request.setSource ( 10 ); request.setAccountType ( 1 ); request.setPartnerId ( 1 ); request.setPartnerType ( 1 ); request.setPartnerName ( \"大众你好啊-我是表明实现的\"大众 ); request.setPoiFlag ( 1 ); WmCreateAccountRespone respone = wmCreateAccountService.getRPC ( request); System.out.println (respone); }}

只须要在你想远程调用的类上加一个@KoalasClient表明就可以了,远程调用就这么大略,当然异步利用办法也类似

@Service(\公众testServiceAsync\"大众)public class TestServiceAsync { @KoalasClient(zkPath = \"大众127.0.0.1:2181\"大众,readTimeout = 50001000) WmCreateAccountService.AsyncIface wmCreateAccountService; public void getRemoteRpc() throws TException{ KoalasAsyncCallBack<WmCreateAccountRespone, WmCreateAccountService.AsyncClient.getRPC_call> koalasAsyncCallBack = new KoalasAsyncCallBack<> (); WmCreateAccountRequest request= new WmCreateAccountRequest ( ); //request.setSource ( 10 ); request.setAccountType ( 1 ); request.setPartnerId ( 1 ); request.setPartnerType ( 1 ); request.setPartnerName ( \"大众你好啊-我是表明实现的\"大众 ); request.setPoiFlag ( 1 ); wmCreateAccountService.getRPC ( request ,koalasAsyncCallBack); Future<WmCreateAccountRespone> future= koalasAsyncCallBack.getFuture (); try { System.out.println (future.get ()); } catch (InterruptedException e) { e.printStackTrace (); } catch (ExecutionException e) { e.printStackTrace (); } }}

把稳和同步调用不同的是自定义表明注入的接口是xxxx.AsyncIface,同步是xxxx.Iface。
KoalasAsyncCallBack回调利用办法和上面的xml一样。
有一点须要解释

<koalas:annotation package=\"大众thrift.annotation.client.impl\"大众/>

如果package属性设置为空,那么所有的@KoalasClient都会生效,也便是说所有在spring bean中的自定义表明@KoalasClient都会自动注入。
这里说其余一种用法

private WmCreateAccountService.Iface wmCreateAccountService;@KoalasClient(zkPath = \"大众127.0.0.1:2181\"大众,readTimeout = 50001000)public void setWmCreateAccountService(WmCreateAccountService.Iface wmCreateAccountService){ this.wmCreateAccountService = wmCreateAccountService;}

直接注入方法的办法也是可以的。

2. 做事端实现

xml中的配置

<?xml version=\"大众1.0\公众 encoding=\"大众UTF-8\"大众?><beans xmlns=\公众http://www.springframework.org/schema/beans\"大众 xmlns:xsi=\公众http://www.w3.org/2001/XMLSchema-instance\"大众 xmlns:koalas=\"大众http://www.koalas.com/schema/ch\"大众 xsi:schemaLocation=\"大众http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.koalas.com/schema/ch http://www.koalas.com/schema/ch.xsd\"大众> <koalas:annotation package=\"大众thrift.annotation.server.impl\"大众/></beans>

配置和client中一样只须要配置一个自定义标签即可,java中的利用办法如下:

package thrift.annotation.server.impl;import annotation.KoalasServer;import org.apache.thrift.TException;import thrift.domain.WmCreateAccountRequest;import thrift.domain.WmCreateAccountRespone;import thrift.service.WmCreateAccountService;import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;@KoalasServer ( port = 8801,zkpath=\"大众127.0.0.1:2181\"大众)public class WmCreateAccountServiceNettyImpl implements WmCreateAccountService.Iface { private AtomicInteger atomicInteger = new AtomicInteger ( 0 ); @Override public WmCreateAccountRespone getRPC(WmCreateAccountRequest wmCreateAccountRequest) throws TException { WmCreateAccountRespone wmCreateAccountRespone = new WmCreateAccountRespone (); wmCreateAccountRespone.setCode ( 1 ); wmCreateAccountRespone.setMessage ( \公众你好啊\公众 ); if(new Random ( ).nextInt ( 5 )>100){ try { Thread.sleep ( 5000 ); } catch (InterruptedException e) { e.printStackTrace (); } } System.out.println ( \"大众getRPC start ....\公众 + wmCreateAccountRequest + \"大众------\"大众 + atomicInteger.incrementAndGet () ); return wmCreateAccountRespone; }}

这样做事实现就会主从注册到zookeeper中供应给client端利用了。
值得解释的是被扫描到并且类上有@KoalasServer的类会被加载到spring高下文中,可以当成一个普通的spring bean来处理,还有一点如果你不指定package,配置成如下情形

<koalas:annotation package=\"大众\"大众/>

这样配置会以spring的bean为根本实现,那么利用办法须要改成

package thrift.annotation.server.impl;import annotation.KoalasServer;import org.apache.thrift.TException;import thrift.domain.WmCreateAccountRequest;import thrift.domain.WmCreateAccountRespone;import thrift.service.WmCreateAccountService;import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;@KoalasServer ( port = 8801,zkpath=\"大众127.0.0.1:2181\"大众)@Servicepublic class WmCreateAccountServiceNettyImpl implements WmCreateAccountService.Iface { private AtomicInteger atomicInteger = new AtomicInteger ( 0 ); @Override public WmCreateAccountRespone getRPC(WmCreateAccountRequest wmCreateAccountRequest) throws TException { WmCreateAccountRespone wmCreateAccountRespone = new WmCreateAccountRespone (); wmCreateAccountRespone.setCode ( 1 ); wmCreateAccountRespone.setMessage ( \"大众你好啊\公众 ); if(new Random ( ).nextInt ( 5 )>100){ try { Thread.sleep ( 5000 ); } catch (InterruptedException e) { e.printStackTrace (); } } System.out.println ( \"大众getRPC start ....\公众 + wmCreateAccountRequest + \公众------\"大众 + atomicInteger.incrementAndGet () ); return wmCreateAccountRespone; }}

就这么大略即可。

3. 泛化调用

为什么须要泛化调用? 1:有一个通用压测平台,想去压测不同的server。
那么现在就有一个问题了,不可能让压测平台做事端去依赖所有的下贱做事,这样依赖会很繁杂,这时候如果说只配置serviceName,request模型和request要求json就可以进行远程调用,那么将大大的减少头疼的依赖。
2:假设php同事对java代码不熟习,不可能让他们去依赖spring,一共一套大略的api来利用是很有必要的。
3:上游做事不想依赖下贱做事的数据模型。

对付泛化调用来说,dubbo已经供应,soft-rpc也有供应。
当然koalas-rpc也不会例外,并且支持xml,表明和java api的利用办法。
下面几个例子来解释一下利用办法。
更多demo去源码中查看,作者已经写好,开箱即用。

xml利用办法

<koalas:client id=\"大众wmCreateAccountService3\"大众 serviceInterface=\"大众thrift.service.WmCreateAccountService\公众 zkPath=\"大众127.0.0.1:2181\"大众 generic=\"大众true\"大众 readTimeout=\"大众50000000\公众/>@Autowired@Qualifier(\"大众wmCreateAccountService3\公众)GenericService.Iface wmGenericService;public void getGenericRpc() throws TException { GenericRequest request = new GenericRequest ( ); request.setMethodName ( \公众getRPC\"大众 ); request.setClassType ( new ArrayList<String> ( ){{ add ( \"大众thrift.domain.WmCreateAccountRequest\"大众); }} ); request.setRequestObj ( new ArrayList<String> ( ){{ add ( \"大众{\\"大众accountType\\"大众:1,\\"大众partnerId\\"大众:1,\\"大众partnerName\\"大众:\\公众你好\\"大众,\\"大众partnerType\\"大众:1,\\公众poiFlag\\"大众:1,\\公众source\\公众:0}\"大众); }} ); String str = wmGenericService.invoke ( request ); System.out.println (str); }

大略解释一下,GenericService.Iface是通用做事,有三个参数,第一个是方法名称,第二个是要求体类型凑集,第三个是要求体内容。
直接调用即可,返回值是server真个json类型,利用json工具为阿里巴巴的Fast-json

表明利用办法

@KoalasClient(zkPath = \"大众127.0.0.1:2181\"大众,readTimeout = 50001000,genericService = \"大众thrift.service.WmCreateAccountService\公众) GenericService.Iface genericService; public void getGenericRemoteRpc() throws TException { GenericRequest request = new GenericRequest ( ); request.setMethodName ( \"大众getRPC\"大众 ); request.setClassType ( new ArrayList<String> ( ){{ add ( \公众thrift.domain.WmCreateAccountRequest\"大众); }} ); request.setRequestObj ( new ArrayList<String> ( ){{ add ( \公众{\\"大众accountType\\公众:1,\\公众partnerId\\"大众:1,\\"大众partnerName\\"大众:\\"大众你好\\"大众,\\"大众partnerType\\"大众:1,\\"大众poiFlag\\"大众:1,\\公众setAccountType\\"大众:true,\\公众setPartnerId\\"大众:true,\\公众setPartnerName\\"大众:true,\\公众setPartnerType\\"大众:true,\\"大众setPoiFlag\\公众:true,\\"大众setSource\\"大众:false,\\公众source\\"大众:0}\"大众); }} ); String str = genericService.invoke ( request ); System.out.println (str); }

唯一差异的是表明要指定genericService,当genericService不为空时,默认开启泛化调用 当然,java api办法也是支持的。

KoalasClientProxy koalasClientProxy = new KoalasClientProxy(); koalasClientProxy.setServiceInterface ( \"大众thrift.service.WmCreateAccountService\"大众 ); koalasClientProxy.setZkPath (\"大众127.0.0.1:2181\"大众 ); koalasClientProxy.setGeneric ( true ); koalasClientProxy.setReadTimeout ( 50000000 ); koalasClientProxy.afterPropertiesSet (); GenericService.Iface genericService = (GenericService.Iface) koalasClientProxy.getObject (); GenericRequest request = new GenericRequest ( ); request.setMethodName ( \"大众getRPC\"大众 ); request.setClassType ( new ArrayList<String> ( ){{ add ( \"大众thrift.domain.WmCreateAccountRequest\公众); }} ); request.setRequestObj ( new ArrayList<String> ( ){{ add ( \"大众{\\"大众accountType\\"大众:1,\\"大众partnerId\\"大众:1,\\"大众partnerName\\"大众:\\公众你好\\公众,\\"大众partnerType\\"大众:1,\\"大众poiFlag\\公众:1,\\"大众setAccountType\\"大众:true,\\"大众setPartnerId\\"大众:true,\\"大众setPartnerName\\公众:true,\\公众setPartnerType\\公众:true,\\"大众setPoiFlag\\"大众:true,\\公众setSource\\"大众:false,\\"大众source\\"大众:0}\公众); }} ); String str = genericService.invoke ( request ); System.out.println (str); koalasClientProxy.destroy ();

特殊把稳的是KoalasClientProxy工具非常非常重,一定要在做事关闭的时候实行koalasClientProxy.destroy ();方法,并且须要带运用程序中缓存该工具,千万不要每次利用都要创建,这样会极大的摧残浪费蹂躏资源,每个做事对应一个KoalasClientProxy,同步和异步也是不同的工具,这些利用者须要把稳。

相关文章

语言游戏聚会的魅力,跨界交流的盛宴

在繁忙的都市生活中,一场别开生面的语言游戏聚会悄然兴起。这不仅是一场简单的娱乐活动,更是一次跨界交流的盛宴,一场思想的碰撞与火花。...

PHP教程 2024-12-29 阅读1 评论0

语言序列逻辑在现代传播中的运用与影响

语言序列逻辑,作为现代传播学中的重要理论之一,对于理解语言传播的规律、提高传播效果具有重要作用。在信息化、网络化时代,语言序列逻辑...

PHP教程 2024-12-29 阅读1 评论0