README
Dee-GRPC 框架
Usage
- dee 中添加配置项
services: {
rpc: {
constructor: 'grpc',
constructorArgs: {
// 定义提供的 RPC 服务
serverProtoPath: path.resolve(__dirname, './server.proto'),
// 控制器函数,是一个对象,其每个属性对应一个 RPC 操作。
handlers: require('./handlers'),
// 检查客户端是否有权限调用接口
havePermision: function(serverName, operationId) {
return true;
},
// RPC 服务工作端口
port: 4444,
// 定义可供消费的第三方 RPC 服务
clientProtoPath: path.resolve(__dirname, './client.proto'),
// 获取第三方 RPC 服务器地址
getExternalSrvAddr: function(srv) {
return srv + ':' + 4444
}
}
}
}
- 编写 server.proto
syntax = "proto3";
package myapp;
// The service definition.
service Service {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
- 编写 client.proto
syntax = "proto3";
package myapp;
// The external service definition.
service ExternalService {
// Sends a greeting
rpc SayGoodBye (GoodByeRequest) returns (GoodByeReply) {}
}
// The request message containing the user's name.
message GoodByeRequest {
string name = 1;
}
// The response message containing the greetings
message GoodByeReply {
string message = 1;
}
- 编写 handlers
module.exports = {
sayHello: function(ctx) {
ctx.srvs
return 'Goodby: ' + ctx.request.name;
}
}
server.proto 定义的函数将对外暴露
- client.proto 定义的函数可以被调用
req.srvs.rpc.ExternalService.call('sayGoodBye', {name}, function(err, response) {
})
// or promise
req.srvs.rpc.ExternalService.call('sayGoodBye', {name}).then(function(response) {
}).catch(function(err) {
})
Roadmap
- 添加 timeout, retry 选项
req.srvs.rpc.ExternalService.sayGoodBye({name}, {timeout: 1000, retry: 3})
- 添加 circuit breaker
services: {
rpc: {
constructor: 'grpc',
constructorArgs:
...
circuitBreaker: {
}
}
}
}
req.srvs.rpc.ExternalService.sayGoodBye({name}, {useCircuitBreaker: true})