2019-10-31 09:27:28 +08:00
|
|
|
|
<!--
|
|
|
|
|
#
|
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
-->
|
|
|
|
|
|
2020-08-12 22:54:11 +08:00
|
|
|
|
- [English](../../plugins/grpc-transcode.md)
|
2020-03-22 20:46:14 +08:00
|
|
|
|
|
2020-06-11 12:03:12 +08:00
|
|
|
|
# grpc-transcode
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
HTTP(s) -> APISIX -> gRPC server
|
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
## Proto
|
|
|
|
|
|
|
|
|
|
### 参数
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
* `content`: `.proto` 文件的内容
|
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
### 添加proto
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
路径中最后的数字,会被用作 proto 的 id 做唯一标识,比如下面示例的 proto `id` 是 `1` :
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-03-05 14:48:27 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/proto/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-08-21 23:10:34 +08:00
|
|
|
|
{
|
2019-09-03 13:53:41 +08:00
|
|
|
|
"content" : "syntax = \"proto3\";
|
|
|
|
|
package helloworld;
|
|
|
|
|
service Greeter {
|
|
|
|
|
rpc SayHello (HelloRequest) returns (HelloReply) {}
|
|
|
|
|
}
|
|
|
|
|
message HelloRequest {
|
|
|
|
|
string name = 1;
|
|
|
|
|
}
|
|
|
|
|
message HelloReply {
|
|
|
|
|
string message = 1;
|
|
|
|
|
}"
|
2019-08-21 23:10:34 +08:00
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
## 参数列表
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
|
|
|
|
|
| --------- | -------------------------------------------------------------------------- | ------ | ------ | ------ | -------------------------- |
|
|
|
|
|
| proto_id | string/integer | 必须 | | | `.proto` 内容的 id |
|
|
|
|
|
| service | string | 必须 | | | grpc 服务名 |
|
|
|
|
|
| method | string | 必须 | | | grpc 服务中要调用的方法名 |
|
|
|
|
|
| deadline | number | 可选 | 0 | | grpc deadline, ms |
|
|
|
|
|
| pb_option | array[string([pb_option_def](#使用-grpc-transcode-插件的-pb_option-选项))] | 可选 | | | proto 编码过程中的转换选项 |
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
## 示例
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
### 使用 grpc-transcode 插件
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
在指定 route 中,代理 grpc 服务接口:
|
|
|
|
|
|
2019-11-05 10:58:04 +08:00
|
|
|
|
* 注意: 这个 route 的属性`service_protocol` 必须设置为 `grpc`
|
2020-09-23 08:11:27 +08:00
|
|
|
|
* 代理 grpc 服务例子可参考:[grpc_server_example](https://github.com/iresty/grpc_server_example)
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-03-05 14:48:27 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/111 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-08-21 23:10:34 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/grpctest",
|
|
|
|
|
"service_protocol": "grpc",
|
|
|
|
|
"plugins": {
|
2019-08-22 14:40:56 +08:00
|
|
|
|
"grpc-transcode": {
|
2019-08-21 23:10:34 +08:00
|
|
|
|
"proto_id": "1",
|
|
|
|
|
"service": "helloworld.Greeter",
|
|
|
|
|
"method": "SayHello"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:50051": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
### 测试
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
访问上面配置的 route:
|
|
|
|
|
|
|
|
|
|
```shell
|
2019-08-24 09:13:52 +08:00
|
|
|
|
$ curl -i http://127.0.0.1:9080/grpctest?name=world
|
2019-08-21 23:10:34 +08:00
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
|
Date: Fri, 16 Aug 2019 11:55:36 GMT
|
|
|
|
|
Content-Type: application/json
|
|
|
|
|
Transfer-Encoding: chunked
|
|
|
|
|
Connection: keep-alive
|
|
|
|
|
Server: APISIX web server
|
|
|
|
|
Proxy-Connection: keep-alive
|
|
|
|
|
|
|
|
|
|
{"message":"Hello world"}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这表示已成功代理。
|
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
## 使用 grpc-transcode 插件的 pb_option 选项
|
2019-11-28 09:07:09 +08:00
|
|
|
|
|
|
|
|
|
在指定 route 中,代理 grpc 服务接口:
|
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
### 选项清单
|
2020-03-22 20:46:14 +08:00
|
|
|
|
|
|
|
|
|
* 枚举类型
|
2020-09-23 08:11:27 +08:00
|
|
|
|
* enum_as_name
|
|
|
|
|
* enum_as_value
|
2019-11-28 09:07:09 +08:00
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
* 64位整型
|
2020-09-23 08:11:27 +08:00
|
|
|
|
* int64_as_number
|
|
|
|
|
* int64_as_string
|
|
|
|
|
* int64_as_hexstring
|
2019-11-28 09:07:09 +08:00
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
* 使用默认值
|
2020-09-23 08:11:27 +08:00
|
|
|
|
* auto_default_values
|
|
|
|
|
* no_default_values
|
|
|
|
|
* use_default_values
|
|
|
|
|
* use_default_metatable
|
2019-11-28 09:07:09 +08:00
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
* Hooks开关
|
2020-09-23 08:11:27 +08:00
|
|
|
|
* enable_hooks
|
|
|
|
|
* disable_hooks
|
2019-11-28 09:07:09 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-03-05 14:48:27 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/23 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-11-28 09:07:09 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/zeebe/WorkflowInstanceCreate",
|
|
|
|
|
"service_protocol": "grpc",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"grpc-transcode": {
|
2020-09-23 08:11:27 +08:00
|
|
|
|
"proto_id": "1",
|
|
|
|
|
"service": "gateway_protocol.Gateway",
|
|
|
|
|
"method": "CreateWorkflowInstance",
|
|
|
|
|
"pb_option":["int64_as_string"]
|
2019-11-28 09:07:09 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:26500": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
### 测试 pb_option 参数
|
2019-11-28 09:07:09 +08:00
|
|
|
|
|
|
|
|
|
访问上面配置的 route:
|
|
|
|
|
|
2020-03-22 20:46:14 +08:00
|
|
|
|
```shell
|
2019-11-28 09:07:09 +08:00
|
|
|
|
$ 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}
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
`"workflowKey":"#2251799813685260"` 表示已成功。
|