gRPC 初探

gRPC 初探

  • 前言
  • 安裝
  • 使用
  • 參考

前言

gRPC 出來好久了,不少地方都在使用中。由於是google 出品的因此一直受到很大的關注。php

在實際的學習中,和其餘的rpc框架仍是有些特色:html

  1. 跨語言,若是項目是跨語言通訊的能夠考慮使用gRPC。
  2. gRPC 基於 HTTP/2 標準設計,使其在移動設備上表現更好,更省電和節省空間佔用。
  3. 通訊格式默認使用protocolbuffer(是google 的一種數據交換的格式,它獨立於語言,獨立於平臺),二進制,性能好,如今使用 proto3的新風格。

下面是使用中的一些筆記,已 golang 爲基礎git

安裝

咱們先安裝gRPC-Go:
#直接使用 get 安裝,源碼中包括了後面的examples
go get -u google.golang.org/grpc

如今還不能使用gRPC,還須要安裝前面提到的protocolbuffergithub

protocolbuffer 安裝,golang須要編譯安裝
git clone git@github.com:google/protobuf.git
cd protobuf/

 #以防萬一
brew install autoconf automake libtool

./autogen.sh

./configure
make
make check
sudo make install
  
 #驗證,命令
protoc
安裝go的protobuf插件
#安裝,提供golang運行protobuf的環境和go服務端源碼生成功能(?)
 go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
 
 #protoc-gen-go 命令須要直接能被執行到
 export PATH=$PATH:$GOPATH/bin

使用

目錄結構golang

helloworld/helloworld.proto
此爲 protobuf 的IDL,定義了rpc服務的具體內容,內容不作解釋了

helloworld/helloworld.pb.go
此爲根據 helloworld.proto 生成golang的protobuf定義文件,客戶端服務端同時須要引入並使用

咱們直接看例子,先跑起來框架

#第一個例子目錄
cd GOPAHT/src/google.golang.org/grpc/examples/helloworld

 #運行go 服務端
go run greeter_server/main.go
 
 #運行go 客戶端
go run greeter_client/main.go haha

 #客戶端返回內容,成功!
2017/10/10 01:09:24 Greeting: Hello haha
使用代碼生成插件
#修改proto文件時,須要使用命令重現生成go的protobuf依賴文件
 
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
修改咱們的helloworld代碼

下面咱們修改下helloworld代碼在完整的試一試tcp

helloworld/helloworld.proto性能

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  
  //咱們新增了 SayHelloAgain rpc 方法
  rpc SayHelloAgain (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;
}

greeter_server/main.go學習

type server struct{}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}

//咱們對應增長了 服務端的 支持 SayHelloAgain
func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
        return &pb.HelloReply{Message: "Hello again " + 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)
    }
}
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)
    
    //咱們又加了 客戶端 的調用邏輯
    r, err = c.SayHelloAgain(context.Background(), &pb.HelloRequest{Name: name})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }
    log.Printf("Greeting: %s", r.Message)
    
}
#使用protoc 重現生成 protobuf依賴文件  
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

 #執行驗證
go run greeter_client/main.go haha
 
 #返回,調用了兩次rpc
2017/10/10 01:48:20 Greeting: Hello haha
2017/10/10 01:48:20 Greeting: Hello again haha

這就是gRPC初探,後面會寫一篇php作爲客戶端使用gRPC的小文章。ui


參考

相關文章
相關標籤/搜索