[置顶] kiss-rpc IDL协议编写和使用方式

时间:2021-02-21 20:38:59

什么是IDL

1. IDL是kiss rpc接口代码生成协议, 编写IDL协议, 可以生成对应的服务端和客户端通用的RPC代码调用接口.
2. 规范统一化, 接口统一化, 使用简单.

IDL使用方式

1. [idl文件路径]    [输出名字]    [输出路径,默认为当前目录]. E."/root/home/kiss-rpc.idl"   kiss-rpc    "/root/home/rpc/"
2. 同时输出client和server文件代码,只需要拷贝到对应的客户端和服务端目录就行了.

IDL支持的类型

IDL D lang
bool bool
byte byte
ubyte ubyte
short short
int int
uint uint
long long
ulong ulong
float float
double double
char char
wchar wchar
string string
@message struct

IDL代码使用方式

1. 服务端只要填充server目录下service文件的函数接口代码.
2. 客户端只需要调用client目录下service文件的接口的函数.

kiss-rpc IDL 编写示例

    //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);
}

客户端远程调用(示例目录:IDL-Example/client/source/app.d)

IDL会同时生成同步接口和异步接口,异步接口都为参数回调的方式。
  • 倒入头文件
import KissRpc.IDL.KissIdlService;
import KissRpc.IDL.KissIdlMessage;
import KissRpc.Unit;
  • 客户端同步调用
            try{

auto c = addressBookService.getContactList("jasonalex");
foreach(v; c.userInfoList)
{
writefln("sync number:%s, name:%s, phone:%s, address list:%s", c.number, v.userName, v.phone, v.addressList);

}

}catch(Exception e)
{
writeln(e.msg);
}
  • 客户端异步调用
            try{

addressBookService.getContactList("jasonsalex", delegate(contacts c){

foreach(v; c.userInfoList)
{
writefln("async number:%s, name:%s, phone:%s, address list:%s", c.number, v.userName, v.phone, v.addressList);
}
}
);
}catch(Exception e)
{
writeln(e.msg);
}
以压缩方式调用(支持动态压缩和强制压缩)
  • 绑定socket方式压缩
RpcClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC); //动态压缩方式,默认超过200个字节压缩.
RpcClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_COMPRESS); //强制压缩方式
  • 单个请求方式压缩,同步调用,强制压缩
            //use compress demo
try{
writeln("-------------------------user request compress---------------------------------------------");
auto c = addressBookService.getContactList("jasonalex", RPC_PACKAGE_COMPRESS_TYPE.RPCT_COMPRESS);
foreach(v; c.userInfoList)
{
writefln("compress test: sync number:%s, name:%s, phone:%s, address list:%s", c.number, v.userName, v.phone, v.addressList);

}

}catch(Exception e)
{
writeln(e.msg);
}
  • 单个请求方式压缩,异步调用,设置100个字节的动态压缩方式,请求超时30秒
            //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("jasonsalex", delegate(contacts c){

foreach(v; c.userInfoList)
{
writefln("dynamic compress test: async number:%s, name:%s, phone:%s, address list:%s", c.number, v.userName, v.phone, v.addressList);
}
}, RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC, 30
);
}catch(Exception e)
{
writeln(e.msg);
}

服务端service文件代码(示例目录:IDL-Example/server/source/app.d):

服务端接口都会异步事件处理。
  • RpcAddressBookService.getContactList
    contacts getContactList(string accountName){

contacts contactsRet;

contactsRet.number = 100;
contactsRet.userInfoList = new UserInfo[10];


foreach(i,ref v; contactsRet.userInfoList)
{
v.phone ~= "135167321"~to!string(i);
v.age = cast(int)i;
v.userName = accountName~to!string(i);
v.addressList = new string[2];
v.addressList[0] = accountName ~ "address1 :" ~ to!string(i);
v.addressList[1] = accountName ~ "address2 :" ~ to!string(i);

}


return contactsRet;
}