登录
首页 >  Golang >  Go问答

解决问题:如何安装 grpc 编码解压缩器?

来源:stackoverflow

时间:2024-02-06 10:51:23 324浏览 收藏

今天golang学习网给大家带来了《解决问题:如何安装 grpc 编码解压缩器?》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

当我从 dart 调用 grpc golang 服务器时收到此错误:

捕获错误:grpc 错误(代码:12,codename:unimplmented,消息: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 进行压缩。

dart 原型

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;
}

go 原型:

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 main(List 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 也很陌生。

预先感谢您为解决此问题提供的任何帮助。


正确答案


您分享的错误表明您的服务器不支持 gzip 压缩。

最快的解决方法是在客户端的调用选项中不使用 gzip 压缩,方法是删除以下行:

options: calloptions(compression: const gzipcodec()),

来自您的 dart 代码。

go-grpc 库在 package github.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"
)

documentation about compression for grpc-go 提到上述包作为如何实现此类压缩器的示例

因此,您可能还想将代码复制到更稳定的位置,并自行负责维护它,直到有稳定的受支持版本为止。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《解决问题:如何安装 grpc 编码解压缩器?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>