Gaoyang's Blog
Gaoyang's Blog
/
🎃
关于 gRPC-Web 的应用
Search
关于 gRPC-Web 的应用
🎃

关于 gRPC-Web 的应用

Tags
gRPC
Web Dev
C#
.NET
Published
June 25, 2023
Author
gaoyang

示例项目

GitHub - gaoyang/example-grpc-web: An example of communication using the gRPC-web solution.
An example of communication using the gRPC-web solution. - GitHub - gaoyang/example-grpc-web: An example of communication using the gRPC-web solution.
GitHub - gaoyang/example-grpc-web: An example of communication using the gRPC-web solution.
https://github.com/gaoyang/example-grpc-web
GitHub - gaoyang/example-grpc-web: An example of communication using the gRPC-web solution.
 

为什么会用到 gRPC-Web

前后端通信大多数时候用 REST API 就够了,但如果后端本身已经有一套完整的 gRPC + Protobuf 服务(尤其是桌面端嵌入式 Web 视图、或者微服务内部已经统一了 Proto 契约的场景),前端再额外维护一层 REST 适配就显得多余。gRPC-Web 就是用来解决这个问题的。

什么是 gRPC-Web

gRPC-Web 是面向浏览器的 gRPC 客户端协议。由于浏览器原生 API 无法直接控制 HTTP/2 帧和 Trailers,gRPC-Web 通过 HTTP/1.1 或 HTTP/2 承载编码后的 Protobuf 消息,再由代理(如 Envoy)或服务端中间件(如 ASP.NET Core 的 Grpc.AspNetCore.Web)做协议转换。
注意:gRPC-Web 支持一元调用(Unary)和服务端流(Server Streaming),不支持客户端流和双向流。

定义 Proto 文件

关于 Proto3 语法和定义方式可以直接参考官方文档:
Protocol Buffers
Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.
Protocol Buffers
https://protobuf.dev/

前端基于 Vite 的集成步骤

安装运行时依赖(客户端代码通常由 protoc + protoc-gen-grpc-web 或 protoc-gen-ts 预生成):
npm install grpc-web google-protobuf
生成 TypeScript / JS 客户端代码:
protoc -I=. ./proto/greeter.proto \ --js_out=import_style=commonjs,binary:./src/proto \ --grpc-web_out=import_style=typescript,mode=grpcwebtext:./src/proto
然后在 Vite 项目中直接创建客户端调用服务端接口(如遇到 CommonJS 模块互操作问题,可在 vite.config.ts 的 optimizeDeps 中显式包含生成文件或改用 @protobuf-ts/grpcweb-transport 这类纯 ESM 方案):
import { GreeterClient } from "./proto/GreeterServiceClientPb"; import { HelloRequest } from "./proto/greeter_pb"; const client = new GreeterClient("http://localhost:8080"); const request = new HelloRequest(); request.setName("World"); client.sayHello(request, {}, (err, response) => { if (err) { console.error(err); return; } console.log(response.getMessage()); });

服务端基于 .NET 的示例

在 ASP.NET Core 中不需要额外搭 Envoy 代理,直接引入中间件即可支持 gRPC-Web。安装 NuGet 包:
  • Grpc.AspNetCore
  • Grpc.AspNetCore.Web
在 Program.cs(或早期版本的 Startup.cs)中注册并启用 gRPC-Web(跨域场景记得配合 CORS 暴露 Grpc-Status、Grpc-Message 等响应头):
var builder = WebApplication.CreateBuilder(args); builder.Services.AddGrpc(); var app = builder.Build(); app.UseRouting(); app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true }); app.MapGrpcService<MyService>().EnableGrpcWeb(); app.Run();
服务实现本身和标准 gRPC 完全一致,无需针对 Web 端做特殊修改:
public class MyService : Greeter.GreeterBase { public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); } }

小结

如果团队后端已经重度使用 gRPC + Protobuf,用 gRPC-Web 可以直接把前后端的接口定义收口到同一份 .proto 文件里,省掉手写 DTO 和接口文档同步的成本。不过在浏览器侧如果强依赖双向流,或者更看重纯 ESM 生态,也可以评估 Connect-Web 或结合 WebSocket 使用。
Table of Contents
示例项目为什么会用到 gRPC-Web什么是 gRPC-Web定义 Proto 文件前端基于 Vite 的集成步骤服务端基于 .NET 的示例小结
Copyright 2026 gaoyang