egg-grpc-servers
v1.0.3
Published
a egg grpc server plugin
Downloads
4
Readme
egg-grpc-servers
Install
npm install egg-plugin-grpc-server --save
Usage
// {app_root}/config/plugin.js
exports.grpcServer = {
enable: true,
package: 'egg-grpc-server',
};
Configuration
// {app_root}/config/config.default.js
exports.grpcServer = {
port: 50051,
timeOut: 5000,
nameSpace: 'egg.node',
dir: 'app/protos',
};
see config/config.default.js for more detail.
Example
- proto文件夹中文件名({app_root}/app/protos/xxx.proto) proto文件中service名(service xxx) grpc文件夹中文件名({app_root}/app/grpc/xxx.js) 三者应当同名
- proto/xxx.proto/service的接口名 grpc/xxx.js接口名 二者应当同名
proto 文件,可通过config.dir
配置目录,默认app/proto
- Protocol Buffers Docs
- 暂未支持流 后续会增加支持
// {app_root}/app/protos/hello.proto
syntax = "proto3";
package egg.node;
service hello {
rpc sayHello (helloRequest) returns (helloResponse) {};
}
message helloRequest {
string name = 1;
int32 group = 2;
}
message helloResponse {
int32 code = 1;
string message = 2;
}
接口实现,this
为egg
的Application
,接受一个参数为请求的request
// {app_root}/app/grpc/hello.js
'use strict';
exports.sayHello = async function(request) {
return {
code: 200,
message: 'hello ' + request.name + ', you are in ' + request.group,
};
};
单元测试
const { assert, app } = require('egg-mock/bootstrap');
const path = require('path');
const PROTO_PATH_HELLO = path.join(__dirname, '../app/proto/hello.proto');
const grpc = require('grpc');
describe('test/grpc-server.test.js', () => {
beforeEach(() => {
return app.ready();
});
it('should visit hello by grpcServer.hello', async () => {
const { test } = app.grpcServer;
const res = await test.call(app, {
proto: PROTO_PATH_HELLO,
implement: 'sayHello',
data: { name: 'leo', group: 1 },
});
assert(res.code === 200);
});
Questions & Suggestions
Please open an issue here.