push埋点-thrift使用笔记

交互模型: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)

打印结果

//console.log('opts:', opts)
server/server route.src is "/thrift/:method"
opts: { params: { method: 'mmsMessageList' },
           splat: null,
           user: { id: 2086350, login: 'xiaojia05', name: '肖佳', userId: 2086350 },
           ssoid: 'f8833cbd47*94a90b719ea2957e15a7d',
           queryKeyForUserId: 'ssoid' }
//log.info('thrift rpc', opts.params.method, JSON.stringify(params), JSON.stringify(topts))
thrift rpc mmsMessageList {"offset":"0","limit":"30"} {"header":{"globalContext":{"ssoid":"2086350"}},"unified":true}



代码阅读:

thrift.js:

function thriftify(idl, config)


加载路径下所有文件及依赖

解析

返回

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

index.js

const testClient = thriftify(idlPath, {config}); //调用thriftify创建client客户端
const thriftRpc = thriftRpcCreator(testClient); //创建rpc函数
module.exports = {
  client: testClient,
 handler: thriftHandlerCreator(thriftRpc), //创建hanlder函数
};


其中

const thriftRpcCreator = client => params => (req, res, opts, cb) => {
 //...
  log.info('thrift rpc', opts.params.method, JSON.stringify(params), JSON.stringify(topts));

 client(opts.params.method, params, topts)
  .then(data => {
    console.info('rpc data: ', data);
 	sendJson(req, res, { data }, cb);
 })
  .catch(error => {
    console.info('rpc err: ', error);
 	sendJson(req, res, { error }, cb);
 });
};


const thriftHandlerCreator = rpc => ({
  	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);}));
 }),
});

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);}));
 }),
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值