grpc-web

描述

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

启用插件

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

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri":"/grpc/web/*",
  5. "plugins":{
  6. "grpc-web":{}
  7. },
  8. "upstream":{
  9. "scheme":"grpc",
  10. "type":"roundrobin",
  11. "nodes":{
  12. "127.0.0.1:1980":1
  13. }
  14. }
  15. }'
grpc-web - 图1IMPORTANT

在使用 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 发出请求。

grpc-web - 图2note

请求方式仅支持 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 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri":"/grpc/web/*",
  5. "plugins":{},
  6. "upstream":{
  7. "scheme":"grpc",
  8. "type":"roundrobin",
  9. "nodes":{
  10. "127.0.0.1:1980":1
  11. }
  12. }
  13. }'