我希望使用 grpc-node
客户端与使用 go-micro
框架在 Go
中构建的微服务进行通信。我遇到了一个问题,其中 go-micro
使用句点 (.
) 定义方法名称来分隔命名空间和方法名称,而 grpc-node
斜杠 (/
)。无论如何,是否可以配置此模式以使这两个进程相互通信?
最佳答案
gRPC over HTTP/2 protocol documentation定义路径构造如下:
Path → ":path" "/" Service-Name "/" {method name}
附上此附加说明
Some gRPC implementations may allow the Path format shown above to be overridden, but this functionality is strongly discouraged. gRPC does not go out of its way to break users that are using this kind of override, but we do not actively support it, and some functionality (e.g., service config support) will not work when the path is not of the form shown above.
因此,Node gRPC 客户端遵循规范,并且 go-micro 使用的替代格式似乎是硬编码在其代码生成插件 ( here ) 中。我认为这是一个错误。
话虽如此,有一个可行的解决方法可以匹配 Node gRPC 库中的方法名称格式。当您在 Node 中加载 .proto
文件时,每个客户端构造函数都有一个 service
成员,它是描述服务的纯 JavaScript 对象。它是方法名称到方法定义的映射,每个方法定义都包含一个 path
成员。您可以修改每个方法的路径以匹配 go-micro 使用的模式,然后将生成的服务对象传递给 grpc.makeGenericClientConstructor 以获得连接到修改后的服务的新客户端构造函数。
关于node.js - 有没有办法为 grpc-node 客户端配置生成方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308482/