【问题标题】:How to define a gRPC service that handles any call?如何定义处理任何调用的 gRPC 服务?
【发布时间】:2020-02-21 16:39:53
【问题描述】:

我正在尝试做一些逆向工程,试图了解在几个 .proto 文件中调用了哪些服务。我的问题是是否有可能在服务器上实现一个服务来处理所有的调用,并为我提供调用了哪个函数和服务的信息。

示例:

protocol.proto

syntax = "proto3";

package greating;

message PersonRequest {
    string name = 1;
    int32 age = 2;
}

message PersonResponse {
    string message = 1;
}

service GreatingService {
    rpc helloPerson (PersonRequest) returns (PersonResponse);
} 

service WarningService {
    rpc attentionPerson (PersonRequest) returns (PersonResponse);
}


server.js

const packageDefinition = protoLoader.loadSync("greating.proto");
const greatingProto = grpc.loadPackageDefinition(packageDefinition);

var server = new grpc.Server();

server.addService(greatingProto.greating.GreatingService.service, {
    helloPerson: function(call, callback) {
        let response = `Hello ${call.request.name}! You're ${call.request.age} years old`;

        return callback(null, { message: response });
    }
});

server.addService(greatingProto.greating.WarningService.service, {
    helloPerson: function(call, callback) {

        let response = `Attention ${call.request.name}! You're  ${call.request.age} years left to live`;

        return callback(null, { message: response });
    }
});


我想要做的是实现处理两个(所有)调用的第三个函数,并显示调用了哪个服务。像这样的:

server.addService("*", {
    function(call, callback) {
        let response = `The service ${call.service}, function ${call.function} was called.`;

        return callback(null, { message: response });
    }
});

有没有办法做到这一点?

谢谢。

【问题讨论】:

  • 我认为这不会影响您的问题,但在您处理WarningService 的代码中,您传递的是一个名为helloPerson 的函数,而不是attentionPerson。我相信,如果您运行该代码,您应该会看到一条警告,指出未提供 attentionPerson 的实现。
  • 你是对的。那是一个复制粘贴错误。比你。

标签: protocol-buffers grpc protobuf.js grpc-node grpc-web


【解决方案1】:

不,grpc 不支持通配符方法处理程序,或任何其他使用单个处理程序处理每个传入请求的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2019-11-04
    • 2017-11-22
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多