當我從 Dart 呼叫我的 gRPC Golang 服務器時出現此錯誤:
捕獲的錯誤:gRPC 錯誤(代碼:12,代號:未實作,訊息:grpc:未為 grpc 編碼“gzip”安裝解壓器,詳細資訊:[],rawResponse:null,預告片:{})
我已閱讀https://github.com/bradleyjkemp/grpc-tools/issues/19,它似乎不適用于我的問題。
服務器在 Gcloud Ubuntu 上運行 1.19.2。Dart 在 Mac Monterey 上運行 2.18.2
我有一個 Dart 客戶端呼叫 Go 服務器。兩者似乎都使用 GZIP 進行壓縮。
飛鏢原型
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
去原型:
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Dart 客戶端代碼:
import 'package:grpc/grpc.dart';
import 'package:helloworld/src/generated/helloworld.pbgrpc.dart';
Future<void> main(List<String> args) async {
final channel = ClientChannel(
'ps-dev1.savup.com',
port: 54320,
options: ChannelOptions(
credentials: ChannelCredentials.insecure(),
codecRegistry:
CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
),
);
final stub = GreeterClient(channel);
final name = args.isNotEmpty ? args[0] : 'world';
try {
final response = await stub.sayHello(
HelloRequest()..name = name,
options: CallOptions(compression: const GzipCodec()),
);
print('Greeter client received: ${response.message}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
Go gRPC 服務器與 Go gRPC 客戶端和 BloomRPC 一起作業得很好。
我對 gRPC 很陌生,對 Dart 也很陌生。
提前感謝您提供解決此問題的任何幫助。
uj5u.com熱心網友回復:
您分享的那個錯誤表明您的服務器不支持 gzip 壓縮。
最快的解決方法是在客戶端的呼叫選項中不使用 gzip 壓縮,方法是洗掉以下行:
options: CallOptions(compression: const GzipCodec()),
來自你的 Dart 代碼。
go-grpc 庫在packagegithub.com/grpc/grpc-go/encoding/gzip中有一個 gzip 壓縮編碼的實作,但它是實驗性的,所以在生產中使用它可能不明智;或者至少你應該密切關注它:
// Package gzip implements and registers the gzip compressor
// during the initialization.
//
// Experimental
//
// Notice: This package is EXPERIMENTAL and may be changed or removed in a
// later release.
如果你想在你的服務器中使用它,你只需要匯入包;包中沒有面向用戶的代碼:
import (
_ "github.com/grpc/grpc-go/encoding/gzip"
)
關于 grpc-go 壓縮的檔案提到了上面的這個包作為你如何實作這種壓縮器的一個例子。
因此,您可能還希望將代碼復制到更穩定的位置并自行負責維護,直到有穩定的受支持版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514600.html
標籤:镖去grpc
