Skip to main content
Version: 3.2

grpc-web

描述#

grpc-web 插件是一个代理插件,可以处理从 JavaScript 客户端到 gRPC Service 的 gRPC Web 请求。

启用插件#

你可以通过如下命令在指定路由上启用 gRPC-web 插件:

curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri":"/grpc/web/*",
"plugins":{
"grpc-web":{}
},
"upstream":{
"scheme":"grpc",
"type":"roundrobin",
"nodes":{
"127.0.0.1:1980":1
}
}
}'
IMPORTANT

在使用 gRPC Web 代理插件时,路由必须使用前缀匹配模式(例如:/*/grpc/example/*),因为 gRPC Web 客户端会在 URI 中传递 proto 中声明的包名称服务接口名称方法名称等信息(例如:/path/a6.RouteService/Insert)。

因此,在使用绝对匹配时将无法命中插件和提取 proto 信息。

测试插件#

请参考 gRPC-Web Client Runtime LibraryApache APISIX gRPC Web Test Framework 了解如何配置你的 Web 客户端。

运行 gRPC Web 客户端后,你可以从浏览器或通过 Node.js 向 APISIX 发出请求。

note

请求方式仅支持 POSTOPTIONS,详细信息请参考:CORS support

内容类型支持 application/grpc-webapplication/grpc-web-textapplication/grpc-web+protoapplication/grpc-web-text+proto,详细信息请参考:Protocol differences vs gRPC over HTTP2

禁用插件#

当你需要禁用 grpc-web 插件时,可以通过如下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri":"/grpc/web/*",
"plugins":{},
"upstream":{
"scheme":"grpc",
"type":"roundrobin",
"nodes":{
"127.0.0.1:1980":1
}
}
}'