当前位置: 首页 > news >正文

做网站赚钱难免费门户网站搭建

做网站赚钱难,免费门户网站搭建,张北网站seo,网址查询服务中心文章目录 RPC 协议gRPCJSON-RPC 数据序列化与反序列化lua-cjsonlua-protobuf RPC 协议 在分布式计算#xff0c;远程过程调用#xff08;英语#xff1a;Remote Procedure Call#xff0c;缩写为 RPC#xff09;是一个计算机通信协议。该协议允许运行于一台计算机的程序调… 文章目录 RPC 协议gRPCJSON-RPC 数据序列化与反序列化lua-cjsonlua-protobuf RPC 协议 在分布式计算远程过程调用英语Remote Procedure Call缩写为 RPC是一个计算机通信协议。该协议允许运行于一台计算机的程序调用另一个地址空间通常为一个开放网络的一台计算机的子程序而程序员就像调用本地程序一样无需额外地为这个交互作用编程无需关注细节。RPC 是一种服务器-客户端Client/Server模式经典实现是一个通过发送请求-接受回应进行信息交互的系统。 RPC 协议根据所使用的数据格式可以分为有模式schema和无模式schema-less。 有模式schema 通讯双方需要提前定义协议模板在数据传输过程中只传输值无需传输数据结构节省流量。典型的有模式 RPC 协议是基于 Protobuf 接口描述语言来实现的例如 gRPC。 优点是协议结构明确解析效率高缺点是不够灵活协议变更需要重新定义。 无模式(schema-less) 没有预定义数据结构支持动态语言。无需协议模板数据传输过程中需要带上数据结构。典型的无模式 RPC 协议是基于 JSON 来实现的 例如 JSON-RPC 。 优点是协议灵活、易于扩展缺点是解析效率较低。 通常我们会在数据序列化格式如 Protobuf、JSON的基础上定制符合自己要求的 RPC 协议灵活的在服务器和客户端之间通信。而不是采用通用的 RPC 框架对于不需要的功能增加了通信的开销。 gRPC https://doc.oschina.net/grpc JSON-RPC https://wiki.geekdream.com/Specification/json-rpc_2.0.html 数据序列化与反序列化 lua-cjson 采用的 json for lua 库https://github.com/cloudwu/lua-cjson lua CJSON 网站: https://www.kyne.com.au/~mark/software/lua-cjson.php 安装步骤 git clone https://github.com/cloudwu/lua-cjson.gitcd lua-cjson sudo vim Makefile 修改如下版本 5.4lua 文件前缀 /home/cauchy/.luaver/lua/5.4.6 由于我使用 luaver 来管理所以这里 lua 的路径可能不同。 make 执行完 Makefile成功后会生成 cjson.so我们只需要这个动态库。 mv cjson.so ../ cd .. sudo rm -rf lua-cjson 这一步可不执行只需要 cjson.so 在 require cjson 时能找到即可自己可以放置它的位置。 示例代码 协议格式 {fid: c2s_hello,msg: world }{fid: s2c_hello,succ: true, msg: hello .. ${msg} }fid 用于映射 RPC 协议处理的函数名fd 用于标识网络连接的文件描述符JS_data 标识序列化后的 JSON 数据data 是反序列化后的数据。 创建三个文件libnet.luaserver.luaclient.lua。 libnet.lua 模拟网络库简易实现网络数据的收发过程。提供三个接口发送消息给服务端发送消息给客户端连接服务器。 local M {}local server, clientfunction M.send_to_server(fd, JS_data)if not server then server require serverend server.dispatch(fd, JS_data) endfunction M.send_to_client(fd, JS_data)if not client then client require clientend client.dispatch(fd, JS_data) endlocal fd 0 function M.connect_server()fd fd 1return fd end return M server.lua 模拟服务器业务逻辑实现处理客户端的请求。 local cjson require cjson local libnet require libnetlocal M {} local RPC {}function RPC.c2s_hello(data)return {fid s2c_hello,succ true, msg hello .. data.msg} end function M.dispatch(fd, JS_data)local data cjson.decode(JS_data)local f assert(RPC[data.fid], Not exists Func: .. data.fid)local ok, r pcall(f, data)if ok then libnet.send_to_client(fd, cjson.encode(r))end return ok end return Mclient.lua 模拟客户端业务逻辑实现处理服务端的请求。 local cjson require cjson local libnet require libnetlocal M {} local RPC {}function RPC.s2c_hello(data)print(data.succ, data.msg) end function M.dispatch(fd, JS_data)local data cjson.decode(JS_data)local f assert(RPC[data.fid], Not exists Func: .. data.fid)local ok, r pcall(f, data)return ok end return M 新建 main.lua测试逻辑。 local cjson require cjson local libnet require libnetlocal function main()local fd libnet.connect_server()local data {fid c2s_hello,msg world}local JS_data cjson.encode(data)libnet.send_to_server(fd, JS_data) endmain()lua-protobuf lua-protobuf 库https://github.com/starwing/lua-protobuf luaver use 5.4.6luaver use-luarocks 3.9.2luarocks install lua-protobuf 使用 luarocks 来管理安装 lua 包 安装完后可以查看需要的 pb.so 和 protoc.lua 两个文件的路径 pb.soProtocol Buffers 的 Lua 语言动态库文件通过在 Lua 中 require(pb) 来加载该库。 protoc.luaProtocol Buffers 的 Lua 描述文件编译器将 .proto 文件编译生成对应的 Lua 代码生成的 Lua 代码依赖 pb.so 库来实现序列化和反序列化。 示例代码 local pb require pb local protoc require protoc-- 直接载入schema (这么写只是方便, 生产环境推荐使用 protoc.new() 接口) assert(protoc:load [[message Phone {optional string name 1;optional int64 phonenumber 2;}message Person {optional string name 1;optional int32 age 2;optional string address 3;repeated Phone contacts 4;} ]])-- lua 表数据 local data {name ilse,age 18,contacts {{ name alice, phonenumber 12312341234 },{ name bob, phonenumber 45645674567 }} }-- 将Lua表编码为二进制数据 local bytes assert(pb.encode(Person, data)) print(pb.tohex(bytes))-- 再解码回Lua表 local data2 assert(pb.decode(Person, bytes)) print(data2.name, data2.age, data2.contacts[1].name, data2.contacts[1].phonenumber, data2.contacts[2].name, data2.contacts[2].phonenumber)使用 protoc.new() 创建一个编译器实例加载 .proto 文件 addressbook.proto syntax proto3; package cauchy; message Person {string name 1; int32 age 2; enum PhoneType {MOBILE 0; HOME 1;WORK 2;}message PhoneNumber {string number 1;PhoneType type 2; }repeated PhoneNumber phones 3; }main.lua local protoc require protoc local pb require pblocal data {name cauchy,age 20,phones {{ number 1234567890, type 1 },{ number 0987654321 }} }local p protoc.new()local addressbook io.open(addressbook.proto):read(a) p:load(addressbook)local bytes assert(pb.encode(cauchy.Person, data)) local data2 assert(pb.decode(cauchy.Person, bytes)) print(data2.name, data2.age, data2.phones[1].number, data2.phones[1].type)for name, id, types in pb.fields(cauchy.Person) do print(name, id, types) end 由于我是通过 luaver 管理 luarocks 和 lua使用 luarocks 分发 lua 模块安装的 lua-protobuf。所以上述路径下没有 pb.so 和 protoc.lua直接找到安装路径下的需要 luaver 使用 lua 和 luarocks。 不过使用者也可以直接拉取到执行脚本的路径下。 上述操作是在 lua 代码中直接通过导入 protoc.lua 文件来在运行时加载 .proto 文件的无需提前编译但是这样转换性能可能比较慢。 下面我们来安装 protoc 编译器 sudo apt updatesudo apt install -y protobuf-compiler protoc 默认安装在 /usr/bin/ 下。 执行 protoc -o addressbook.pb addressbook.proto 生成 .pb 文件。 .proto 文件以 Protocol Buffers 语言编写的接口定义文件,用来定义数据结构、服务接口等。 .pb 文件从 .proto 接口定义文件生成的目标语言代码文件而没有指定目标语言生成通用的二进制文件包含了编码后的 Protocol Buffers 数据。 local pb require pbpb.loadfile(./addressbook.pb)local data {name cauchy,age 20,phones {{ number 1234567890, type 1 },{ number 0987654321}} }local bytes pb.encode(cauchy.Person, data) print(pb.tohex(bytes))local data2 pb.decode(cauchy.Person, bytes) print(data2.phones[2].number, data2.phones[2].type)for name, id, types in pb.fields(cauchy.Person) do print(name, id, types) end 更多具体的 API 操作参考官方 GitHubhttps://github.com/starwing/lua-protobuf
http://www.hkea.cn/news/14450293/

相关文章:

  • 网站建设文字sem竞价是什么意思
  • 桥东区住房和建设局网站辽宁省网站备案
  • 安阳区号0372网站内页怎样做优化
  • 网站代码素材建设智能家居网站建设方案
  • 苏州网站建设 苏州网络推广专家网站设计模板是什么
  • 如何建设和优化一个网站工作室 网站经营性备案
  • 广州网站建设 app 小程序玉林专业网站建设
  • 请打开网站wordpress 内外网
  • 网站建设对图片有哪些要求以下是付费推广方式是
  • 汽车租赁网站设计潍坊市住房和城乡建设局官方网站
  • 西安响应式网站建设哪家强dede网站地图怎么做
  • 台州网站推广优化网站建设平台合同模板下载
  • 公司建设网站的优缺点wordpress如何分版
  • 介绍一学一做视频网站wordpress站点更换域名
  • 免费的黄冈网站有哪些代码北京网站建设一站式服务
  • 推介做界面的网站付费网站源码
  • 一流的盐城网站开发网上做石材去哪个网站
  • 做分销网站好吗石景山郑州阳网站建设
  • 网站建设需要学多久任务发布平台
  • flash网站设计概述装修论坛
  • 网络 网站建设网站制作代码大全
  • 宣传网站建设的意义标题制作网站
  • 岳阳建设网站制作杭州市网站seo
  • 网站建设管理系统免费网站室内装修设计软件排行榜
  • 网站视频怎么做公共资源交易中心网站建设汇报
  • 南沙哪有做网站的专业网站建设哪家好
  • 购物网站开发 需求分析wordpress文章固定字段
  • 网站免费建站 图标用python做的网站多吗
  • 凡科做的网站提示证书错误展示型网站 带后台
  • wordpress电影网站主题广州优俊网站制作公司