這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
GRPC是google開源的一個高效能、跨語言的RPC架構,基於HTTP2協議,基於protobuf 3.x,基於Netty 4.x。
前面寫過一篇golang標準庫的rpc包的用法,這篇文章接著講一下google的grpc。
介紹
在 gRPC 裡用戶端應用可以像調用本機物件一樣直接調用另一台不同的機器上服務端應用的方法,使得您能夠更容易地建立分布式應用和服務。
使用grpc的優點很多,支援多種語言,二進位的資料可以加快傳輸速度,基於http2的多工可以減少服務之間的串連次數,和函數一樣的調用方式也有效提升了開發效率。
grpc提供有go版本,下面介紹一下grpc在golang中的使用。
安裝
grpc支援1.5及以上版本。
用以下命令安裝grpc-go:
go get google.golang.org/grpc
安裝Protocol Buffers v3
去https://github.com/google/protobuf/releases下載最新的穩定的版本,然後解壓縮,把裡面的檔案放到$PATH中。
安裝外掛程式
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
別忘了將$GOPATH/bin添加到$PATH中:
export PATH=$PATH:$GOPATH/bin
樣本
範例程式碼擷取地址:https://github.com/andyidea/go-example。
代碼檔案結構如下
├── bin│ ├── grpc-client│ └── grpc-server└── src └── grpc-helloworld ├── greeter_client │ └── main.go ├── greeter_server │ └── main.go └── helloworld ├── helloworld.pb.go └── helloworld.proto
grpc-helloworld裡有三個包,greeter_client是用戶端代碼,greeter_server是服務端代碼,helloworld是協議檔案。
先看下協議。
helloworld.proto
syntax = "proto3";option java_multiple_files = true;option java_package = "io.grpc.examples.helloworld";option java_outer_classname = "HelloWorldProto";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 greetingsmessage HelloReply { string message = 1;}
協議中定義了兩個結構體HelloRequest和HelloReply,還有一個函數SayHello,函數的參數是HelloRequest,返回HelloReply。
在src/下用下面命令產生協議的go檔案:
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
這樣就產生了helloworld.pb.go協議檔案。
接著我們看下伺服器端的代碼:
package mainimport ( "log" "net" "golang.org/x/net/context" "google.golang.org/grpc" pb "grpc-helloworld/helloworld" "google.golang.org/grpc/reflection")const ( port = ":50051")// server is used to implement helloworld.GreeterServer.type server struct{}// SayHello implements helloworld.GreeterServerfunc (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{Message: "Hello " + in.Name}, nil}func main() { lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) // Register reflection service on gRPC server. reflection.Register(s) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) }}
伺服器端主要邏輯就是實現之前協議中的SayHello方法,這裡是將字串Hello和參數拼接在一起返回。
協議產生的go檔案給了一個RegisterGreeterServer方法,我們用這個方法綁定實現函數的結構體和server。
然後是用戶端代碼:
package mainimport ( "log" "os" "golang.org/x/net/context" "google.golang.org/grpc" pb "grpc-helloworld/helloworld")const ( address = "localhost:50051" defaultName = "world")func main() { // Set up a connection to the server. conn, err := grpc.Dial(address, grpc.WithInsecure()) 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. name := defaultName if len(os.Args) > 1 { name = os.Args[1] } r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Message)}
用戶端的思路也很清晰,建立一個rpc用戶端串連,將這個串連用pb.NewGreeterClient和協議綁定,返回一個client對象,用這個對象就可以調用遠端函數了。
調用輸出如下:
Greeting: Hello world
樣本到此結束。範例程式碼擷取地址:https://github.com/andyidea/go-example。