如何在Go中启用GRPC压缩

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

我的第一次尝试,</ p>

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

</ code> </ pre>

在我拨打电话时导致此错误:</ p>

  grpc:grpc-未安装解压缩器 编码\“ gzip \” 
</ code> </ pre>
</ div>

展开原文

原文

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个回答

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"
dongya5893
dongya5893 完成,谢谢指出
2 年多之前 回复
doutidi5037
doutidi5037 谢谢回答。 我认为您应该添加服务器应如何导入gzip软件包。 我最终发现了所有这些:github.com/grpc/grpc-go/pull/1820/files
2 年多之前 回复

可以在以下位置找到有关压缩API的

展开原文

原文

drl9940
drl9940 谢谢; 这是半有用的,因为它命名了要调用的函数,但未显示如何使用它们。 另外,“如果目标站点无法访问或永久脱机,请始终引用重要链接中最相关的部分。” stackoverflow.com/help/how-to-answer。
接近 3 年之前 回复
立即提问
相关内容推荐