Golang gRPC连接客户端出现错误 – “读取服务器前言时出错:http2:帧太大”

huangapple go评论180阅读模式
英文:

Golang gRPC connection client side error - "error reading server preface: http2: frame too large"

问题

我正在写一个Go客户端来消费以下代码:

  1. conn, err := grpc.DialContext(ctx, serverAddr, grpc.WithBlock(), grpc.WithReturnConnectionError(), getTransportCredential(false))

上述调用会在上下文超时时挂起,并返回以下错误:

  1. failed to dial: context deadline exceeded: connection error: desc = "error reading server preface: http2: frame too large"

下面是定义的getTransportCredential(insecure bool)函数:

  1. func getTransportCredential(insecure bool) grpc.DialOption {
  2. if insecure {
  3. return grpc.WithTransportCredentials(insecure2.NewCredentials())
  4. }
  5. rootCAs, err := x509.SystemCertPool()
  6. if err != nil {
  7. panic(err)
  8. }
  9. if rootCAs == nil {
  10. fmt.Println("SystemCertPool is nil")
  11. rootCAs = x509.NewCertPool()
  12. }
  13. caCert := `-----BEGIN CERTIFICATE-----
  14. -----END CERTIFICATE-----`
  15. if caCert != "" {
  16. // 将我们的证书添加到系统池中
  17. if ok := rootCAs.AppendCertsFromPEM([]byte(caCert)); !ok {
  18. fmt.Println("Couldn't add cert to the cert pool")
  19. }
  20. }
  21. creds := credentials.NewClientTLSFromCert(rootCAs, "")
  22. fmt.Printf("%+v\n", creds.Info())
  23. return grpc.WithTransportCredentials(creds)
  24. }

请问你能帮我解决这个问题吗?

我可以从我的机器上使用grpcurl命令向服务器发送请求并获得成功的响应。

英文:

I'm writing a go client to consume

  1. conn, err := grpc.DialContext(ctx, serverAddr, grpc.WithBlock(), grpc.WithReturnConnectionError(), getTransportCredential(false))

The above call hangs until context timeout and returns the following error

  1. failed to dial: context deadline exceeded: connection error: desc = "error reading server preface: http2: frame too large"

getTransportCredential(insecure bool) is defined below

  1. func getTransportCredential(insecure bool) grpc.DialOption {
  2. if insecure {
  3. return grpc.WithTransportCredentials(insecure2.NewCredentials())
  4. }
  5. rootCAs, err := x509.SystemCertPool()
  6. if err != nil {
  7. panic(err)
  8. }
  9. if rootCAs == nil {
  10. fmt.Println("SystemCertPool is nil")
  11. rootCAs = x509.NewCertPool()
  12. }
  13. caCert := `-----BEGIN CERTIFICATE-----
  14. -----END CERTIFICATE-----`
  15. if caCert != "" {
  16. // Append our cert to the system pool
  17. if ok := rootCAs.AppendCertsFromPEM([]byte(caCert)); !ok {
  18. fmt.Println("Couldn't add cert to the cert pool")
  19. }
  20. }
  21. creds := credentials.NewClientTLSFromCert(rootCAs, "")
  22. fmt.Printf("%+v\n", creds.Info())
  23. return grpc.WithTransportCredentials(creds)
  24. }

Could you please help me solve this problem?

I can grpcurl from my machine to the server and get a successful response.

答案1

得分: 2

HTTP/2的最大帧大小是2^14(16384)Golang gRPC连接客户端出现错误 – “读取服务器前言时出错:http2:帧太大”。你需要减小负载大小。请参考:https://www.rfc-editor.org/rfc/rfc7540#page-12,第76页。

英文:

maximum frame size of http/2 is 2^14 (16384)Golang gRPC连接客户端出现错误 – “读取服务器前言时出错:http2:帧太大”
you need to reduce yourself payload,

refer to: https://www.rfc-editor.org/rfc/rfc7540#page-12, page 76

huangapple
  • 本文由 发表于 2023年2月2日 10:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75318063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定