gRPC用法

官方文档
前置技能 protobuf

什么是 gRPC?

A high performance, open-source universal RPC framework

RPC : Remote Procedure Call

在gRPC中,客户端应用程序可以直接在其他计算机上的服务器应用程序上调用方法,就好像它是本地对象一样,这使您更轻松地创建分布式应用程序和服务。 与许多RPC系统一样,gRPC围绕定义服​​务的思想,指定可通过其参数和返回类型远程调用的方法。 在服务器端,服务器实现此接口并运行gRPC服务器以处理客户端调用。 在客户端,客户端具有一个存根(在某些语言中仅称为客户端),提供与服务器相同的方法。

如何工作?

在默认情况下 gRPC 使用 protocol bufers 进行序列化结构化数据(serializing structured data),当然也可以使用其他数据格式,比如JSON。建议将proto3gRPC一起使用

工作步骤:(默认使用protobuf的情况下)

  • helloworld.proto文件中定义gRPC服务,将RPC方法参数和返回类型指定为protobuf消息,像下面这样:
// The greeter 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使用protoc工具以及gRPC的插件来生成代码

安装和使用

首先先安装protobufprotoc(见前置技能链接)以及对应语言的支持
对于不同的语言,需要为protoc安装对应的gRPC插件

c++

$ # 克隆仓库,-c选项是添加代理,可以不用
$ git -c http.proxy=socks5://192.168.0.103:1080 clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
$ cd grpc
$ git -c http.proxy=socks5://192.168.0.103:1080 submodule update --init 
$ mkdir build && cd build
$ cmake ..
$ make -j4
$ sudo make install 

仓库目录grpc/examples/cpp/helloworld下有示例源码
仓库目录grpc/examples/protos下有.proto文件,包括helloworld.proto
生成代码的命令是:

$ protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` helloworld.proto

go

$ export https_proxy=socks5://127.0.0.1:1080  # 这个是我自己代理
$ go get -u google.golang.org/grpc

然后应可以在$GOPATH/src/google.golang.org/grpc/examples/helloworld目录找到示例
生成代码的命令$ protoc -I. ./helloworld.proto --go_out=plugins=grpc:.
将在当前目录生成helloworld.pb.go文件
使用:

// server 端
package main

import (
	"context"
	"log"
	"net"

	"google.golang.org/grpc"
	pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

const (
	port = ":50051"
)

// 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() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}

// client 端
package main

import (
	"context"
	"log"
	"os"
	"time"

	"google.golang.org/grpc"
	pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

const (
	address     = "localhost:50051"
	defaultName = "world"
)

func main() {
	// Set up a connection to the server.
	conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
	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]
	}
	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())
}
posted @ 2020-01-16 17:16  kirito1  阅读(491)  评论(0编辑  收藏  举报