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)
打印结果
代码阅读:
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);})); }),});
阅读全文
0 0
- Thrift
- thrift
- thrift
- thrift
- Thrift
- Thrift
- thrift
- thrift
- thrift
- Thrift
- thrift
- Thrift
- Thrift
- Thrift
- Thrift
- Thrift
- Thrift
- thrift
- 汇编语言(第三版)检测点笔记
- Kotlin基础教程-运算符重载
- 电子信息工程专业大一学生的软件之路
- 检查nginx反向代理节点状态
- leetcode Longest Substring Without Repeating Character(Java)
- thrift
- 5-9 互评成绩 (25分)
- 欢迎使用CSDN-markdown编辑器
- ARKit从入门到精通(1)-ARKit初体验
- node
- python中从str中提取元素到list以及将list转换为str
- reduce&compose&applyMiddleware&redux-thunk
- EventBus 使用心得
- (个人)AR电子书创新系统实训第五周(1)