2021-02-26 21:40:08 +08:00
|
|
|
|
---
|
|
|
|
|
title: grpc-transcode
|
|
|
|
|
---
|
|
|
|
|
|
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.
|
|
|
|
|
#
|
|
|
|
|
-->
|
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
|
## Name
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
HTTP(s) -> APISIX -> gRPC server
|
|
|
|
|
|
|
|
|
|
### Proto
|
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
|
#### Attributes
|
2020-03-22 19:49:40 +08:00
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
|
* `content`: `.proto` file's content.
|
|
|
|
|
|
|
|
|
|
#### Add a proto
|
|
|
|
|
|
|
|
|
|
Here's an example, adding a proto which `id` is `1`:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +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 19:49:40 +08:00
|
|
|
|
## Attribute List
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
| Name | Type | Requirement | Default | Valid | Description |
|
|
|
|
|
| --------- | ------------------------------------------------------------------------------ | ----------- | ------- | ----- | -------------------------------- |
|
|
|
|
|
| proto_id | string/integer | required | | | `.proto` content id. |
|
|
|
|
|
| service | string | required | | | the grpc service name. |
|
|
|
|
|
| method | string | required | | | the method name of grpc service. |
|
|
|
|
|
| deadline | number | optional | 0 | | deadline for grpc, ms |
|
2021-05-03 10:37:07 +08:00
|
|
|
|
| pb_option | array[string([pb_option_def](#use-pb_option-option-of-grpc-transcode-plugin))] | optional | | | protobuf options |
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
|
## How To Enable
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2019-08-22 14:40:56 +08:00
|
|
|
|
Here's an example, to enable the grpc-transcode plugin to specified route:
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2021-01-26 11:04:16 +08:00
|
|
|
|
* attention: the `scheme` in the route's upstream must be `grpc`
|
2019-11-14 11:57:16 +08:00
|
|
|
|
* the grpc server example:[grpc_server_example](https://github.com/iresty/grpc_server_example)
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +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",
|
|
|
|
|
"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": {
|
2021-01-26 11:04:16 +08:00
|
|
|
|
"scheme": "grpc",
|
2019-08-21 23:10:34 +08:00
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:50051": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
|
## Test Plugin
|
2019-08-21 23:10:34 +08:00
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
The above configuration proxy:
|
2020-03-22 19:49:40 +08:00
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
|
```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
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
response:
|
2020-03-22 19:49:40 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
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"}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This means that the proxying is working.
|
2020-09-23 08:11:27 +08:00
|
|
|
|
|
|
|
|
|
## Use pb_option option of grpc-transcode plugin
|
|
|
|
|
|
|
|
|
|
### option list
|
|
|
|
|
|
|
|
|
|
* enum as result
|
|
|
|
|
* enum_as_name
|
|
|
|
|
* enum_as_value
|
|
|
|
|
|
|
|
|
|
* int64 as result
|
|
|
|
|
* int64_as_number
|
|
|
|
|
* int64_as_string
|
|
|
|
|
* int64_as_hexstring
|
|
|
|
|
|
|
|
|
|
* default values option
|
|
|
|
|
* auto_default_values
|
|
|
|
|
* no_default_values
|
|
|
|
|
* use_default_values
|
|
|
|
|
* use_default_metatable
|
|
|
|
|
|
|
|
|
|
* hooks option
|
|
|
|
|
* enable_hooks
|
|
|
|
|
* disable_hooks
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/23 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2020-09-23 08:11:27 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/zeebe/WorkflowInstanceCreate",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"grpc-transcode": {
|
|
|
|
|
"proto_id": "1",
|
|
|
|
|
"service": "gateway_protocol.Gateway",
|
|
|
|
|
"method": "CreateWorkflowInstance",
|
|
|
|
|
"pb_option":["int64_as_string"]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
2021-01-26 11:04:16 +08:00
|
|
|
|
"scheme": "grpc",
|
2020-09-23 08:11:27 +08:00
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:26500": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Test pb_option
|
|
|
|
|
|
|
|
|
|
Visit configured route:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
$ 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-12-13 00:04:48 +08:00
|
|
|
|
`"workflowKey":"#2251799813685260"` suggests pb_option configuration success.
|
2020-09-26 09:21:01 +08:00
|
|
|
|
|
|
|
|
|
## Disable Plugin
|
|
|
|
|
|
|
|
|
|
Remove the corresponding json configuration in the plugin configuration to disable `grpc-transcode`.
|
|
|
|
|
APISIX plugins are hot-reloaded, therefore no need to restart APISIX.
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/111 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2020-09-26 09:21:01 +08:00
|
|
|
|
{
|
|
|
|
|
"uri": "/grpctest",
|
|
|
|
|
"plugins": {},
|
|
|
|
|
"upstream": {
|
2021-01-26 11:04:16 +08:00
|
|
|
|
"scheme": "grpc",
|
2020-09-26 09:21:01 +08:00
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:50051": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|