grpc-transcode

描述

使用 grpc-transcode 插件可以在 HTTP 和 gRPC 请求之间进行转换。

APISIX 接收 HTTP 请求后,首先对请求进行转码,并将转码后的请求转发到 gRPC 服务,获取响应并以 HTTP 格式将其返回给客户端。

属性

名称类型必选项默认值
proto_idstring/integer
servicestring
methodstring
deadlinenumber0
pb_optionarray[string(pb_option_def)]
show_status_in_bodybooleanfalse
status_detail_typestring

pb_option 的选项

类型有效值
enum as resultenum_as_name, enum_as_value
int64 as resultint64_as_number, int64_as_string, int64_as_hexstring
default valuesauto_default_values, no_default_values, use_default_values, use_default_metatable
hooksenable_hooks, disable_hooks

启用插件

在启用插件之前,你必须将 .proto.pb 文件的内容添加到 APISIX。

可以使用 /admin/protos/id 接口将文件的内容添加到 content 字段:

  1. curl http://127.0.0.1:9180/apisix/admin/protos/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "content" : "syntax = \"proto3\";
  5. package helloworld;
  6. service Greeter {
  7. rpc SayHello (HelloRequest) returns (HelloReply) {}
  8. }
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. message HelloReply {
  13. string message = 1;
  14. }"
  15. }'

如果你的 .proto 文件包含 import,或者想要把多个 .proto 文件合并成一个 proto,你可以生成一个 .pb 文件并在 APISIX 中使用它。

假设已经有一个 .proto 文件(proto/helloworld.proto),它导入了另一个 proto 文件:

  1. syntax = "proto3";
  2. package helloworld;
  3. import "proto/import.proto";
  4. ...

首先,让我们从 .proto 文件创建一个 .pb 文件。

  1. protoc --include_imports --descriptor_set_out=proto.pb proto/helloworld.proto

输出的二进制文件 proto.pb 将同时包含 helloworld.protoimport.proto

然后将 proto.pb 的内容作为 proto 的 content 字段提交。

由于 proto 的内容是二进制的,我们需要使用以下 shell 命令将其转换成 base64

  1. curl http://127.0.0.1:9180/apisix/admin/protos/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "content" : "'"$(base64 -w0 /path/to/proto.pb)"'"
  5. }'

返回 HTTP/1.1 201 Created 结果如下:

  1. {"node":{"value":{"create_time":1643879753,"update_time":1643883085,"content":"CmgKEnByb3RvL2ltcG9ydC5wcm90bxIDcGtnIhoKBFVzZXISEgoEbmFtZRgBIAEoCVIEbmFtZSIeCghSZXNwb25zZRISCgRib2R5GAEgASgJUgRib2R5QglaBy4vcHJvdG9iBnByb3RvMwq9AQoPcHJvdG8vc3JjLnByb3RvEgpoZWxsb3dvcmxkGhJwcm90by9pbXBvcnQucHJvdG8iPAoHUmVxdWVzdBIdCgR1c2VyGAEgASgLMgkucGtnLlVzZXJSBHVzZXISEgoEYm9keRgCIAEoCVIEYm9keTI5CgpUZXN0SW1wb3J0EisKA1J1bhITLmhlbGxvd29ybGQuUmVxdWVzdBoNLnBrZy5SZXNwb25zZSIAQglaBy4vcHJvdG9iBnByb3RvMw=="},"key":"\/apisix\/proto\/1"}}

现在我们可以在指定路由中启用 grpc-transcode 插件:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/grpctest",
  6. "plugins": {
  7. "grpc-transcode": {
  8. "proto_id": "1",
  9. "service": "helloworld.Greeter",
  10. "method": "SayHello"
  11. }
  12. },
  13. "upstream": {
  14. "scheme": "grpc",
  15. "type": "roundrobin",
  16. "nodes": {
  17. "127.0.0.1:50051": 1
  18. }
  19. }
  20. }'
grpc-transcode - 图1note

此处使用的 Upstream 应该是 gRPC 服务。请注意,scheme 需要设置为 grpc

可以使用 grpc_server_example 进行测试。

测试插件

通过上述示例配置插件后,你可以向 APISIX 发出请求以从 gRPC 服务(通过 APISIX)获得响应:

访问上面配置的 route:

  1. curl -i http://127.0.0.1:9080/grpctest?name=world

返回结果

  1. HTTP/1.1 200 OK
  2. Date: Fri, 16 Aug 2019 11:55:36 GMT
  3. Content-Type: application/json
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Server: APISIX web server
  7. Proxy-Connection: keep-alive
  8. {"message":"Hello world"}

你也可以配置 pb_option,如下所示:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/23 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/zeebe/WorkflowInstanceCreate",
  5. "plugins": {
  6. "grpc-transcode": {
  7. "proto_id": "1",
  8. "service": "gateway_protocol.Gateway",
  9. "method": "CreateWorkflowInstance",
  10. "pb_option":["int64_as_string"]
  11. }
  12. },
  13. "upstream": {
  14. "scheme": "grpc",
  15. "type": "roundrobin",
  16. "nodes": {
  17. "127.0.0.1:26500": 1
  18. }
  19. }
  20. }'

可以通过如下命令检查刚刚配置的路由:

  1. curl -i "http://127.0.0.1:9080/zeebe/WorkflowInstanceCreate?bpmnProcessId=order-process&version=1&variables=\{\"orderId\":\"7\",\"ordervalue\":99\}"
  1. HTTP/1.1 200 OK
  2. Date: Wed, 13 Nov 2019 03:38:27 GMT
  3. Content-Type: application/json
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. grpc-encoding: identity
  7. grpc-accept-encoding: gzip
  8. Server: APISIX web server
  9. Trailer: grpc-status
  10. Trailer: grpc-message
  11. {"workflowKey":"#2251799813685260","workflowInstanceKey":"#2251799813688013","bpmnProcessId":"order-process","version":1}

在返回体中展示 grpc-status-details-bin

如果 gRPC 服务返回了错误,返回头中可能存在 grpc-status-details-bin 字段对错误进行描述,你可以解码该字段,并展示在返回体中。

上传 proto 文件:

  1. curl http://127.0.0.1:9180/apisix/admin/protos/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "content" : "syntax = \"proto3\";
  5. package helloworld;
  6. service Greeter {
  7. rpc GetErrResp (HelloRequest) returns (HelloReply) {}
  8. }
  9. message HelloRequest {
  10. string name = 1;
  11. repeated string items = 2;
  12. }
  13. message HelloReply {
  14. string message = 1;
  15. repeated string items = 2;
  16. }"
  17. }'

启用 grpc-transcode 插件,并设置选项 show_status_in_bodytrue

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/grpctest",
  6. "plugins": {
  7. "grpc-transcode": {
  8. "proto_id": "1",
  9. "service": "helloworld.Greeter",
  10. "method": "GetErrResp",
  11. "show_status_in_body": true
  12. }
  13. },
  14. "upstream": {
  15. "scheme": "grpc",
  16. "type": "roundrobin",
  17. "nodes": {
  18. "127.0.0.1:50051": 1
  19. }
  20. }
  21. }'

访问上面配置的 route:

  1. curl -i http://127.0.0.1:9080/grpctest?name=world

返回结果

  1. HTTP/1.1 503 Service Temporarily Unavailable
  2. Date: Wed, 10 Aug 2022 08:59:46 GMT
  3. Content-Type: application/json
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. grpc-status: 14
  7. grpc-message: Out of service
  8. grpc-status-details-bin: CA4SDk91dCBvZiBzZXJ2aWNlGlcKKnR5cGUuZ29vZ2xlYXBpcy5jb20vaGVsbG93b3JsZC5FcnJvckRldGFpbBIpCAESHFRoZSBzZXJ2ZXIgaXMgb3V0IG9mIHNlcnZpY2UaB3NlcnZpY2U
  9. Server: APISIX web server
  10. {"error":{"details":[{"type_url":"type.googleapis.com\/helloworld.ErrorDetail","value":"\b\u0001\u0012\u001cThe server is out of service\u001a\u0007service"}],"message":"Out of service","code":14}}

注意返回体中还存在未解码的字段,如果需要解码该字段,需要在上传的 proto 文件中加上该字段对应的 message type

  1. curl http://127.0.0.1:9180/apisix/admin/protos/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "content" : "syntax = \"proto3\";
  5. package helloworld;
  6. service Greeter {
  7. rpc GetErrResp (HelloRequest) returns (HelloReply) {}
  8. }
  9. message HelloRequest {
  10. string name = 1;
  11. repeated string items = 2;
  12. }
  13. message HelloReply {
  14. string message = 1;
  15. repeated string items = 2;
  16. }
  17. message ErrorDetail {
  18. int64 code = 1;
  19. string message = 2;
  20. string type = 3;
  21. }"
  22. }'

同时配置选项 status_detail_typehelloworld.ErrorDetail

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/grpctest",
  6. "plugins": {
  7. "grpc-transcode": {
  8. "proto_id": "1",
  9. "service": "helloworld.Greeter",
  10. "method": "GetErrResp",
  11. "show_status_in_body": true,
  12. "status_detail_type": "helloworld.ErrorDetail"
  13. }
  14. },
  15. "upstream": {
  16. "scheme": "grpc",
  17. "type": "roundrobin",
  18. "nodes": {
  19. "127.0.0.1:50051": 1
  20. }
  21. }
  22. }'

此时就能返回完全解码后的结果

  1. HTTP/1.1 503 Service Temporarily Unavailable
  2. Date: Wed, 10 Aug 2022 09:02:46 GMT
  3. Content-Type: application/json
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. grpc-status: 14
  7. grpc-message: Out of service
  8. grpc-status-details-bin: CA4SDk91dCBvZiBzZXJ2aWNlGlcKKnR5cGUuZ29vZ2xlYXBpcy5jb20vaGVsbG93b3JsZC5FcnJvckRldGFpbBIpCAESHFRoZSBzZXJ2ZXIgaXMgb3V0IG9mIHNlcnZpY2UaB3NlcnZpY2U
  9. Server: APISIX web server
  10. {"error":{"details":[{"type":"service","message":"The server is out of service","code":1}],"message":"Out of service","code":14}}

删除插件

当你需要禁用 grpc-transcode 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/111 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/grpctest",
  4. "plugins": {},
  5. "upstream": {
  6. "scheme": "grpc",
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:50051": 1
  10. }
  11. }
  12. }'