Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egg框架使用egg-sofa-rpc时无法调用service中定义的方法 #3447

Closed
hupengfoot opened this issue Feb 5, 2019 · 3 comments
Closed

Comments

@hupengfoot
Copy link

Background

使用egg框架,部分服务希望通过rpc进程间通信,引入egg-sofa-rpc,结果发现egg框架所有的
方法功能都是绑定在koa HTTP框架下的,对rpc调用很不友好

Proposal

建议增加对rpc调用使用全局方法的支持

@egg-bot
Copy link

egg-bot commented Feb 5, 2019

Hello @hupengfoot. Please provide a reproducible example following the instruction.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.


@hupengfoot,请根据这个说明提供最小可复现代码。

如果在 7 天内没有进展会被自动关闭。

@gxcsoccer
Copy link
Contributor

gxcsoccer commented Feb 12, 2019

可以调用 service 啊

class 写法

'use strict';

const Service = require('chair').Service;

module.exports = class UserService extends Service {
  async sayHelllo(name) {
     const { ctx } = this;
     // 可以通过 ctx 来访问所有的 api
     await ctx.service.xxx.yyy();

     return 'hello ' + name;
  }
}

Function 写法

'use strict';

exports.sayHello = async function(name) {
  // 这里的 this 就是 ctx
  await this.service.xxx.yyy();

  return 'hello ' + name;
};

@egg-bot
Copy link

egg-bot commented Feb 12, 2019

Hello @hupengfoot, we use GitHub issues to trace bugs or discuss plans of Egg.js. So, please don't ask usage questions here. You can try to ask questions on Stack Overflow or CNode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants