Name: egg-grpc
Owner: egg
Description: grpc plugin for egg
Created: 2017-05-27 03:24:04.0
Updated: 2018-04-10 04:50:48.0
Pushed: 2018-04-26 06:51:19.0
Homepage: null
Size: 31
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
grpc plugin for egg
m i egg-grpc --save
s
app_root}/config/plugin.js
rts.grpc = {
able: true,
ckage: 'egg-grpc',
app_root}/config/config.default.js
rts.grpc = {
dpoint: 'localhost:50051',
dir: 'app/proto', // proto files dir, relative path
property: 'grpc', // default attach to `ctx.grpc.**`
loadOpts: { convertFieldsToCamelCase: true, }, // message field case: `string user_name` -> `userName`
see config/config.default.js for more detail.
fixtures:
proto
egg
??? test
??? game.proto
??? message.proto
uc
??? test.proto
share.proto
rotobuf
pp/proto/share.proto
ax = "proto3";
age egg.share;
age Status {
ring code = 1;
ring err_msg = 2;
ice ShowCase {
c Echo(Status) returns (Status) {}
quickstart:
ount by `package` define, not proto file path
t client = ctx.grpc.egg.share.showCase;
t result = yield client.echo({ code: 200 });
ole.log(result);
app/proto
.ctx
and app
.app/proto/share.proto
, it defined as package egg;
, so will visit asyield ctx.grpc.egg.share.showCase.echo(data, meta, options)
new app.grpcProto.egg.share.Status({ code: 200 })
new app.grpcProto.egg.share.ShowCase(address)
new ctx.grpcProto.egg.share.ShowCase(address)
| term | case at proto | case when load |
| ———– | —————— | ———————————- |
| package | lowercase with .
| camleCase if contains _
|
| service | PascalCase | camleCase when initialize at ctx
|
| rpc | PascalCase | camleCase |
| message | PascalCase | PascalCase at app
|
| field | snake_case | camleCase |
| enums | CONSTANT_CASE | CONSTANT_CASE |
timeout
- for convenient usage of deadline
, equals to { deadline: Date.now() + timeout }
You can get service instance and invoke rpc by ctx.mypackage.myService.myRpc({ id: 1 })
.
Usually, you don't need to instantiate a message instace, grpc will do it for you, however you can create it by new app.grpcProto.mypackage.SomeMessage({ id: 1 })
.
nary RPC, such as `rpc Echo(Request) returns (Response) {}`
param {Object} data - data sent to sever
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
return {Promise} response promise chain
see http://www.grpc.io/docs/guides/concepts.html#unary-rpc
onst client = ctx.grpc.<package>.<service>;
d client.echo(data, metadata, options);
d client.echo(data, options);
lient Streaming RPC, such as `rpc EchoClientStream(stream Request) returns (Response) {}`
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
param {Function} [callback] - callback for response, `(err, response) => {}`
return {Stream} write stream
see http://www.grpc.io/docs/guides/concepts.html#client-streaming-rpc
t stream = client.echoClientStream(meta, options, callback);
onst stream = client.echoClientStream(callback);
onst stream = client.echoClientStream(meta, callback);
rigger order: metadata -> callback -> status
am.once('metadata', meta => {});
am.once('status', status => {});
am.on('error', status => {});
end data to server or end
am.write(data1);
am.write(data2);
am.end(data4);
erver Streaming RPC, such as `rpc EchoServerStream(Request) returns (stream Response) {}`
param {Object} data - data sent to sever
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
return {Stream} read stream
see http://www.grpc.io/docs/guides/concepts.html#server-streaming-rpc
t stream = client.echoServerStream(data, meta, options);
rigger order: metadata -> data -> status -> end
am.on('data', response => {});
am.on('end', response => {});
am.once('metadata', meta => {});
am.once('status', status => {});
am.on('error', status => {});
idirectional Streaming RPC, such as `rpc echoStreamStream(stream Request) returns (stream Response) {}`
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
return {Stream} duplex stream
see http://www.grpc.io/docs/guides/concepts.html#bidirectional-streaming-rpc
t stream = client.echoStreamStream(meta, options);
rigger order: metadata -> data -> status -> end
am.on('data', response => {});
am.on('end', () => {});
am.once('metadata', meta => {});
am.once('status', status => {});
am.on('error', status => {});
end data to server or end
am.write(data1);
am.write(data2);
am.end(data3);
see grpc.tests.js.
Please open an issue here.