diff --git a/.travis/linux_runner.sh b/.travis/linux_runner.sh index 57aa4ff09cdb..33571a95a505 100755 --- a/.travis/linux_runner.sh +++ b/.travis/linux_runner.sh @@ -65,15 +65,32 @@ do_install() { ls -l ./ if [ ! -f "build-cache/grpc_server_example" ]; then sudo apt-get install golang - git clone https://github.com/iresty/grpc_server_example.git grpc_server_example - cd grpc_server_example/ go build -o grpc_server_example main.go mv grpc_server_example ../build-cache/ cd .. fi + if [ ! -f "build-cache/proto/helloworld.proto" ]; then + + if [ ! -f "grpc_server_example/main.go" ]; then + git clone https://github.com/iresty/grpc_server_example.git grpc_server_example + fi + + cd grpc_server_example/ + mv proto/ ../build-cache/ + cd .. + fi + + if [ ! -f "build-cache/grpcurl" ]; then + sudo apt-get install golang + git clone https://github.com/fullstorydev/grpcurl.git grpcurl + cd grpcurl + go build -o grpcurl ./cmd/grpcurl + mv grpcurl ../build-cache/ + cd .. + fi } script() { @@ -90,8 +107,13 @@ script() { ./bin/apisix start mkdir -p logs sleep 1 + + sudo sh ./t/grpc-proxy-test.sh + sleep 1 + ./bin/apisix stop sleep 1 + make check || exit 1 APISIX_ENABLE_LUACOV=1 prove -Itest-nginx/lib -r t } diff --git a/.travis/osx_runner.sh b/.travis/osx_runner.sh index 699c4c05de53..fe53fe3d3f6b 100755 --- a/.travis/osx_runner.sh +++ b/.travis/osx_runner.sh @@ -43,6 +43,8 @@ do_install() { cd grpc_server_example/ go build -o grpc_server_example main.go cd .. + + brew install grpcurl } script() { @@ -63,6 +65,25 @@ script() { sudo make run mkdir -p logs sleep 1 + + #test grpc proxy + curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d ' + { + "methods": ["POST", "GET"], + "uri": "/helloworld.Greeter/SayHello", + "service_protocol": "grpc", + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:50051": 1 + } + } + }' + + grpcurl -insecure -import-path ./grpc_server_example/proto -proto helloworld.proto -d '{"name":"apisix"}' 127.0.0.1:9443 helloworld.Greeter.SayHello + + sleep 1 + sudo make stop sleep 1 diff --git a/README.md b/README.md index 162c404047f4..402dbb25264d 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ For more detailed information, see the [White Paper](https://www.iresty.com/down - **[Proxy Rewrite](doc/plugins/proxy-rewrite.md)**: Support for rewriting the `host`, `uri`, `schema`, `enable_websocket`, `headers` information upstream of the request. - **OpenTracing: [support Apache Skywalking and Zipkin](doc/plugins/zipkin.md)** - **Monitoring And Metrics**: [Prometheus](doc/plugins/prometheus.md) +- **[gRPC proxy](doc/grpc-proxy.md)**:Proxying gRPC traffic. - **[gRPC transcoding](doc/plugins/grpc-transcoding.md)**:Supports protocol transcoding so that clients can access your gRPC API by using HTTP/JSON. - **[Serverless](doc/plugins/serverless.md)**: Invoke functions in each phase in APISIX. - **Custom plugins**: Allows hooking of common phases, such as `rewrite`, `access`, `header filer`, `body filter` and `log`, also allows to hook the `balancer` stage. diff --git a/README_CN.md b/README_CN.md index 0239df582a78..2d3f62842c7b 100644 --- a/README_CN.md +++ b/README_CN.md @@ -42,6 +42,7 @@ APISIX 通过插件机制,提供动态负载平衡、身份验证、限流限 - **[代理请求重写](doc/plugins/proxy-rewrite.md)**: 支持重写请求上游的`host`、`uri`、`schema`、`enable_websocket`、`headers`信息。 - **OpenTracing: [支持 Apache Skywalking 和 Zipkin](doc/plugins/zipkin.md)** - **监控和指标**: [Prometheus](doc/plugins/prometheus-cn.md) +- **[gRPC 代理](doc/grpc-proxy-cn.md)**:通过 APISIX 代理 gRPC 连接,并使用 APISIX 的大部分特性管理你的 gRPC 服务。 - **[gRPC 协议转换](doc/plugins/grpc-transcoding-cn.md)**:支持协议的转换,这样客户端可以通过 HTTP/JSON 来访问你的 gRPC API。 - **[Serverless](doc/plugins/serverless-cn.md)**: 在 APISIX 的每一个阶段,你都可以添加并调用自己编写的函数。 - **自定义插件**: 允许挂载常见阶段,例如`rewrite`,`access`,`header filer`,`body filter`和`log`,还允许挂载 `balancer` 阶段。 diff --git a/doc/admin-api-cn.md b/doc/admin-api-cn.md index fd065dc48afe..8ef8b13797b1 100644 --- a/doc/admin-api-cn.md +++ b/doc/admin-api-cn.md @@ -38,7 +38,7 @@ |upstream |可选 |Upstream|启用的 Upstream 配置,详见 [Upstream](architecture-design-cn.md#upstream)|| |upstream_id|可选 |Upstream|启用的 upstream id,详见 [Upstream](architecture-design-cn.md#upstream)|| |service_id|可选 |Service|绑定的 Service 配置,详见 [Service](architecture-design-cn.md#service)|| -|service_protocol|可选|上游协议类型|只可以是 "grpc", "http" 二选一。|默认 "http"| +|service_protocol|可选|上游协议类型|只可以是 "grpc", "http" 二选一。|默认 "http",使用gRPC proxy 或gRPC transcode 时,必须用"grpc"| 对于同一类参数比如 `host` 与 `hosts`,`remote_addr` 与 `remote_addrs`,是不能同时存在,二者只能选择其一。如果同时启用,接口会报错。 diff --git a/doc/grpc-proxy-cn.md b/doc/grpc-proxy-cn.md new file mode 100644 index 000000000000..6bbf9043359c --- /dev/null +++ b/doc/grpc-proxy-cn.md @@ -0,0 +1,53 @@ +[English](grpc-proxy.md) +# grpc-proxy + +通过APISIX代理gRPC连接,并使用APISIX的大部分特性管理你的grpc服务。 + + + +### 参数 + +* `service_protocol`: 这个 route 的属性 `service_protocal` 必须设置为 `grpc` +* `uri`: 格式为 /service/method 如:/helloworld.Greeter/SayHello + + + +### 示例 + +#### 创建代理grpc的route + +在指定 route 中,代理 gRPC 服务接口: + +* 注意: 这个 route 的属性 `service_protocal` 必须设置为 `grpc` +* 注意: APISIX 使用 TLS 加密的 HTTP/2 暴露 gRPC 服务, 所以需要先[配置 SSL 证书](https://github.com/iresty/apisix/blob/master/doc/https-cn.md) +* 例子所代理的 gRPC 服务可参考:[grpc_server_example](https://github.com/nic-chen/grpc_server_example) + +```shell +curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d ' +{ + "methods": ["POST", "GET"], + "uri": "/helloworld.Greeter/SayHello", + "service_protocol": "grpc", + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:50051": 1 + } + } +}' +``` + + +#### 测试 + +访问上面配置的 route: + +```shell +$ grpcurl -insecure -import-path /pathtoprotos -proto helloworld.proto -d '{"name":"apisix"}' 127.0.0.1:9443 helloworld.Greeter.SayHello +{ + "message": "Hello apisix" +} +``` + +这表示已成功代理。 + diff --git a/doc/grpc-proxy.md b/doc/grpc-proxy.md new file mode 100644 index 000000000000..4e6eb70bcda5 --- /dev/null +++ b/doc/grpc-proxy.md @@ -0,0 +1,52 @@ +[中文](grpc-proxy-cn.md) +# grpc-proxy + +proxying gRPC traffic: +gRPC client -> APISIX -> gRPC server + +### Parameters + +* `service_protocol`: the route's option `service_protocal` must be `grpc` +* `uri`: format likes /service/method , Example:/helloworld.Greeter/SayHello + + + +### Example + +#### create proxying gRPC route + +Here's an example, to proxying gRPC service by specified route: + +* attention: the route's option `service_protocal` must be `grpc` +* attention: APISIX use TLS‑encrypted HTTP/2 to expose gRPC service, so need to [config SSL certificate](https://github.com/iresty/apisix/blob/master/doc/https.md) +* the grpc server example:[grpc_server_example](https://github.com/nic-chen/grpc_server_example) + +```shell +curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d ' +{ + "methods": ["POST", "GET"], + "uri": "/helloworld.Greeter/SayHello", + "service_protocol": "grpc", + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:50051": 1 + } + } +}' +``` + + +#### testing + +Invoking the route created before: + +```shell +$ grpcurl -insecure -import-path /pathtoprotos -proto helloworld.proto -d '{"name":"apisix"}' 127.0.0.1:9443 helloworld.Greeter.SayHello +{ + "message": "Hello apisix" +} +``` + +This means that the proxying is working. + diff --git a/t/grpc-proxy-test.sh b/t/grpc-proxy-test.sh new file mode 100644 index 000000000000..62470aa4cb1e --- /dev/null +++ b/t/grpc-proxy-test.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +#test grpc proxy +curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d ' +{ + "methods": ["POST"], + "uri": "/helloworld.Greeter/SayHello", + "service_protocol": "grpc", + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:50051": 1 + } + } +}' + +./build-cache/grpcurl -insecure -import-path ./build-cache/proto -proto helloworld.proto -d '{"name":"apisix"}' 127.0.0.1:9443 helloworld.Greeter.SayHello