2021-02-26 21:40:08 +08:00
|
|
|
|
---
|
2021-03-01 12:00:06 +08:00
|
|
|
|
title: gRPC 代理
|
2021-02-26 21:40:08 +08:00
|
|
|
|
---
|
|
|
|
|
|
2019-11-01 11:22:49 +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.
|
|
|
|
|
#
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
通过 APISIX 代理 gRPC 连接,并使用 APISIX 的大部分特性管理你的 gRPC 服务。
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2020-04-16 19:21:46 +08:00
|
|
|
|
## 参数
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2021-01-26 11:04:16 +08:00
|
|
|
|
* `scheme`: Route 对应的 Upstream 的 `scheme` 必须设置为 `grpc` 或者 `grpcs`
|
|
|
|
|
* `uri`: 格式为 /service/method 如:/helloworld.Greeter/SayHello
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2020-04-16 19:21:46 +08:00
|
|
|
|
## 示例
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2020-04-16 19:21:46 +08:00
|
|
|
|
### 创建代理 gRPC 的 Route
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2019-11-01 11:22:49 +08:00
|
|
|
|
在指定 Route 中,代理 gRPC 服务接口:
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2021-01-26 11:04:16 +08:00
|
|
|
|
* 注意:这个 Route 对应的 Upstream 的 `scheme` 必须设置为 `grpc` 或者 `grpcs`。
|
2020-06-19 12:03:55 +08:00
|
|
|
|
* 注意: APISIX 使用 TLS 加密的 HTTP/2 暴露 gRPC 服务, 所以需要先 [配置 SSL 证书](https.md);
|
2021-02-19 09:04:48 +08:00
|
|
|
|
* 注意: APISIX 也支持通过纯文本的 HTTP/2 暴露 gRPC 服务,这不需要依赖 SSL,通常用于内网环境代理gRPC服务
|
2021-01-26 11:04:16 +08:00
|
|
|
|
* 下面例子所代理的 gRPC 服务可供参考:[grpc_server_example](https://github.com/api7/grpc_server_example)。
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-11-01 10:09:01 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["POST", "GET"],
|
|
|
|
|
"uri": "/helloworld.Greeter/SayHello",
|
|
|
|
|
"upstream": {
|
2021-01-26 11:04:16 +08:00
|
|
|
|
"scheme": "grpc",
|
2019-11-01 10:09:01 +08:00
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:50051": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2021-02-19 09:04:48 +08:00
|
|
|
|
### 测试 TLS 加密的 HTTP/2
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
2019-11-01 11:22:49 +08:00
|
|
|
|
访问上面配置的 Route:
|
2019-11-01 10:09:01 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2019-11-01 11:22:49 +08:00
|
|
|
|
grpcurl -insecure -import-path /pathtoprotos -proto helloworld.proto \
|
|
|
|
|
-d '{"name":"apisix"}' 127.0.0.1:9443 helloworld.Greeter.SayHello
|
2019-11-01 10:09:01 +08:00
|
|
|
|
{
|
|
|
|
|
"message": "Hello apisix"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这表示已成功代理。
|
2021-01-26 11:04:16 +08:00
|
|
|
|
|
2021-02-19 09:04:48 +08:00
|
|
|
|
### 测试纯文本的 HTTP/2
|
|
|
|
|
|
|
|
|
|
默认情况下,APISIX只在 `9443` 端口支持 TLS 加密的 HTTP/2。你也可以支持纯本文的 HTTP/2,只需要修改 `conf/config.yaml` 文件中的 `node_listen` 配置即可。
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
apisix:
|
|
|
|
|
node_listen:
|
|
|
|
|
- port: 9080
|
|
|
|
|
enable_http2: false
|
|
|
|
|
- port: 9081
|
|
|
|
|
enable_http2: true
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
访问上面配置的 Route:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
grpcurl -plaintext -import-path /pathtoprotos -proto helloworld.proto \
|
|
|
|
|
-d '{"name":"apisix"}' 127.0.0.1:9081 helloworld.Greeter.SayHello
|
|
|
|
|
{
|
|
|
|
|
"message": "Hello apisix"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这表示已成功代理。
|
|
|
|
|
|
2021-01-26 11:04:16 +08:00
|
|
|
|
### gRPCS
|
|
|
|
|
|
|
|
|
|
如果你的 gRPC 服务使用了自己的 TLS 加密,即所谓的 `gPRCS` (gRPC + TLS),那么需要修改 scheme 为 `grpcs`。继续上面的例子,50052 端口上跑的是 gPRCS 的服务,这时候应该这么配置:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|
|
|
|
{
|
|
|
|
|
"methods": ["POST", "GET"],
|
|
|
|
|
"uri": "/helloworld.Greeter/SayHello",
|
|
|
|
|
"upstream": {
|
|
|
|
|
"scheme": "grpcs",
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:50052": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|