Connect Rpc快速上手
入门 Connect RPC 是一个轻量级的 HTTP API 构建库,支持浏览器和 gRPC 兼容的 API。它通过 Protocol Buffer 定义服务,并生成类型安全的服务器和客户端代码。 压缩和序列化 Connect 支持多种压缩和序列化选项,默认情况下,Connect 处理程序支持在默认压缩级别使用标准库的 compress/gzip 进行 gzip 压缩。Connect 客户端默认发送未压缩的请求并请求 gzip 压缩的响应。如果您知道服务器支持 gzip,则还可以在客户端构建期间使用 WithSendGzip 选项来压缩请求。 // 服务端不需要进行什么选项设置 参考https://github.com/connectrpc/connect-go/issues/773 handler := greetv1connect.NewGreetServiceHandler( &GreetServer{}, ) // 客户端配置压缩,默认 client := greetv1connect.NewGreetServiceClient( http.DefaultClient, "http://localhost:8080", connect.WithSendGzip(), ) 自定义压缩实现: type CustomCompressor struct{} func (c *CustomCompressor) Name() string { return "custom" } func (c *CustomCompressor) Compress(w io.Writer) (io.WriteCloser, error) func (c *CustomCompressor) Decompress(r io.Reader) (io.Reader, error) Get 请求 Connect 支持通过 HTTP GET 进行无副作用的请求,这使得可以在浏览器、CDN 或代理中缓存某些类型的请求。...