doumicheng6732
2018-01-09 16:28
浏览 1.2k
已采纳

如何在Go中启用GRPC压缩

I see some methods WithCompression and UseCompression but I'm not very clear how they all fit together - a simple example of what to put in the client and what to put in the server would really help.

My first attempt,

conn, err := grpc.Dial(
    addr,
    grpc.WithTimeout(timeout),
    grpc.WithCompressor(grpc.NewGZIPCompressor()),
    ...

resulted in this error when I made a call:

grpc: Decompressor is not installed for grpc-encoding \"gzip\"

图片转代码服务由CSDN问答提供 功能建议

我看到了 WithCompression UseCompression 的一些方法,但我 我不太清楚它们是如何组合在一起的-一个简单的示例来说明如何将客户端放入服务器以及将什么放入服务器确实会有所帮助。

我的第一次尝试,

  conn,err:= grpc.Dial(
 addr,
 grpc.WithTimeout(timeout),
 grpc.WithCompressor(grpc.NewGZIPCompressor()),
 ...  
   
 
 

在我拨打电话时导致此错误:

  grpc:grpc-未安装解压缩器 编码\“ gzip \” 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyi1996 2018-03-27 08:03
    已采纳

    grpc.WithCompressor as DialOption is deprecated, according to the docs.

    You can use the grpc.UseCompressor(gzip.Name) CallOption, which operates at call level

    var opts []grpc.CallOption
    opts = append(callOptions, grpc.UseCompressor(gzip.Name))
    opts = append(callOptions, ...)
    opts = append(callOptions, ...)
    
    err := grpc.Invoke(mycontext, "/myRpcFuntion", myInput, myOutput, myGrpcConn, opts...)
    

    You do not need to call RegisterCompressor. The gzip package import does it for you in the init() function

    func init() {
        c := &compressor{}
        c.poolCompressor.New = func() interface{} {
            return &writer{Writer: gzip.NewWriter(ioutil.Discard), pool: &c.poolCompressor}
        }
        encoding.RegisterCompressor(c)
    }
    

    At the server side, you need to import the gzip package to make sure that the gzip compressor is registered.

    import _ "google.golang.org/grpc/encoding/gzip"
    
    已采纳该答案
    打赏 评论
  • douxiajia6720 2018-01-09 23:59
    打赏 评论

相关推荐 更多相似问题