我如何使Go gRPC在服务器端使用标准IO进行工作? [英] How do I make a Go gRPC work with standard IO on the server side?
问题描述
我是gRPC的新生,这是我的问题。我试图编写一个服务,按照以下服务方法将 myOwnService
暴露给gRPC服务:
<$ p $ (HighRequest)返回(流HighReply){}
服务器端代码如下:
func(s * server)HighFive(stream pb.Greeter_HighFiveServer)error {
code> [] byte value via
//哎呀,不知道该怎么办...
myOwnService(stdin io.ReadCloser,stdout io.WriteCloser)
return nil
}
func myOwnService(stdin io.ReadCloser,stdout io.WriteCloser)error {
//从stdin读取输入,执行某些操作,将结果写入stdout
...
返回nil
$ b正如你所看到的,我不知道如何使
stream
在我的原始服务中使用io.Reader
和io.Writer
以便HighFive
gRPC服务的调用者可以读写数据j你可以改变它们,如果你喜欢,可以改变它们。必要:
消息HighRequest {
bytes content = 1;
}
消息HighReply {
bytes content = 1;
解决方案Per 关于双向流式RPC的gRPC基础教程部分,每次打电话给您
stream
参数的Recv
方法会给你解码HighRequest
消息,不是字节流,因为您期望您的myOwnService
函数。
现在,如果
HighRequest
消息包含类型字节
或字符串
,您可能希望将该字段的内容作为stdin
参数通过包装原始c> myOwnService bytes .NewReader
。
然而,我发现
myOwnService
需求一个io.ReadCloser
。我不知道为什么你期望myOwnService
关闭它的输入参数,但是我会相当信任你,所以你需要它然后推荐使用ioutil.NopCloser
可以轻松满足这种需求。
素描:
//下一个标记:2
消息HighRequest {
bytes content = 1;
}
func(s * server)HighFive(stream pb.Greeter_HighFiveServer)error {
for req,err:= stream.Recv(); {
if err!= nil {
if err == io.EOF {
return nil
}
return err
}
in := ioutil.NopCloser(bytes.NewReader(req.Content))
out:= / * ... * /
if err:= myOwnService(in,out); err!= nil {
return err
}
}
}
I am a freshman of gRPC, and here is my problem. I'm trying to write a service to expose
myOwnService
into a gRPC service per the following service method:rpc HighFive (stream HighRequest) returns (stream HighReply) {}
The server-side code is as follows:
func (s *server) HighFive(stream pb.Greeter_HighFiveServer) error { // Oops, don't know how to do here ... myOwnService(stdin io.ReadCloser, stdout io.WriteCloser) return nil } func myOwnService(stdin io.ReadCloser, stdout io.WriteCloser) error { // read input from stdin, do something, the write result to stdout ... return nil }
As you can see above, I have no idea how to make
stream
work withio.Reader
andio.Writer
in my original service, so that the caller of theHighFive
gRPC service can read and write data just like callingmyOwnService
normally.[Update] My current messages are like this, but you can change them if necessary:
message HighRequest { bytes content = 1; } message HighReply { bytes content = 1; }
解决方案Per the gRPC Basics tutorial section on Bidirectional streaming RPC, each call to your
stream
parameter'sRecv
method will give you a decodedHighRequest
message, not a byte stream, as you're expecting for yourmyOwnService
function.Now, if your
HighRequest
message contains a field of typesbytes
orstring
, you may wish to feed that field's content intomyOwnService
as thestdin
parameter by wrapping the raw[]byte
value viabytes.NewReader
.I see, though, that
myOwnService
demands anio.ReadCloser
. I don't know why you'd expectmyOwnService
to close its input parameter, but I'll trust you well enough that you need it to then recommend usingioutil.NopCloser
to satisfy that demand trivially.Sketching:
// Next tag: 2 message HighRequest { bytes content = 1; } func (s *server) HighFive(stream pb.Greeter_HighFiveServer) error { for req, err := stream.Recv(); { if err != nil { if err == io.EOF { return nil } return err } in := ioutil.NopCloser(bytes.NewReader(req.Content)) out := /* ... */ if err := myOwnService(in, out); err != nil { return err } } }
这篇关于我如何使Go gRPC在服务器端使用标准IO进行工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!