buf connect-go 试用

golang 包含一个grpc-web 可以让浏览器运行grpc,但是是需要一些中间件工具的,connect-go 简化了处理,可以基于post 通过
http 协议就可以试用grpc 了

包含的特性

  • 基于http 简单
  • 多协议支持,包含了grpc,grpc-web

参考试用

  • 初始化

    注意还需要配置环境变量

 
mkdir connect-apps
go mod init github.com/rongfengliang/go-connect-app
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
  • 项目接口
├── README.md
├── buf.gen.yaml // gen 定义包含了go 以及conenct go 插件使用
├── buf.work.yaml // workspace 定义
├── buf.yaml
├── go.mod
├── go.sum
├── main.go // 入口
└── proto
    └── userlogin
        └── v1
            └── userloign.proto // 服务定义
  • 代码说明
    userloign.proto
 
syntax = "proto3";
 
package userlogin.v1;
 
option go_package = "github.com/rongfengliang/go-connect-app/pkg/userlogin/v1;userloginv1";
 
message LoginRequest{
    string username =1;
    string userpassword =2;
}
 
message LoginResponse{
    string token =1;
    int32 exprise =2;
}
 
service UserLoginService {
 
    rpc Login(LoginRequest) returns (LoginResponse){};
}

buf.gen.yaml

version: v1
directories:
  - proto

buf.gen.yaml

version: v1
plugins:
  - name: go
    out: pkg
    opt: paths=source_relative
  - name: connect-go
    out: pkg
    opt: paths=source_relative

main.go

package main
 
import (
    "context"
    "log"
    "net/http"
 
    "github.com/bufbuild/connect-go"
    userloginv1 "github.com/rongfengliang/go-connect-app/pkg/userlogin/v1"
    userloginv1connect "github.com/rongfengliang/go-connect-app/pkg/userlogin/v1/userloginv1connect"
    "golang.org/x/net/http2"
    "golang.org/x/net/http2/h2c"
)
 
type UserLogin struct{}
 
func (s *UserLogin) Login(
    ctx context.Context,
    req *connect.Request[userloginv1.LoginRequest],
) (*connect.Response[userloginv1.LoginResponse], error) {
    log.Println("Request headers: ", req.Header())
    log.Println("Request username: ", req.Msg.Username)
    res := connect.NewResponse(&userloginv1.LoginResponse{
        Token:   "demoapp",
        Exprise: 100,
    })
    res.Header().Set("userlogin-Version", "v1")
    return res, nil
}
 
func main() {
    userlogin := &UserLogin{}
    mux := http.NewServeMux()
    path, handler := userloginv1connect.NewUserLoginServiceHandler(userlogin)
    mux.Handle(path, handler)
    http.ListenAndServe(
        "0.0.0.0:8080",
        h2c.NewHandler(mux, &http2.Server{}),
    )
}
  • 运行
buf generate 
go run main.go
  • 测试
curl \
    --header "Content-Type: application/json" \
    --data '{"username": "dalong","userpassword":"demoapp"}' \
    http://localhost:8080/userlogin.v1.UserLoginService/Login

效果

 

 

说明

以上支持简单的试用,实际上buf connect go 的功能还是很多的,比较压缩,拦截器,路由。。。。

参考资料

https://connect.build/docs/web/getting-started/
https://github.com/bufbuild/connect-go
https://github.com/bufbuild/connect-web/tree/main/packages/connect-web
https://buf.build/blog/connect-a-better-grpc
https://github.com/rongfengliang/buf-connect-go-learning

posted on 2022-09-18 17:50  荣锋亮  阅读(193)  评论(0编辑  收藏  举报

导航