交互模型:thrift api handler
基本环境:
1.全局下载thrift解析器:
(1)打开homebrew ,cmd中运行: homebrew
(2)更新homebrew: brew update
(3)下载thrift:brew install thrift
2.copy thriftify中的server/api/thrift文件下文件到自己项目中的同路径下,同时pakage下载@mtfe/thrift
2.将自己所需的idl文件倒入到/idl下
3.更改thrift配置:
(1)server/router下添加
const thrift = require('./api/thrift'); router.set(`${rootdir}thrift/:method`, thrift.handler);
引入thrift模块,设定request的路径,method为idl中server文件中的访问目标,引入thrift.handler来处理该路由下的request
(2)修改server/api/thrift/index中的idlPath , testClient中的参数
const idlPath = require.resolve('./idl/TMessageService.thrift'); // const idlPath = require.resolve('./idl/test_parse.thrift'); const testClient = thriftify(idlPath, { localAppKey: names.clientId, remoteAppKey: 'com.sankuai.meituan.meishi.merchant.message.remote', serviceName: 'com.sankuai.meituan.meishi.merchant.message.remote.thrift.TMessageService', // serviceName: 'com.sankuai.meituan.meishi.merchant.photo.thrift.service.TPhotoBatchService', timeout: 20000, serviceList: [{ ip: '10.4.239.194', port: 9001, }], });
4.thrift编译命令
thrift --gen js:node -r -out compiled/ -I idl/ idl/TPhotoBatchService.thrift
其他命令可以thrift help查看
5.在.eslintignore中添加thrift的路径
server/api/thrift/idl server/api/thrift/compiled
因为编译后为es5代码,eslint不能通过,所以要忽略该路径下文件
6.在组件中使用request的时候,就可以访问`${rootdir}thrift/${method}?${query}来获取数据,其中method为想访问的目标(名字可以自行修改),query为get方法的参数(eg:limit=20&offset=0)
打印结果
代码阅读:
thrift.js:
function thriftify(idl, config)
加载路径下所有文件及依赖
解析
返回
index.js
const testClient = thriftify(idlPath, {config}); //调用thriftify创建client客户端
const thriftRpc = thriftRpcCreator(testClient); //创建rpc函数
module.exports = { client: testClient, handler: thriftHandlerCreator(thriftRpc), //创建hanlder函数 };
其中
3个函数嵌套,thriftHandlerCreator(thriftRpc) 等同于
thriftHandlerCreator(thriftRpcCreator(thriftify(idlPath, {config})))
//
thriftHandlerCreator = rpc => client => params => (req, res, opts, cb) => {
client(opts.params.method, params, topts)
} => ({
GET: bsso.useAuthRouter((req, res, opts, cb) => {rpc(query)(req, res, opts, cb);}), POST: bsso.useAuthRouter((req, res, opts, cb) => {rpc(body)(req, res, opts, cb);})); }), });