如何使用 envoy-proxy 在 Grpc 中将错误响应发送为 json

How to send error response as json in Grpc using envoy-proxy

我使用 resonseObserver.onError() 从 grpc 服务抛出错误,但在从休息客户端点击 REST API 时我没有收到 json 格式的消息,尽管积极的情况是工作正常并给出 json 的响应。

我正在使用 envoy 作为转码器,任何人都可以帮助我如何获得错误响应以及 json。目前我收到关于错误场景的 BadRequest。项目在SpringBoot中

TIA

您可以使用 convert_grpc_status: true 来执行此操作。

      http_filters:
      - name: envoy.filters.http.grpc_json_transcoder
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
          proto_descriptor: "/tmp/envoy/proto.pb"
          services: ["xxxxxxxx"]
          convert_grpc_status: true
          print_options:
            always_print_primitive_fields: true
            always_print_enums_as_ints: false
            preserve_proto_field_names: false

如果你的意思是 return 详细信息 键是这样的:

{
  "code": 3,
  "message": "API call quota depleted",
  "details": [
    {
      "@type": "type.googleapis.com/google.rpc.ResourceInfo",
      "resourceType": "xxxxxx",
      "resourceName": "",
      "owner": "",
      "description": ""
    }
  ]
}

你必须编译你的 .proto 文件:

import "google/rpc/error_details.proto";

因为 Envoy 无法在没有错误类型的情况下从您的后端服务器反序列化二进制详细信息。

您还可以阅读如何使用 Python 发送详细的错误响应: