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

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

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

问题

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

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

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

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

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

func getTransportCredential(insecure bool) grpc.DialOption {
	if insecure {
		return grpc.WithTransportCredentials(insecure2.NewCredentials())
	}

	rootCAs, err := x509.SystemCertPool()
	if err != nil {
		panic(err)
	}
	if rootCAs == nil {
		fmt.Println("SystemCertPool is nil")
		rootCAs = x509.NewCertPool()
	}

	caCert := `-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----`

	if caCert != "" {
		// 将我们的证书添加到系统池中
		if ok := rootCAs.AppendCertsFromPEM([]byte(caCert)); !ok {
			fmt.Println("Couldn't add cert to the cert pool")
		}
	}

	creds := credentials.NewClientTLSFromCert(rootCAs, "")
	fmt.Printf("%+v\n", creds.Info())
	return grpc.WithTransportCredentials(creds)
}

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

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

英文:

I'm writing a go client to consume

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

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

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

getTransportCredential(insecure bool) is defined below

func getTransportCredential(insecure bool) grpc.DialOption {
	if insecure {
		return grpc.WithTransportCredentials(insecure2.NewCredentials())
	}

	rootCAs, err := x509.SystemCertPool()
	if err != nil {
		panic(err)
	}
	if rootCAs == nil {
		fmt.Println("SystemCertPool is nil")
		rootCAs = x509.NewCertPool()
	}

	caCert := `-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----`

	if caCert != "" {
		// Append our cert to the system pool
		if ok := rootCAs.AppendCertsFromPEM([]byte(caCert)); !ok {
			fmt.Println("Couldn't add cert to the cert pool")
		}
	}

	creds := credentials.NewClientTLSFromCert(rootCAs, "")
	fmt.Printf("%+v\n", creds.Info())
	return grpc.WithTransportCredentials(creds)
}

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:

确定