go grpc protobuf 使用
1.下载protoc https://github.com/protocolbuffers/protobuf/releases,解压出 protoc.exe 放到到GOROOT目录下
2.下载 protoc-gen-go 文件
go install github.com/golang/protobuf/protoc-gen-go@latest
会在<GOPATH>/bin
下生成protoc-gen-go.exe
文件,
3.下载依赖包
1 2 3 4 | # 安装 golang 的proto工具包 # go get -u github.com/golang/protobuf/proto # 安装 goalng 的proto编译支持 # go get -u # 安装 GRPC 包 # go get -u google.golang.org/grpc |
4.编写proto文件
1 2 3 4 5 6 7 8 9 10 11 12 | syntax = "proto3" ; package services; option go_package = "./;proto" ; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } |
5.生成 pd.go 文件
cd到proto目录下 命令:protoc -I . hello.proto --go_out=plugins=grpc:. 命令解释: protoc -I .:在当前路径下寻找hello.proto文件 --go_out=plugins=grpc:. :生成go语言的proto文件放在当前路径下
6.server 和client 通信
1 2 3 4 5 6 7 8 9 | ├─client │ client.go │ ├─proto │ hello.pb.go │ hello.proto │ └─server server.go |
7.server.go 服务端文件
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 32 33 34 35 36 37 38 39 40 | package main import ( "context" "net" "test/grpc/proto" "google.golang.org/grpc" ) type Server struct {} // 业务逻辑 func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) { res := &proto.HelloReply{ Message: "hello " + request.Name, } return res, nil } // 启动rpc的server服务 func start() { // 1.实例化server g := grpc.NewServer() // 2.注册逻辑到server中 proto.RegisterGreeterServer(g, &Server{}) // 3.启动server lis, err := net.Listen( "tcp" , "127.0.0.1:8081" ) if err != nil { panic( "监听错误:" + err.Error()) } err = g.Serve(lis) if err != nil { panic( "启动错误:" + err.Error()) } } func main() { start() } |
8.client.go 客户端文件
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 32 33 34 35 36 37 38 39 40 | package main import ( "context" "fmt" "test/grpc/proto" "google.golang.org/grpc" ) // rpc调用 func clientRpc(body map[ string ] string ) (res *proto.HelloReply, err error) { name := body[ "name" ] conn, err := grpc.Dial( "127.0.0.1:8081" , grpc.WithInsecure()) if err != nil { return nil, err } defer conn.Close() rpc := proto.NewGreeterClient(conn) response, err := rpc.SayHello(context.Background(), &proto.HelloRequest{Name: name}) if err != nil { return nil, err } return response, nil } // 业务代码 func start() { body := make(map[ string ] string ) body[ "name" ] = "jeff" response, err := clientRpc(body) if err != nil { fmt.Println( "rpc调用失败:" , err) return } fmt.Println(response.Message) } func main() { start() } |
posted on 2022-07-15 15:17 running-fly 阅读(216) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-07-15 linux nohup 命令
2020-07-15 go 语言之fmt.Sprintf格式化使用
2020-07-15 golang 中string和int类型相互转换
2020-07-15 GO 获取时间的日期函数、时间戳函数
2020-07-15 go 字符串切割方法小结