This is an automated email from the ASF dual-hosted git repository. membphis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git
The following commit(s) were added to refs/heads/master by this push: new b230961 plugin(grpc-transcode): support new options for `protocol buffer` encoding. (#846) b230961 is described below commit b23096139e78064bd9ccf0e70b8539596ceab9c5 Author: tom2nonames <13682645...@139.com> AuthorDate: Thu Nov 28 09:07:09 2019 +0800 plugin(grpc-transcode): support new options for `protocol buffer` encoding. (#846) * bugfix: Adjust the MaxLength parameter in grpc proto from 4096b to 1M (#820) * change: grpc-transcode plugins add schema check --- doc/plugins/grpc-transcoding-cn.md | 71 +++++++++++++++++++++++++- lua/apisix/plugins/grpc-transcode.lua | 43 ++++++++++++++-- lua/apisix/plugins/grpc-transcode/request.lua | 15 ++++-- lua/apisix/plugins/grpc-transcode/response.lua | 10 +++- lua/apisix/plugins/grpc-transcode/util.lua | 6 ++- 5 files changed, 133 insertions(+), 12 deletions(-) diff --git a/doc/plugins/grpc-transcoding-cn.md b/doc/plugins/grpc-transcoding-cn.md index 200504f..f95b448 100644 --- a/doc/plugins/grpc-transcoding-cn.md +++ b/doc/plugins/grpc-transcoding-cn.md @@ -53,7 +53,7 @@ curl http://127.0.0.1:9080/apisix/admin/proto/1 -X PUT -d ' * `proto_id`: `.proto`内容的id. * `service`: grpc服务名. * `method`: grpc服务中要调用的方法名. - +* `option`: proto 编码过程中的转换选项. [ "int64_as_string" / "enum_as_value" / "auto_default_values" / "enable_hooks" ] ### 示例 @@ -107,3 +107,72 @@ Proxy-Connection: keep-alive 这表示已成功代理。 + +#### 使用 grpc-transcode 插件的 pb_option 选项 + +在指定 route 中,代理 grpc 服务接口: + +**选项清单** + * 枚举类型 + > enum_as_name + > enum_as_value + + * 64位整型 + > int64_as_number + > int64_as_string + > int64_as_hexstring + + * 使用默认值 + > auto_default_values + > no_default_values + > use_default_values + > use_default_metatable + + * Hooks开关 + > enable_hooks + > disable_hooks + +```shell +curl http://127.0.0.1:9080/apisix/admin/routes/23 -X PUT -d ' +{ + "methods": ["GET"], + "uri": "/zeebe/WorkflowInstanceCreate", + "service_protocol": "grpc", + "plugins": { + "grpc-transcode": { + "proto_id": "1", + "service": "gateway_protocol.Gateway", + "method": "CreateWorkflowInstance", + "pb_option":["int64_as_string"] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:26500": 1 + } + } +}' +``` + +#### 测试 + +访问上面配置的 route: + +``` +$ curl -i "http://127.0.0.1:9080/zeebe/WorkflowInstanceCreate?bpmnProcessId=order-process&version=1&variables=\{\"orderId\":\"7\",\"ordervalue\":99\}" +HTTP/1.1 200 OK +Date: Wed, 13 Nov 2019 03:38:27 GMT +Content-Type: application/json +Transfer-Encoding: chunked +Connection: keep-alive +grpc-encoding: identity +grpc-accept-encoding: gzip +Server: APISIX web server +Trailer: grpc-status +Trailer: grpc-message + +{"workflowKey":"#2251799813685260","workflowInstanceKey":"#2251799813688013","bpmnProcessId":"order-process","version":1} +``` + +`"workflowKey":"#2251799813685260"` 这表示已成功。 diff --git a/lua/apisix/plugins/grpc-transcode.lua b/lua/apisix/plugins/grpc-transcode.lua index 7791a4f..4c8191f 100644 --- a/lua/apisix/plugins/grpc-transcode.lua +++ b/lua/apisix/plugins/grpc-transcode.lua @@ -16,18 +16,53 @@ -- local ngx = ngx local core = require("apisix.core") +local schema_def = require("apisix.schema_def") local plugin_name = "grpc-transcode" local proto = require("apisix.plugins.grpc-transcode.proto") local request = require("apisix.plugins.grpc-transcode.request") local response = require("apisix.plugins.grpc-transcode.response") +local pb_option_def = { + { description = "enum as result", + type = "string", + enum = {"int64_as_number", + "int64_as_string", + "int64_as_hexstring"}, + }, + { description = "int64 as result", + type = "string", + enum = {"ienum_as_name", + "enum_as_value"}, + }, + { description ="default values option", + type = "string", + enum = {"auto_default_values", + "no_default_values", + "use_default_values", + "use_default_metatable"}, + }, + { description = "hooks option", + type = "string", + enum = {"enable_hooks", + "disable_hooks" }, + }, +} + local schema = { type = "object", - additionalProperties = true + properties = { + proto_id = schema_def.id_schema, + service = { type = "string" }, + method = { type = "string" }, + pb_option = { type = "array", + items = { type="string", anyOf = pb_option_def }, + minItems = 1, + }, + requried = { "proto_id", "service", "method" }, + additionalProperties = true } } - local _M = { version = 0.1, priority = 506, @@ -66,7 +101,7 @@ function _M.access(conf, ctx) return end - local ok, err = request(proto_obj, conf.service, conf.method) + local ok, err = request(proto_obj, conf.service, conf.method, conf.pb_option) if not ok then core.log.error("transform request error: ", err) return @@ -96,7 +131,7 @@ function _M.body_filter(conf, ctx) return end - local err = response(proto_obj, conf.service, conf.method) + local err = response(proto_obj, conf.service, conf.method, conf.pb_option) if err then core.log.error("transform response error: ", err) return diff --git a/lua/apisix/plugins/grpc-transcode/request.lua b/lua/apisix/plugins/grpc-transcode/request.lua index 86ad724..d985d66 100644 --- a/lua/apisix/plugins/grpc-transcode/request.lua +++ b/lua/apisix/plugins/grpc-transcode/request.lua @@ -22,9 +22,9 @@ local bit = require("bit") local ngx = ngx local string = string local table = table +local ipairs = ipairs - -return function (proto, service, method, default_values) +return function (proto, service, method, pb_option, default_values) core.log.info("proto: ", core.json.delay_encode(proto, true)) local m = util.find_method(proto, service, method) if not m then @@ -33,8 +33,15 @@ return function (proto, service, method, default_values) end ngx.req.read_body() - local encoded = pb.encode(m.input_type, - util.map_message(m.input_type, default_values or {})) + + if pb_option then + for _, opt in ipairs(pb_option) do + pb.option(opt) + end + end + + local map_message = util.map_message(m.input_type, default_values or {}) + local encoded = pb.encode(m.input_type, map_message) if not encoded then return false, "failed to encode request data to protobuf" diff --git a/lua/apisix/plugins/grpc-transcode/response.lua b/lua/apisix/plugins/grpc-transcode/response.lua index 617a07a..e764938 100644 --- a/lua/apisix/plugins/grpc-transcode/response.lua +++ b/lua/apisix/plugins/grpc-transcode/response.lua @@ -20,9 +20,9 @@ local pb = require("pb") local ngx = ngx local string = string local table = table +local ipairs = ipairs - -return function(proto, service, method) +return function(proto, service, method, pb_option) local m = util.find_method(proto, service, method) if not m then return false, "2.Undefined service method: " .. service .. "/" .. method @@ -51,6 +51,12 @@ return function(proto, service, method) buffer = string.sub(buffer, 6) end + if pb_option then + for _, opt in ipairs(pb_option) do + pb.option(opt) + end + end + local decoded = pb.decode(m.output_type, buffer) if not decoded then ngx.arg[1] = "failed to decode response data by protobuf" diff --git a/lua/apisix/plugins/grpc-transcode/util.lua b/lua/apisix/plugins/grpc-transcode/util.lua index 3d9deba..83d89ab 100644 --- a/lua/apisix/plugins/grpc-transcode/util.lua +++ b/lua/apisix/plugins/grpc-transcode/util.lua @@ -67,7 +67,11 @@ local function get_from_request(name, kind) if prefix == "int" then if request_table[name] then - return tonumber(request_table[name]) + if kind == "int64" then + return request_table[name] + else + return tonumber(request_table[name]) + end end end