Golang 语言 gRPC 怎么使用?

开发 后端
既然我们要介绍 gRPC 怎么在 Golang 语言中使用,那么我们必须搭建 Golang 开发环境。这部分内容比较简单,本文就不再赘述了,如果有读者朋友对这块内容不清楚,建议阅读 Golang 官网文档。

[[423367]]

01介绍

在之前的两篇文章中,我们已经介绍了使用 gRPC 创建 RPC 应用的前导知识。我们了解到 gRPC 支持多语言,本文我们介绍在 Golang 语言中怎么使用 gRPC。

02准备工作

既然我们要介绍 gRPC 怎么在 Golang 语言中使用,那么我们必须搭建 Golang 开发环境。这部分内容比较简单,本文就不再赘述了,如果有读者朋友对这块内容不清楚,建议阅读 Golang 官网文档。

此外,我们还需要安装接口设计语言 Protocol buffer 的编译器 protoc,我们在之前的文章「Protobuf - 更小、更快、更简单的交互式数据语言」中也已经介绍过 protoc 的安装方法,本文就不再赘述了,如果有需要了解的读者朋友,可以翻阅一下这篇文章。

最后,我们介绍一下 protoc 编译生成 pb 文件需要使用的插件 protoc-gen-go 和 protoc-gen-go-grpc。插件安装方式,具体如下:

执行 go install 命令安装插件

  1. go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26 
  2.  go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1 

修改 PATH

  1. $ export PATH="$PATH:$(go env GOPATH)/bin" 

完成以上两步之后,我们就可以使用 protoc 编译 .proto 文件,生成 pb 文件了。

03编写 .proto 文件和生成 pb 文件

在 Golang 语言中使用 gRPC,首先编写 .proto 文件,然后使用 protoc 编译 .proto 文件生成 pb 文件,最后编写剩余的 Golang 代码。

接口设计语言 protobuf,在之前的文章 「Golang 语言 gRPC 使用的接口设计语言 protobuf」 中也已经介绍过了,本文不再赘述,如果有需要了解的读者朋友,可以翻阅一下这篇文章。

示例代码:

编写 .proto 文件。

  1. syntax = "proto3"
  2.  
  3. option go_package = "advanced_go/lesson06/proto/greeter"
  4.  
  5. service Greeter { 
  6.   rpc SayHello (HelloRequest) returns (HelloReply) {} 
  7.  
  8. message HelloRequest { 
  9.   string name = 1; 
  10.  
  11. message HelloReply { 
  12.   string message = 1; 

使用 protoc 编译 .proto 文件,生成 pb 文件。

  1. $ protoc --go_out=. --go_opt=paths=source_relative \ 
  2. --go-grpc_out=. --go-grpc_opt=paths=source_relative \ 
  3. proto/helloworld.proto 

04编写服务端和客户端 Golang 代码

我们在之前的文章中介绍过 gRPC 是什么,接下来,我们通过示例代码介绍在 Golang 语言中怎么使用 gRPC,本文先来介绍使用 gRPC 的编码流程,限于篇幅,关于 gRPC 的更多使用方法,后续会新开篇文章介绍。

首先使用接口设计语言 protobuf 的编译器 protoc、protoc-gen-go 和 protoc-gen-go-grpc 插件生成 pb 文件,我们通过查看生成的 pb 文件,可以看到 protoc 为我们自动生成结构体、接口和方法等 Golang 代码。

接下来,我们只需把剩余的 Golang 代码写完就可以了,具体实现如下:

服务端示例代码:

  1. const ( 
  2.  port = ":50051" 
  3.  
  4. type server struct { 
  5.  pb.UnimplementedGreeterServer 
  6.  
  7. func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { 
  8.  log.Printf("Received: %v"in.GetName()) 
  9.  return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil 
  10.  
  11. func main () { 
  12.  lis, err := net.Listen("tcp", port) 
  13.  if err != nil { 
  14.   log.Fatalf("failed to listen: %v", err) 
  15.  } 
  16.  s := grpc.NewServer() 
  17.  pb.RegisterGreeterServer(s, &server{}) 
  18.  log.Printf("server listening at %v", lis.Addr()) 
  19.  if err := s.Serve(lis); err != nil { 
  20.   log.Fatalf("failed to serve: %v", err) 
  21.  } 

阅读上面这段代码,我们使用 Golang 语言编写了 SayHello 方法,该方法实际上就是 pb 文件中自动生成的 SayHello 方法的具体实现,对应自动生成的 pb 文件 helloworld_grpc.pb.go 中的代码如下:

  1. // UnimplementedGreeterServer must be embedded to have forward compatible implementations. 
  2. type UnimplementedGreeterServer struct { 
  3.  
  4. func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest) (*HelloReply, error) { 
  5.  return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented"

在 main 函数中,我们使用 grpc 调用 NewServer 函数创建一个服务,然后使用 pb 文件中的 RegisterGreeterServer 函数注册服务,对应自动生成的 pb 文件 helloworld_grpc.pb.go 中的代码如下:

  1. func RegisterGreeterServer(s grpc.ServiceRegistrar, srv GreeterServer) { 
  2.  s.RegisterService(&Greeter_ServiceDesc, srv) 

客户端示例代码:

  1. const( 
  2.  address = ":50051" 
  3.  defaultName = "word" 
  4.  
  5. func main () { 
  6.  conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) 
  7.  if err != nil { 
  8.   log.Fatalf("did not connect: %v", err) 
  9.  } 
  10.  defer conn.Close() 
  11.  c := pb.NewGreeterClient(conn) 
  12.  
  13.  name := defaultName 
  14.  if len(os.Args) > 1 { 
  15.   name = os.Args[1] 
  16.  } 
  17.  ctx, cancel := context.WithTimeout(context.Background(), time.Second
  18.  defer cancel() 
  19.  r, err := c.SayHello(ctx, &pb.HelloRequest{Namename}) 
  20.  if err != nil { 
  21.   log.Fatalf("could not greet: %v", err) 
  22.  } 
  23.  log.Printf("Greeting: %s", r.GetMessage()) 

阅读上面这段代码,我们使用 pb 文件中的 NewGreeterClient 方法创建一个客户端,然后就可以使用创建的客户端直接调用服务端的 SayHello 方法,对应自动生成的 pb 文件 helloworld_grpc.pb.go 中的代码如下:

  1. type GreeterClient interface { 
  2.  SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) 
  3.  
  4. type greeterClient struct { 
  5.  cc grpc.ClientConnInterface 
  6.  
  7. func NewGreeterClient(cc grpc.ClientConnInterface) GreeterClient { 
  8.  return &greeterClient{cc} 
  9.  
  10. func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) { 
  11.  out := new(HelloReply) 
  12.  err := c.cc.Invoke(ctx, "/Greeter/SayHello"inout, opts...) 
  13.  if err != nil { 
  14.   return nil, err 
  15.  } 
  16.  return out, nil 

编写完服务端和客户端代码,接下来,我们分别启动服务端和客户端,执行结果如下:

  1. go run grpc_server/main.go  
  2. 2021/09/11 23:02:59 server listening at [::]:50051 
  3. 2021/09/11 23:03:23 Received: word 
  4. 2021/09/11 23:03:31 Received: frank 
  5.  
  6. go run grpc_client/main.go  
  7. 2021/09/11 23:03:23 Greeting: Hello word 
  8.  
  9. go run grpc_client/main.go frank 
  10. 2021/09/11 23:03:31 Greeting: Hello frank 

05总结

本文我们介绍在 Golang 语言中怎么使用 gRPC,为了方便读者朋友们理解,文章通过一个简单示例从零到一的实现,介绍了在 Golang 语言中使用 gRPC 的编码流程。

建议读者朋友们阅读完本文,动手敲一遍示例代码,来进一步加深理解。限于篇幅,关于 gRPC 的更多使用方法,我们后续撰文介绍。

编码流程归纳如下:

 

  1. 搭建 Golang 开发环境。
  2. 安装 protobuf 编译器 protoc 和插件 protoc-gen-go、protoc-gen-go-grpc,设置环境变量。
  3. 初始化项目 go mod init。
  4. 编写 protobuf,生成 pb 文件,执行 go mod tidy 整理依赖包。
  5. 编写剩余 Golang 代码。

 

责任编辑:武晓燕 来源: Golang语言开发栈
相关推荐

2021-09-26 10:20:06

开发Golang代码

2022-02-20 23:15:46

gRPCGolang语言

2021-09-01 23:29:37

Golang语言gRPC

2021-06-07 23:19:44

Golang语言 Defer

2021-07-12 05:05:59

Golang语言字段

2021-01-29 08:56:13

Golang标准库函数

2021-06-09 23:36:46

Golang语言版本

2021-10-10 23:02:49

Golang语言代码

2021-06-29 23:40:19

Golang语言并发

2021-12-13 01:24:14

语言Golang panic

2021-11-08 23:09:07

Go排序数据

2021-12-05 23:14:24

微服务GolanggRPC

2021-07-26 11:19:43

微服务开发技术

2021-04-28 09:02:48

Golang语言Context

2021-10-31 23:01:50

语言拼接字符串

2021-11-28 23:06:30

语言编程接口

2022-04-29 11:52:02

API代码HTTP

2022-01-04 23:13:57

语言PanicGolang

2023-03-05 23:11:07

Go语言服务

2023-07-17 18:42:47

gRPCDemo项目
点赞
收藏

51CTO技术栈公众号