(1)打开homebrew ,cmd中运行: homebrew
(2)更新homebrew: brew update
(3)下载thrift:brew install 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, }], });thrift --gen js:node -r -out compiled/ -I idl/ idl/TPhotoBatchService.thrift
其他命令可以thrift help查看
因为编译后为es5代码,eslint不能通过,所以要忽略该路径下文件
加载路径下所有文件及依赖
解析
返回
return function client(name, params, opts) { const method = parsed.method[name]; const serviceName = method.serviceName; const args = method.args.map(arg => encode(params[arg.name], arg.type)); const servicePool = thriftPool[serviceName] || thriftPool; const service = serviceList[serviceName]; return servicePool.exec.apply(servicePool, [service, xtend({ methodName: name }, opts), ].concat(args)); };
其中
展开原码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);})); }), });