Skip to main content

Connect

Connect 协议

POST /<Package>.<Service>/<Method>

connect-web

npm install @bufbuild/protoc-gen-es @bufbuild/protoc-gen-connect-web

PATH=$PATH:$(pwd)/node_modules/.bin

connect-go

# 基于 POST 的 connect 协议
curl \
--header "Content-Type: application/json" \
--data '{"sentence": "I feel happy."}' \
https://demo.connect.build/buf.connect.demo.eliza.v1.ElizaService/Say

# 兼容 gRPC 协议
grpcurl \
-d '{"sentence": "I feel happy."}' \
demo.connect.build:443 \
buf.connect.demo.eliza.v1.ElizaService/Say
go install github.com/bufbuild/buf/cmd/buf@latest
go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install github.com/bufbuild/connect-go/cmd/protoc-gen-connect-go@latest
go install github.com/bufbuild/protobuf-es/cmd/protoc-gen-es@latest
buf.gen.yaml:
version: v1
plugins:
- name: go
out: gen
opt: paths=source_relative
- name: connect-go
out: gen
opt: paths=source_relative

实现

  • Handler - 当个 RPC 方法处理
    • 实现 Connect, gRPC, gRPC-Web 协议
    • 提供 ServeHTTP
    • 构造
      • NewBidiStreamHandler
      • NewClientStreamHandler
      • NewServerStreamHandler
      • NewUnaryHandler

Why Connect

  • gRPC 因为考虑 Google 内部使用和开源社区使用,一些设计上很乱很奇怪
  • gRPC 小版本也可能不出现兼容
  • gRPC 在 Web 环境非常麻烦
  • 要求 HTTP/2 Trailer - 对使用环境、实现要求苛刻
  • vs golang grpc
    • 不兼容 net/http
    • 实现复杂