我如何使Go gRPC在服务器端使用标准IO进行工作? [英] How do I make a Go gRPC work with standard IO on the server side?

查看:125
本文介绍了我如何使Go gRPC在服务器端使用标准IO进行工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是gRPC的新生,这是我的问题。我试图编写一个服务,按照以下服务方法将 myOwnService 暴露给gRPC服务:



<$ p $ (HighRequest)返回(流HighReply){}

服务器端代码如下:

  func(s * server)HighFive(stream pb.Greeter_HighFiveServer)error {
//哎呀,不知道该怎么办...
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 code> [] byte value via 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 with io.Reader and io.Writer in my original service, so that the caller of the HighFive gRPC service can read and write data just like calling myOwnService 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's Recv method will give you a decoded HighRequest message, not a byte stream, as you're expecting for your myOwnService function.

Now, if your HighRequest message contains a field of types bytes or string, you may wish to feed that field's content into myOwnService as the stdin parameter by wrapping the raw []byte value via bytes.NewReader.

I see, though, that myOwnService demands an io.ReadCloser. I don't know why you'd expect myOwnService to close its input parameter, but I'll trust you well enough that you need it to then recommend using ioutil.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆