项目作者: hm496

项目描述 :
egg-tchannel
高级语言: JavaScript
项目地址: git://github.com/hm496/egg-tchannel.git
创建时间: 2020-03-16T02:32:47Z
项目社区:https://github.com/hm496/egg-tchannel

开源协议:MIT License

下载


egg-tchannel

tchannel plugin for egg.

Install

  1. $ npm i tchannel
  2. $ npm i egg-tchannel

Configuration

egg-tchannel with default configurations below:

  • tchannelModule: ‘tchannel’,
  • cacheThriftTime: 0 thriftIDL cache millisecond, 0 => permanent
  • thriftIDLPath: path.join(appInfo.baseDir, "thrift_idl")
  • timeout: 2500
  • thriftOptions:

    strict: false
    allowOptionalArguments: true

  1. // {app_root}/config/config.default.js
  2. exports.tchannel = {
  3. // timeout: 2500,
  4. };

Usage

  1. // {app_root}/app.js
  2. class AppBootHook {
  3. constructor (app) {
  4. this.app = app;
  5. }
  6. async didLoad () {
  7. // registry should return a promise, like:
  8. function registry(serviceName, tag) {
  9. return Promise.resolve([
  10. {
  11. peer: '192.x.x.10:6666'
  12. },
  13. {
  14. peer: '192.x.x.11:6666'
  15. }
  16. ]);
  17. }
  18. this.app.tchannel.configure({
  19. registry: registry
  20. });
  21. }
  22. }
  23. module.exports = AppBootHook;

In controller, you can use app.tchannel.request.

  1. // app/controller/home.js
  2. module.exports = app => {
  3. return class HomeController extends app.Controller {
  4. async index() {
  5. const { ctx, app } = this;
  6. const res = await app.tchannel.request({
  7. serviceName: 'serviceName',
  8. method: 'method',
  9. headers: {},
  10. body: {
  11. 'some key': 'some data',
  12. },
  13. tag: 'group1',
  14. options: {}
  15. });
  16. ctx.body = res;
  17. }
  18. };
  19. };

Questions & Suggestions

Please open an issue here.

License

MIT