grpc-web
摘要#
定义#
grpc-web 插件是一个代理插件,用于转换 gRPC Web 客户端到 gRPC Server 的请求。
gRPC Web Client -> APISIX -> gRPC server
如何开启#
启用 gRPC Web 代理插件,路由必须使用 前缀匹配 模式(例如:/* 或 /grpc/example/*),
因为 gRPC Web 客户端会在 URI 中传递 proto 中声明的包名称、服务接口名称、方法名称等信息(例如:/path/a6.RouteService/Insert),
使用 绝对匹配 时将无法命中插件和提取 proto 信息。
curl http://127.0.0.1:9080/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
}
}
}'
测试插件#
- 请求方式仅支持
POST和OPTIONS,参考:CORS support 。 - 内容类型支持
application/grpc-web、application/grpc-web-text、application/grpc-web+proto、application/grpc-web-text+proto,参考:Protocol differences vs gRPC over HTTP2 。 - 客户端部署,参考:gRPC-Web Client Runtime Library 或 Apache APISIX gRPC Web 测试框架 。
- 完成
gRPC Web客户端部署后,即可通过浏览器或node向APISIX发起gRPC Web代理请求。
禁用插件#
只需删除插件配置中 grpc-web 的JSON配置即可。 APISIX 插件是热加载的,所以不需要重启 APISIX。
curl http://127.0.0.1:9080/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
}
}
}'