Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc proxy support #410

Merged
merged 50 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
270d563
feature: grpc proxy --test
johzchen Aug 12, 2019
2184dc6
feat compat grpc pass
johzchen Aug 13, 2019
ac69ad2
feat update nginx conf template
johzchen Aug 14, 2019
0901ba1
feat grpc proto admin && fetch proto from etcd
johzchen Aug 15, 2019
19a880c
feat fetch proto
johzchen Aug 15, 2019
87b08ed
fix remove debug log
johzchen Aug 16, 2019
ab6141d
feat fetch proto from etcd
johzchen Aug 16, 2019
c6886f7
fix warning error
johzchen Aug 16, 2019
d4932e4
feat doc
johzchen Aug 16, 2019
6d9f9f2
fix typo
johzchen Aug 16, 2019
360dc43
fix Hash Sum mismatch
johzchen Aug 18, 2019
e8f3d44
fix local ngx
johzchen Aug 18, 2019
496075f
feat 性能优化
johzchen Aug 19, 2019
26fbf17
bugfix: avoided invalid json schema.
membphis Aug 19, 2019
fae43fc
feat 不使用lrucache global
johzchen Aug 19, 2019
a57c128
bugfix: plugin initialization is done in `init`.
membphis Aug 19, 2019
5e69c16
test: updated test cases after added new plugin `grpc-proxy`.
membphis Aug 19, 2019
8061c1e
fix error
johzchen Aug 19, 2019
6e14321
feat 不使用lrucache global
johzchen Aug 19, 2019
73cadb1
remove log
johzchen Aug 19, 2019
9fc8e41
test: use 128 worker connections as default value.
membphis Aug 19, 2019
fdc0f1a
doc
membphis Aug 19, 2019
8a0f380
travis add grpc server running with golang
johzchen Aug 20, 2019
1eecd1e
optimize: local cache global variable.
membphis Aug 19, 2019
666480c
change: code style.
membphis Aug 19, 2019
dc1a929
change: code style.
membphis Aug 19, 2019
7cc3ef9
change: code style.
membphis Aug 20, 2019
6bcc2a8
change: code style.
membphis Aug 20, 2019
332a289
change(CLI): updated `nginx.conf` from CLI.
membphis Aug 20, 2019
3fb5801
fix golang version
johzchen Aug 20, 2019
c1fecdf
fix run as root
johzchen Aug 20, 2019
f9a8b7d
fix force yes
johzchen Aug 20, 2019
f4133d0
test mac os
johzchen Aug 20, 2019
0fd15f3
test mac os
johzchen Aug 20, 2019
170d87c
test: added test cases.
membphis Aug 20, 2019
704e3c1
bugfix: added `Trailer` for grpc-proxy case
membphis Aug 21, 2019
d2f9d8e
fix clean go cache
johzchen Aug 21, 2019
ee8f120
feat 查看go环境信息
johzchen Aug 21, 2019
a6d2b75
bugfix: supported to parse http response body which contains http tra…
membphis Aug 21, 2019
22cf2ac
copyright
membphis Aug 21, 2019
a6a893e
fix go run error
johzchen Aug 21, 2019
e20ebc7
fix build && run
johzchen Aug 21, 2019
adc056d
feat route's service protocol limit
johzchen Aug 21, 2019
5996b8e
add lua-protobug copyright
johzchen Aug 21, 2019
b31ca72
fix test index
johzchen Aug 21, 2019
1bc9ae4
fix capture proto loading error
johzchen Aug 21, 2019
cc60515
fix code style
johzchen Aug 21, 2019
07a24b9
travis condition
johzchen Aug 21, 2019
cb4567a
check proto is using or not when deleting it
johzchen Aug 21, 2019
e481d9d
optimize: local cache global variable.
membphis Aug 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .travis/linux_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,39 @@ before_install() {

do_install() {
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
sudo apt-get -y update --fix-missing
sudo apt-get -y install software-properties-common
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install openresty-debug

sudo add-apt-repository -y ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang

export GO111MOUDULE=on

export_or_prefix

sudo luarocks make --lua-dir=${OPENRESTY_PREFIX}luajit rockspec/apisix-dev-1.0-0.rockspec --tree=deps --only-deps --local
sudo luarocks install --lua-dir=${OPENRESTY_PREFIX}luajit lua-resty-libr3 --tree=deps --local

git clone https://github.com/openresty/test-nginx.git test-nginx
git clone https://github.com/membphis/test-nginx.git test-nginx

git clone https://github.com/nic-chen/grpc_server_example.git grpc_server_example

cd grpc_server_example/
go build -o grpc_server_example main.go
cd ..
}

script() {
export_or_prefix
export PATH=$OPENRESTY_PREFIX/nginx/sbin:$OPENRESTY_PREFIX/luajit/bin:$OPENRESTY_PREFIX/bin:$PATH
sudo service etcd start

./grpc_server_example/grpc_server_example &

./bin/apisix help
./bin/apisix init
./bin/apisix init_etcd
Expand Down
12 changes: 11 additions & 1 deletion .travis/osx_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export_or_prefix() {

before_install() {
HOMEBREW_NO_AUTO_UPDATE=1 brew install perl cpanminus etcd luarocks openresty/brew/openresty-debug
brew upgrade go
export GO111MOUDULE=on
sudo cpanm --notest Test::Nginx >build.log 2>&1 || (cat build.log && exit 1)
export_or_prefix
luarocks install --lua-dir=${OPENRESTY_PREFIX}/luajit luacov-coveralls --local --tree=deps
Expand All @@ -29,7 +31,12 @@ do_install() {
make dev
make dev_r3

git clone https://github.com/openresty/test-nginx.git test-nginx
git clone https://github.com/membphis/test-nginx.git test-nginx
git clone https://github.com/nic-chen/grpc_server_example.git grpc_server_example

cd grpc_server_example/
go build -o grpc_server_example main.go
cd ..
}

script() {
Expand All @@ -38,6 +45,9 @@ script() {

luarocks install luacheck
brew services start etcd

./grpc_server_example/grpc_server_example &

make help
make init
sudo make run
Expand Down
55 changes: 55 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,58 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

%%%%%%%%%

lua-resty-grpc-gateway
https://github.com/ysugimoto/lua-resty-grpc-gateway

MIT License

Copyright (c) 2019 Yoshiaki Sugimoto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

%%%%%%%%%

lua-protobuf
https://github.com/starwing/lua-protobuf

MIT License

Copyright (c) 2019 Yoshiaki Sugimoto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ check:
lua/apisix/admin/*.lua \
lua/apisix/core/*.lua \
lua/apisix/http/*.lua \
lua/apisix/plugins/*.lua > \
lua/apisix/plugins/*.lua \
lua/apisix/plugins/grpc-proxy/*.lua > \
/tmp/check.log 2>&1 || (cat /tmp/check.log && exit 1)


Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ For more detailed information, see the [White Paper](https://www.iresty.com/down
- **[Limit-concurrency](doc/plugins/limit-conn.md)**
- **OpenTracing: [Zipkin](doc/plugins/zipkin.md)**
- **Monitoring and Metrics**: [Prometheus](doc/plugins/prometheus.md)
- **[gRPC-Proxy](doc/plugins/grpc-proxy.md)**:REST <-> gRPC proxying.
- **Custom plugins**: Allows hooking of common phases, such as `rewrite`, `access`, `header filer`, `body filter` and `log`, also allows to hook the `balancer` stage.
- **Dashboard**: Built-in dashboard to control APISIX.
- **CLI**: start\stop\reload APISIX through the command line.
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ APISIX 通过插件机制,提供动态负载平衡、身份验证、限流限
- **[限制并发](doc/plugins/limit-conn-cn.md)**
- **OpenTracing: [Zipkin](doc/plugins/zipkin.md)**
- **监控和指标**: [Prometheus](doc/plugins/prometheus-cn.md)
- **[gRPC-Proxy](doc/plugins/grpc-proxy-cn.md)**:REST <-> gRPC proxying.
- **自定义插件**: 允许挂载常见阶段,例如`rewrite`,`access`,`header filer`,`body filter`和`log`,还允许挂载 `balancer` 阶段。
- **控制台**: 内置控制台来操作 APISIX 集群。
- **CLI**: 使用命令行来启动、关闭和重启 APISIX。
Expand Down
29 changes: 28 additions & 1 deletion bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ http {
apisix.http_balancer_phase()
}

keepalive 32;
keepalive 320;
}

init_by_lua_block {
Expand Down Expand Up @@ -231,6 +231,33 @@ http {
apisix.http_header_filter_phase()
}

body_filter_by_lua_block {
apisix.http_body_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}

location @grpc_pass {

access_by_lua_block {
apisix.grpc_access_phase()
}

grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
grpc_pass grpc://apisix_backend;

header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

body_filter_by_lua_block {
apisix.http_body_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
Expand Down
1 change: 1 addition & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ plugins: # plugin list
- jwt-auth
- zipkin
- ip-restriction
- grpc-proxy
30 changes: 29 additions & 1 deletion conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ http {
apisix.http_balancer_phase()
}

keepalive 32;
keepalive 320;
}

init_by_lua_block {
Expand Down Expand Up @@ -141,9 +141,37 @@ http {
apisix.http_header_filter_phase()
}

body_filter_by_lua_block {
apisix.http_body_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}

location @grpc_pass {

access_by_lua_block {
apisix.grpc_access_phase()
}

grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
grpc_pass grpc://apisix_backend;

header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

body_filter_by_lua_block {
apisix.http_body_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}

}
}
90 changes: 90 additions & 0 deletions doc/plugins/grpc-proxy-cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
[English](grpc-proxy.md)
# grpc-proxy

HTTP(s) -> APISIX -> gRPC server

### Proto

#### 参数
* `content`: `.proto` 文件的内容

#### 添加proto

路径中最后的数字,会被用作 proto 的 id 做唯一标识,比如下面示例的 proto `id` 是 `1` :

```shell
curl http://127.0.0.1:9080/apisix/admin/proto/1 -X PUT -d '
{
"content" : "syntax = \"proto3\";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}"
}'
```

### 参数

* `proto_id`: `.proto`内容的id.
* `service`: grpc服务名.
* `method`: grpc服务中要调用的方法名.



### 示例

#### 使用 grpc-proxy 插件

在指定 route 中,代理 grpc 服务接口:

* 注意: 这个 route 的属性`service_protocal` 必须设置为 `grpc`
* 例子所代理的 grpc 服务可参考:[grpc_server_example](https://github.com/nic-chen/grpc_server_example)

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/111 -X PUT -d '
{
"methods": ["GET"],
"uri": "/grpctest",
"service_protocol": "grpc",
"plugins": {
"grpc-proxy": {
"proto_id": "1",
"service": "helloworld.Greeter",
"method": "SayHello"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:50051": 1
}
}
}'
```


#### 测试

访问上面配置的 route:

```shell
$ curl -i http://127.0.0.1:9080/grpctest
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"}
```

这表示已成功代理。

Loading