项目作者: msmao

项目描述 :
egg gRPC framework for node.js
高级语言: JavaScript
项目地址: git://github.com/msmao/egg-grpc-framework.git
创建时间: 2021-05-26T05:32:06Z
项目社区:https://github.com/msmao/egg-grpc-framework

开源协议:

下载


gRPC framework for egg

中文文档

Features

  1. Support gRPC + Protobuf
  2. Extend EGG Router To Support RPC Method
  3. It Supports EGG Middleware、Plugin
  4. It Supports Both HTTP、gRPC Request,Common Use Controller And Service

QuickStart

  1. $ npm install egg-grpc-framework --save

Usage

  1. // package.json
  2. {
  3. "name": "user",
  4. "egg": {
  5. "framework": "egg-grpc-framework"
  6. },
  7. // ...
  8. }

Configuration

  1. // {app_root}/config/config.default.js
  2. config.gRPC = {
  3. // listen: false, // disable gRPCServer
  4. listen: {
  5. port: 50051,
  6. hostname: '0.0.0.0',
  7. },
  8. // protoLoader: {
  9. // options: { keepCase: true, longs: String, enums: String, defaults: true, oneofs: true },
  10. // },
  11. // gRPCServerOptions: {},
  12. };

Example

  1. // {app_root}/app/router.js
  2. 'use strict';
  3. module.exports = app => {
  4. const { router, controller } = app;
  5. router.rpc('/user/login', controller.user.login);
  6. router.get('/rpc', controller.user.test);
  7. };
  1. // {app_root}/app/controller/user.js
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. class UserController extends Controller {
  5. async login() {
  6. const body = this.ctx.request.body;
  7. const result = await this.service.user.login(body);
  8. this.ctx.body = result;
  9. }
  10. // test http method call rpc
  11. async test() {
  12. const params = this.ctx.query;
  13. const result = await this.ctx.rpc.userService.user.login(params);
  14. this.ctx.body = result;
  15. }
  16. }
  17. module.exports = UserController;
  1. // {app_root}/app/service/user.js
  2. 'use strict';
  3. const Service = require('egg').Service;
  4. class UserService extends Service {
  5. async login() {
  6. // ...
  7. return { state: 'ok' };
  8. }
  9. }
  10. module.exports = UserService;

Test

  1. $ curl http://localhost:7001/rpc?username=admin&password=xxx