我尝试了解gRPC入门
请参考这份文件。
-
- https://developers.google.com/protocol-buffers/docs/overview
- https://grpc.io/docs/languages/go/quickstart/
需要的工具 de
-
- golang
1.19.2
cli
protobuf
goenv
go module
google.golang.org/grpc
goのgRPCの実装
google.golang.org/grpc/cmd/protoc-gen-go-grpc
goのgrpcコードの生成ツール
准备环境
$mkdir $WORKSPACE
$cd $WORKSPACE
$brew install protobuf
$goenv local 1.19.2
$go mod init grpcexample
$go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
$go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
$export PATH="$PATH:$(go env GOPATH)/bin" #NOTE: PATHが通ってなければ実施する
gRPC服务(API)的定义
gRPC服务(API)使用Protocol Buffer进行定义。
$mkdir -p proto/helloworld
$touch ./proto/helloworld/helloworld.proto
// ./proto/helloworld/helloworld.proto
syntax = "proto3";
option go_package = "pkg/helloworld";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
生成 gRPC 代码
根据已创建的Prot文件生成代码。
$ mkdir -p pkg/helloworld
$ cd proto
$ protoc \
--go_out=../pkg \
--go_opt=paths=source_relative \
--go-grpc_out=../pkg \
--go-grpc_opt=paths=source_relative \
helloworld.proto
服务器端代码的实现
$mkdir server
$touch server/main.go
// ./server/main.go
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
pb "grpcexample/pkg/helloworld"
"google.golang.org/grpc"
)
var (
port = flag.Int("port", 50051, "The server port")
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
客户端代码的实现
$mkdir client
$touch client/main.go
package main
// ./client/main.go
import (
"context"
"flag"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
defaultName = "world"
)
var (
addr = flag.String("addr", "localhost:50051", "the address to connect to")
name = flag.String("name", defaultName, "Name to greet")
)
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
解决 go 依赖模块问题
$go mod tidy
启动服务器
$go run server/main.go
2023/01/19 17:22:34 server listening at [::]:50051
运行客户机
在其他进程中执行。
$go run client/main.go
2023/01/19 17:23:35 Greeting: Hello world
如果要使用gRPC的客户端工具
服务器反射的设置
如果使用POSTMAN或grpcurl等客户端工具,则可以通过从gRPC服务器获取服务信息来执行。要获取服务信息,需要对服务器进行配置。在服务器代码中添加以下内容。
import (
.
"google.golang.org/grpc/reflection"
)
func main() {
.
.
s := grpc.NewServer()
reflection.Register(s)
.
.
}
以 grpcurl 执行
# サービスリストの取得
$grpcurl -plaintext localhost:50051 list
grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter
# サービスのメソッドリスト取得
$grpcurl -plaintext localhost:50051 list helloworld.Greeter
helloworld.Greeter.SayHello
# メソッド実行
$grpcurl -plaintext localhost:50051 helloworld.Greeter.SayHello
{
"message": "Hello "
}