thrift

来源:互联网 发布:程序员群头像 编辑:程序博客网 时间:2024/06/01 16:14

交互模型: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/idlserver/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函数};

 

其中

 展开原码

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);})); }),});
原创粉丝点击