Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang如何使用Protobuf定义RPC接口_创想鸟

Golang如何使用Protobuf定义RPC接口

在Go中使用Protobuf定义RPC需先编写.proto文件,用service声明服务及方法;2. 通过protoc生成Go代码,包括消息结构体和服务接口;3. 实现服务端结构体并注册gRPC服务;4. 客户端通过Stub调用远程方法,完成通信。该流程支持跨语言、高效率的微服务交互。

golang如何使用protobuf定义rpc接口

在Go语言中使用Protobuf定义RPC接口,核心是通过.proto文件描述服务方法,然后借助代码生成工具生成Go代码。整个流程清晰、高效,广泛应用于gRPC等系统中。

1. 定义.proto文件中的service

你需要编写一个.proto文件,在其中定义RPC服务和方法。使用service关键字声明服务,每个方法指定输入和输出类型,这些类型必须是已定义的message。

示例:

rpc_service.proto

syntax = "proto3";package example;// 定义请求和响应消息message HelloRequest {    string name = 1;}message HelloResponse {    string message = 1;}// 定义RPC服务service Greeter {    rpc SayHello (HelloRequest) returns (HelloResponse);}

这个文件定义了一个名为Greeter的服务,包含一个SayHello方法,接收HelloRequest,返回HelloResponse。

立即学习“go语言免费学习笔记(深入)”;

2. 生成Go代码

需要安装protoc编译器以及Go插件来生成代码。

安装工具:

下载protoc:从 GitHub releases 获取对应平台版本 安装Go插件:

go install google.golang.org/protobuf/cmd/protoc-gen-go@latestgo install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

生成代码命令:

protoc --go_out=. --go-grpc_out=. rpc_service.proto

执行后会生成两个文件:

rpc_service.pb.go:包含消息类型的Go结构体和序列化方法 rpc_service_grpc.pb.go:包含客户端和服务端接口定义

3. 实现服务端逻辑

在Go中实现服务接口,只需定义一个结构体并实现.proto中声明的方法。

服务实现示例:

package mainimport (    "context"    "log"    "net"    "google.golang.org/grpc"    pb "your-module-path/example" // 替换为你的模块路径)type server struct {    pb.UnimplementedGreeterServer}func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {    return &pb.HelloResponse{        Message: "Hello, " + req.Name,    }, nil}func main() {    lis, err := net.Listen("tcp", ":50051")    if err != nil {        log.Fatalf("failed to listen: %v", err)    }    s := grpc.NewServer()    pb.RegisterGreeterServer(s, &server{})    log.Println("gRPC server running on :50051")    s.Serve(lis)}

4. 编写客户端调用

客户端通过gRPC连接服务端,调用生成的Stub方法。

客户端示例:

package mainimport (    "context"    "log"    "google.golang.org/grpc"    "google.golang.org/grpc/credentials/insecure"    pb "your-module-path/example")func main() {    conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))    if err != nil {        log.Fatalf("did not connect: %v", err)    }    defer conn.Close()    client := pb.NewGreeterClient(conn)    resp, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "Alice"})    if err != nil {        log.Fatalf("call failed: %v", err)    }    log.Printf("Response: %s", resp.Message)}

基本上就这些。通过.proto定义接口,工具生成代码,再分别实现服务端和客户端,就能完成一个完整的gRPC通信流程。这种方式保证了跨语言兼容性和接口一致性,适合微服务架构。

以上就是Golang如何使用Protobuf定义RPC接口的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1412859.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golang包重命名与导入别名使用方法
上一篇 2025年12月16日 06:42:31
Golang私有仓库模块管理与访问权限实践
下一篇 2025年12月16日 06:42:42

相关推荐

发表回复

登录后才能评论
关注微信