rpc调用基础

1.安装
参考官方文档安装 https://sekiro.iinti.cn/sekiro-doc/01_manual/4.server_install.html#%E5%AE%89%E8%A3%85%E5%90%8E%E6%9C%8D%E5%8A%A1%E5%88%9D%E5%A7%8B%E5%8C%96%E8%AE%BE%E7%BD%AE
如果事windows 直接下载 https://oss.iinti.cn/sekiro/sekiro-demo


2.运行
安装 java(自行安装)
运行启动脚本:
bin/Sekiro.sh :mac or linux
bin/Sekiro.bat :windows


 

3.接口的使用

注意使用本地127.0.0.1:5612/business-demo/
查看分组列表
展示当前系统有那些注册过的group
http://127.0.0.1:5612/business-demo/groupList
#查看队列状态
对于某个group,查看当前挂载了多少个设备,在多设备负载均衡的时候,他会比较有用
http://127.0.0.1:5612/business-demo/clientQueue?group=test
#调用转发
通过httpAPI的方式,可以直接拼接如下URL,调用请求将会被转发到SekiroClient的handler上。为方便可视化,请求参数扁平化,即sekiro保留参数和业务参数公用传输字段域

http://127.0.0.1:5612/business-demo/invoke?group=test&action=test&param=testparm

4.浏览器嵌入

 1).在source中overrides点+,选择路径

2)允许权限

3)找到要添加的位置右键overrides content,然后编辑保存

将下边代码复制到浏览器中找到要覆盖的文件,右键overrides content本地覆盖,保存并刷新


function SekiroClient(e){if(this.wsURL=e,this.handlers={},this.socket={},!e)throw new Error("wsURL can not be empty!!");this.webSocketFactory=this.resolveWebSocketFactory(),this.connect()}SekiroClient.prototype.resolveWebSocketFactory=function(){if("object"==typeof window){var e=window.WebSocket?window.WebSocket:window.MozWebSocket;return function(o){function t(o){this.mSocket=new e(o)}return t.prototype.close=function(){this.mSocket.close()},t.prototype.onmessage=function(e){this.mSocket.onmessage=e},t.prototype.onopen=function(e){this.mSocket.onopen=e},t.prototype.onclose=function(e){this.mSocket.onclose=e},t.prototype.send=function(e){this.mSocket.send(e)},new t(o)}}if("object"==typeof weex)try{console.log("test webSocket for weex");var o=weex.requireModule("webSocket");return console.log("find webSocket for weex:"+o),function(e){try{o.close()}catch(e){}return o.WebSocket(e,""),o}}catch(e){console.log(e)}if("object"==typeof WebSocket)return function(o){return new e(o)};throw new Error("the js environment do not support websocket")},SekiroClient.prototype.connect=function(){console.log("sekiro: begin of connect to wsURL: "+this.wsURL);var e=this;try{this.socket=this.webSocketFactory(this.wsURL)}catch(o){return console.log("sekiro: create connection failed,reconnect after 2s:"+o),void setTimeout(function(){e.connect()},2e3)}this.socket.onmessage(function(o){e.handleSekiroRequest(o.data)}),this.socket.onopen(function(e){console.log("sekiro: open a sekiro client connection")}),this.socket.onclose(function(o){console.log("sekiro: disconnected ,reconnection after 2s"),setTimeout(function(){e.connect()},2e3)})},SekiroClient.prototype.handleSekiroRequest=function(e){console.log("receive sekiro request: "+e);var o=JSON.parse(e),t=o.__sekiro_seq__;if(o.action){var n=o.action;if(this.handlers[n]){var s=this.handlers[n],i=this;try{s(o,function(e){try{i.sendSuccess(t,e)}catch(e){i.sendFailed(t,"e:"+e)}},function(e){i.sendFailed(t,e)})}catch(e){console.log("error: "+e),i.sendFailed(t,":"+e)}}else this.sendFailed(t,"no action handler: "+n+" defined")}else this.sendFailed(t,"need request param {action}")},SekiroClient.prototype.sendSuccess=function(e,o){var t;if("string"==typeof o)try{t=JSON.parse(o)}catch(e){(t={}).data=o}else"object"==typeof o?t=o:(t={}).data=o;(Array.isArray(t)||"string"==typeof t)&&(t={data:t,code:0}),t.code?t.code=0:(t.status,t.status=0),t.__sekiro_seq__=e;var n=JSON.stringify(t);console.log("response :"+n),this.socket.send(n)},SekiroClient.prototype.sendFailed=function(e,o){"string"!=typeof o&&(o=JSON.stringify(o));var t={};t.message=o,t.status=-1,t.__sekiro_seq__=e;var n=JSON.stringify(t);console.log("sekiro: response :"+n),this.socket.send(n)},SekiroClient.prototype.registerAction=function(e,o){if("string"!=typeof e)throw new Error("an action must be string");if("function"!=typeof o)throw new Error("a handler must be function");return console.log("sekiro: register action: "+e),this.handlers[e]=o,this};
var client = new SekiroClient("ws://127.0.0.1:5612/business-demo/register?group=test_web&clientId=" + Math.random());
client.registerAction("testAction", function (request, resolve, reject) {
    resolve("ok");});

名词解释

在快速入门章节,我简单演示了各个语言的代码快速接入,快速接入教程中出现了一些名词,这些名词是sekiro的框架定义。为了让大家加深理解,这里对这些框架名词做一些说明。

sekiro-words

#sekiro-sdk

对于多种语言,sekiro提供编程sdk给用户使用,sekiro-sdk主要用于连接sekiro服务器,并且提供接受服务器调用参数,回写调用结果给服务器的基础能力。sekiro-sdk一般来说都是对socket和线程池的包装。

#group

业务类型(接口组),每个业务一个group。group下面可以注册多个终端(SekiroClient),同时Group可以挂载多个Action。 Group指代一个确定的业务,group下注册的client代码需要保持协议的完全一致。如微信机器人为一个group、xxx爬虫为另一个group。sekiro系统中Group全局唯一,同时Sekiro根据Group进行调用转发路由,并认为统一个group下面的所有client能够提供一致性的服务能力.也就是说,对sekiro来说,转发请求的时候,sekiro认为统一个group下的任意一个设备( 手机、浏览器、客户端代码等)都是等价的,转发到任意一个终端都是相同的语义

#SekiroClient

服务提供者客户端,主要场景为手机/浏览器等。最终的sekiro调用会转发到SekiroClient。每个client需要有一个惟一的clientId, ClientId最好用户自己生成,并且保证ClientId具备特定含义

#action

接口,同一个Group下面可以有多个接口,分别做不同的功能。如视频app,区分用户搜索接口、用户视频接口、直播接口、用户详情接口等。action是为了方便用户开发的时候隔离多个业务进行的抽象,在sekiro层面可以帮你进行一次路由。 这是因为在实际开发过程中可以发现同一个设备,可能可以提供多个接口

#clientId

ClientId指代设备,多个设备使用多个机器提供API服务,提供群控能力和负载均衡能力。如在手机上,同一个接口组下面可以挂载多台设备, ClientId大家需要保证每个客户端的ClientId的唯一性。建议使用mac地址、IMEI、ip地址+进程号等真实的物理ID作为ClientId。

使用ClientId可以作为调度策略控制的ID,完成对特定设备的调用管理。

5.调用接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值