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   荣锋亮  阅读(208)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-09-18 openresty+pdf.js 实现一个通用的pdf预览服务
2019-09-18 cronicle minio s3 存储配置集成
2019-09-18 cronicle 任务调度一主多从安装试用
2018-09-18 socat 广播以及多播
2018-09-18 socat 简单试用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示