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

如何在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\"
  • 写回答

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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 理想汽车的ADB为什么到了国外换了SIM就可以打开?
  • ¥15 k210烧入flash报错error:2005
  • ¥15 GIS土地利用预测FLUS模型
  • ¥15 visual studio code打不出来中文双引号
  • ¥20 关于#matlab#的问题:ps:完美符合要求有打赏(相关搜索:自适应)
  • ¥15 声音感应灯,问题出在哪里
  • ¥15 关于#matlab#的问题:(0,0)上的贝塞尔曲线,0)上的贝塞尔曲线,并使用自适应积分计算其从t=0到不大于1的任意值区间的弧长
  • ¥20 关于#r语言#rda分析作图的问题,请各位专家解答!
  • ¥50 找回 股票行情系统的资源问题
  • ¥15 gwas 分析-plink 检查人口分层出现下面问题