1. IDL是kiss rpc接口代码生成协议, 编写IDL协议, 能够生成对应的服务端和客户端通用的RPC代码调用接口.生成对应的flatbuffer协议接口 2. 规范统一化, 接口统一化, 使用简单,真正意义上的函数式调用方式。
1. [idl文件路径] [输出名字] [输出路径,默认为当前目录]. E."/root/home/kiss-rpc.idl" kiss-rpc "/root/home/rpc/" 2. 以模块名称输出, 模块路径为".": E."/root/home/kiss-rpc.idl" module.test.kiss-rpc "/root/home/rpc/" 3. 同时输出client和server文件代码,只须要拷贝到对应的客户端和服务端目录就好了. 4. message的类型名字必须首字母大写,类型成员必须标上序列号,不然没法编译经过 5. 函数参数列表只能为一个,不然没法编译经过。
IDL | D lang |
---|---|
bool | bool |
byte | byte |
ubyte | ubyte |
short | short |
ushort | ushort |
int | int |
uint | uint |
long | long |
ulong | ulong |
float | float |
double | double |
char | char |
string | string |
[] | DynamicArrayList |
@message | struct |
1. 服务端只要填充server目录下service文件的函数接口代码. 2. 客户端只须要调用client目录下service文件的接口的函数.
//kiss rpc idl demo @message:UserInfo { string phone:3; string userName:1; int age:2; double wiget:4; string[] addressList:5; } @message:contacts { int number:1; UserInfo[] userInfoList:2; } @service:AddressBook //接口类 { contacts getContactList(string accountName); }
倒入头文件app
import KissRpc.IDL.kissidlService; import KissRpc.IDL.kissidlMessage; import KissRpc.Unit;
客户端同步调用异步
try{ auto c = addressBookService.getContactList(name); foreach(v; c.userInfoList) { writefln("sync number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age); } }catch(Exception e) { writeln(e.msg); }
客户端异步调用socket
try{ addressBookService.getContactList(name, delegate(Contacts c){ foreach(v; c.userInfoList) { writefln("async number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age); } } ); }catch(Exception e) { writeln(e.msg); }
绑定socket方式压缩async
RpcClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC); //动态压缩方式,默认超过200个字节压缩. RpcClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_COMPRESS); //强制压缩方式
单个请求方式压缩,同步调用,强制压缩函数
//use compress demo try{ auto c = addressBookService.getContactList(name, RPC_PACKAGE_COMPRESS_TYPE.RPCT_COMPRESS); foreach(v; c.userInfoList) { writefln("compress test: sync number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age); } }catch(Exception e) { writeln(e.msg); }
单个请求方式压缩,异步调用,设置100个字节的动态压缩方式,请求超时30秒ui
//use dynamic compress and set request timeout try{ RPC_PACKAGE_COMPRESS_DYNAMIC_VALUE = 100; //reset compress dynamaic value 100 byte, default:200 byte addressBookService.getContactList(name, delegate(Contacts c){ foreach(v; c.userInfoList) { writefln("dynamic compress test: sync number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age); } }, RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC, 30 ); }catch(Exception e) { writeln(e.msg); }
RpcAddressBookService.getContactListcode
Contacts getContactList(AccountName accountName){ Contacts contactsRet; //input service code for Contacts class contactsRet.number = accountName.count; for(int i = 0; i < 10; i++) { UserInfo userInfo; userInfo.age = 18+i; userInfo.name = accountName.name ~ to!string(i); userInfo.widget = 120+i; contactsRet.userInfoList ~= userInfo; } return contactsRet; }