gPRC学习笔记

gPRC学习笔记

什么是RPC

  • RPC(remote procedure call) -- 远程过程调用(相对于本地调用的概念)。

    • 本地调用
      • ex:本地的函数调用
      • 在函数调用的时候,一般会经过几个步骤
      • 返回地址入栈
      • 参数入栈
      • 提升堆栈空间
      • 函数参数的复制
      • 执行函数调用
      • 清空堆栈
    • 为什么需要RPC?
      • ex:两台机器 一台机器想调用另一台机器的函数执行某个功能
      • 由于是两个不同的进程 我们无法使用函数指针来调用该函数
      • 而只能通过网络请求来调用的具体函数
  • 那么如何知道需要具体调用的函数呢?(关联)

    • 两台机器之间可以各自维护一个关联式容器 从而找到要调用的函数
  • 一次完整的RPC调用流程

    1. client 以本地的方式进行的调用服务
    2. client stub接收到调用后负责将方法 参数等组装成能够进行网络传输的消息体
    3. cleint stub找到服务端的地址 并将消息发送给server stub
    4. server stub收到消息后进行解码
    5. server stub根据解码结果调用本地的服务
    6. 本地服务执行结果并将结果返回后给server stub
    7. server stub将返回结果打包成消息并发送至消费方
    8. client stub接受到消息
    9. client得到返回结果

为什么需要proto buffer

  • 在许多高级语言中, 我们都是使用类的方式对数据进行封装, 而利用网络传递数据只能以二进制的方式进行传输, 因此我们需要将数据转化为二进制数据从而在网络上进行传播过程称为序列化, 再将接收到的数据从二进制转化为对应的数据类型, 称为反序列化。
  • proto buffer是Google使用的一个开源软件,数据打包小,数据传输快。

proto buffer基础教程

  • proto buffer协议的格式
    • .proto文件
    • package name;//可以理解为c++中的名称空间
    • message dataname; //message可以理解为c++中的class 或者struct
    • requried type name = 1 //消息的接收方和发送发都必须提供required修饰字段的值
    • opitional type name = 2 //可选择的
    • =1 =2 表示唯一标记的tag(maybe传输的过程中传递的是tag 让后通过tag找到对应的数据)
  • protobuf入门教程
    • 编译.proto文件,protoc addressbook.proto --cpp_out=./;
    • protoc 是protobuf自带的编译工具,将.proto文件生成指定的类。
    • -cpp_out:指定输出特定的语言和路径。
    • 通过protoc工具编译.proto文件时,编译器将生成所选择语言的代码,这些代码可以操作在.proto文件中定义的消息类型,包括获取、设置字段值,将消息序列化到一个输出流中,以及从一个输入流中解析消息。
    • 对C++来说,编译器会为每个.proto文件生成一个.h文件和一个.cc文件,.proto文件中的每一个消息有一个对应的类。
    • pkg-config –cflags protobuf:列出指定共享库的预处理和编译flags,在终端中执行。
    • -I/path/to/protobuf/include -L/path/to/protobuf/lib -lprotobuf -lpthread --- 编译protoc生成的cc和h文件。
  • Protocol Buffers C++入门教程.
    • protobuf(Protocol Buffers )是Google的开源项目,是Google的中立于语言、平台,可扩展的用于序列化结构化数据的解决方案。
    • 简单的说,protobuf是用来对数据进行序列化和反序列化。
数据的序列化和反序列化
  • 序列化 (Serialization):将数据结构或对象转换成二进制串的过程。
  • 反序列化(Deserialization):将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程。
  • JSON简介: JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。它基于ECMAScript的一个子集,采用完全独立于语言的文本格式来存储和表示数据,这些特性使JSON成为理想的数据交换语言,易于人阅读和编写,同时也易于机器解析和生成,一般用于网络传输。
  • JSON的语法规则:
    • json语法是JavaScript对象表示语法的子集,数据在键值中;
    • 数据由逗号分隔;
    • 花括号保存对象;
    • 花括号保存数组;
    {
        "ID":"312822199204085698",
        "gender":0,
        "major":"math",
        "name":18
    }
    
  • JSON支持的类型有:
    • 数字(整数或浮点数);
    • 字符串(在双引号中);
    • 逻辑值(true或false);
    • 数组(在方括号中);
    • 对象(在花括号中);
  • 当网络中不同主机进行数据传输时,我们就可以采用JSON进行传输。
  • 将现有的数据对象转换为JSON字符串就是对对象的序列化操作,将接收到的JSON字符串转换为我们需要的对象,就是反序列化操作。
  • XML(Extensive Markup Language),可扩展标记语言,用结构化的方式来表示数据,和JSON一样,都是一种数据交换格式。
    • C++可以对象可以序列化为XML,用于网络传输或存储。
    • XML具有统一的标准、可移植性高等优点,但因为文件格式复杂,导致格式化数据较大,传输占用带宽大,其在序列化和反序列化场景中,没有JSON常见。
  • Google Protocal Buffers是Google内部使用得而数据编码方式,旨在用来代替XML进行数据交换,可用于数据序列化与反序列化。
    • 高效;
    • 语言中立(C++, Java, Python等)。
    • 可扩展。
  • Boost Serialization可以差U年间或重建程序中的等效结构,并保存为二进制数据、文本数据、JSON/XML或者用户自定义的其他文件,该库的有点有:
    • 代码可移植性(实现仅依赖于ANSI C++)。
    • 深度指针保存与恢复。
    • 可以序列化STL容器和其他常用模板库。
    • 数据可移植。
    • 可以序列化STL容器和其他常用模板库。
    • 数据可移植。
    • 非入侵性。
  • XML产生的数据文件较大,很少使用。MFC和.NET框架的方法适用范围很窄,只适用于Windows下,且.NET框架方法需要.NET的运行环境,但是二者结合Visual Studio IDE使用最为方便。Google Protocol Buffers效率较高,但是数据对象必须预先定义,并使用protoc编译,适合要求效率,允许自定义类型的内部场合使用。Boost.Serialization使用灵活简单,而且支持标准C++容器。
  • protobuf相对而言效率应该是最高的,不管是安装效率还是使用效率,protobuf都很高效,而且protobuf不仅用于C++序列化,还可用于Java和Python的序列化,使用范围很广。
  • protobuf数据类型: protobuf属于轻量级的, 因此不能支持太多的数据类型,sint32使用可变长编码方式,有符号的整型值,负数编码时比通常的int32高效。
  • protobuf使用的一般步骤:
    • 定义proto文件,文件的内容就是定义我们需要存储或者传输的数据结构,也就是定义我们自己饿数据存储或者传输的协议。
    • 编译安装protocol buffer编译器来编译自定义的.proto文件,用于生成.pb.h文件(proto文件中自定义类的头文件)和.pb.cc(proto文件中自定义的实现文件)。
    • 使用protocol buffer的C++ API来读写消息。
  • 定义proto文件就是定义自己的数据存储或者传输的协议格式。
package tutotial;   // .proto文件以一个package声明开始。这个声明是为了防止不同项目之间的命名冲突。
message Student{ // message 一个消息就是某些类型的字段的集合,可以嵌套使用其他的消息类型。
    requried uint64 id = 1;  // requried 是必须提供的字段,否者对应的消息就会被认为是未初始化的。
    requried string name = 2;
    opitional string email = 3; // 字段值指定与否都可以, 如果没有指定一个optional的字段值,他会使用默认值,如果没有默认值,系统默认值会使用: 数据类型的默认值为0,string的默认值为空字符串,bool的默认值为false,对嵌套消息来说,其默认值总是消息的默认实例或者原型。

// 关于标识: '=1'的标志指出了该字段在二进制编码中使用的唯一"标识(tag)";
// 标识号1~15编码所需要的字节数比更大的标识号使用的字节数要少1个,所以,如果你想寻求优化,可以为经常使用或重复的项采用1~15的标识(tag),其他经常使用的optional项采用>=16的标识(tag)。
// 在重复的字段中,每一项都要求重编码标识(tag number),所以重复的字段特别适用于这种优化。
    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
    }

    message PhoneNumber {
        requried string number = 1;
        opitional PhoneType type = 2 [default = HOME];
    }
    repeated PhoneNumber phone = 4; //字段会重复N次(N可以为0)。重复的值的顺序将被保存在protocol buffer中。你只要将重复的字段视为动态大小的数组就可以了。
}
  • equired是永久性的:在把一个字段标识为required的时候,你应该特别小心。如果在某些情况下你不想写入或者发送一个required的字段,那么将该字段更改为optional可能会遇到问题——旧版本的读者(译者注:即读取、解析消息的一方)会认为不含该字段的消息(message)是不完整的,从而有可能会拒绝解析。在这种情况下,你应该考虑编写特别针对于应用程序的、自定义的消息校验函数。Google的一些工程师得出了一个结论:使用required弊多于利;他们更愿意使用optional和repeated而不是required。当然,这个观点并不具有普遍性。
  • 特别注意:
    • protocol buffers和面向对象的设计 protocol buffer类通常只是纯粹的数据存储器(就像C++中的结构体一样);它们在对象模型中并不是一等公民。如果你想向生成的类中添加更丰富的行为,最好的方法就是在应用程序中对它进行封装。如果你无权控制.proto文件的设计的话,封装protocol buffers也是一个好主意(例如,你从另一个项目中重用一个.proto文件)。在那种情况下,你可以用封装类来设计接口,以更好地适应你的应用程序的特定环境:隐藏一些数据和方法,暴露一些便于使用的函数,等等。但是你绝对不要通过继承生成的类来添加行为。这样做的话,会破坏其内部机制,并且不是一个好的面向对象的实践。
  • 编译不过原因是protobuf的连接库默认安装路径是/usr/local/lib,而/usr/local/lib 不在常见Linux系统的LD_LIBRARY_PATH链接库路径这个环境变量里,所以就找不到该lib。LD_LIBRARY_PATH是Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)。所以,解决办法就是修改环境变量LD_LIBRARY_PATH的值。
    • g++ -o protobufTest.out test.cpp student.pb.cc -I/usr/local/include -L/usr/local/lib -lprotobuf -pthread, 命令行编译生成的文件;
  • Protocol Buffers的作用绝不仅仅是简单的数据存取以及序列化。
  • protocol消息类所提供的一个关键特性就是反射。你不需要编写针对一个特殊的消息(message)类型的代码,就可以遍历一个消息的字段,并操纵它们的值,就像XML和JSON一样。
  • “反射”的一个更高级的用法可能就是可以找出两个相同类型的消息之间的区别,或者开发某种“协议消息的正则表达式”,利用正则表达式,你可以对某种消息内容进行匹配。

为什么使用gRPC

  • 有了 gRPC, 我们可以一次性的在一个 .proto 文件中定义服务并使用任何支持它的语言去实现客户端和服务器,反过来,它们可以在各种环境中,从Google的服务器到你自己的平板电脑- gRPC 帮你解决了不同语言间通信的复杂性以及环境的不同.使用 protocol buffers 还能获得其他好处,包括高效的序列号,简单的 IDL 以及容易进行接口更新。

官方的入门教程

gRPC Basics: C++

This tutorial provides a basic C++ programmer's introduction to working with
gRPC. By walking through this example you'll learn how to:

  • Define a service in a .proto file.
  • Generate server and client code using the protocol buffer compiler.
  • Use the C++ gRPC API to write a simple client and server for your service.

It assumes that you are familiar with
protocol buffers.
Note that the example in this tutorial uses the proto3 version of the protocol
buffers language, which is currently in alpha release: you can find out more in
the proto3 language guide
and see the release notes for the
new version in the protocol buffers Github repository.

Why use gRPC?

Our example is a simple route mapping application that lets clients get
information about features on their route, create a summary of their route, and
exchange route information such as traffic updates with the server and other
clients.

With gRPC we can define our service once in a .proto file and implement clients
and servers in any of gRPC's supported languages, which in turn can be run in
environments ranging from servers inside Google to your own tablet - all the
complexity of communication between different languages and environments is
handled for you by gRPC. We also get all the advantages of working with protocol
buffers, including efficient serialization, a simple IDL, and easy interface
updating.

Example code and setup

The example code for our tutorial is in examples/cpp/route_guide.
You also should have the relevant tools installed to generate the server and
client interface code - if you don't already, follow the setup instructions in
BUILDING.md.

Defining the service

Our first step is to define the gRPC service and the method request and
response types using
protocol buffers.
You can see the complete .proto file in
examples/protos/route_guide.proto.

To define a service, you specify a named service in your .proto file:

service RouteGuide {
   ...
}

Then you define rpc methods inside your service definition, specifying their
request and response types. gRPC lets you define four kinds of service method,
all of which are used in the RouteGuide service:

  • A simple RPC where the client sends a request to the server using the stub
    and waits for a response to come back, just like a normal function call.
   // Obtains the feature at a given position.
   rpc GetFeature(Point) returns (Feature) {}
  • A server-side streaming RPC where the client sends a request to the server
    and gets a stream to read a sequence of messages back. The client reads from
    the returned stream until there are no more messages. As you can see in our
    example, you specify a server-side streaming method by placing the stream
    keyword before the response type.
  // Obtains the Features available within the given Rectangle.  Results are
  // streamed rather than returned at once (e.g. in a response message with a
  // repeated field), as the rectangle may cover a large area and contain a
  // huge number of features.
  rpc ListFeatures(Rectangle) returns (stream Feature) {}
  • A client-side streaming RPC where the client writes a sequence of messages
    and sends them to the server, again using a provided stream. Once the client
    has finished writing the messages, it waits for the server to read them all
    and return its response. You specify a client-side streaming method by placing
    the stream keyword before the request type.
  // Accepts a stream of Points on a route being traversed, returning a
  // RouteSummary when traversal is completed.
  rpc RecordRoute(stream Point) returns (RouteSummary) {}
  • A bidirectional streaming RPC where both sides send a sequence of messages
    using a read-write stream. The two streams operate independently, so clients
    and servers can read and write in whatever order they like: for example, the
    server could wait to receive all the client messages before writing its
    responses, or it could alternately read a message then write a message, or
    some other combination of reads and writes. The order of messages in each
    stream is preserved. You specify this type of method by placing the stream
    keyword before both the request and the response.
  // Accepts a stream of RouteNotes sent while a route is being traversed,
  // while receiving other RouteNotes (e.g. from other users).
  rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}

Our .proto file also contains protocol buffer message type definitions for all
the request and response types used in our service methods - for example, here's
the Point message type:

// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
  int32 latitude = 1;
  int32 longitude = 2;
}

Generating client and server code

Next we need to generate the gRPC client and server interfaces from our .proto
service definition. We do this using the protocol buffer compiler protoc with
a special gRPC C++ plugin.

For simplicity, we've provided a Makefile that runs
protoc for you with the appropriate plugin, input, and output (if you want to
run this yourself, make sure you've installed protoc and followed the gRPC code
installation instructions first):

$ make route_guide.grpc.pb.cc route_guide.pb.cc

which actually runs:

$ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto
$ protoc -I ../../protos --cpp_out=. ../../protos/route_guide.proto

Running this command generates the following files in your current directory:

  • route_guide.pb.h, the header which declares your generated message classes
  • route_guide.pb.cc, which contains the implementation of your message classes
  • route_guide.grpc.pb.h, the header which declares your generated service
    classes
  • route_guide.grpc.pb.cc, which contains the implementation of your service
    classes

These contain:

  • All the protocol buffer code to populate, serialize, and retrieve our request
    and response message types
  • A class called RouteGuide that contains
    • a remote interface type (or stub) for clients to call with the methods
      defined in the RouteGuide service.
    • two abstract interfaces for servers to implement, also with the methods
      defined in the RouteGuide service.

Creating the server

First let's look at how we create a RouteGuide server. If you're only
interested in creating gRPC clients, you can skip this section and go straight
to Creating the client (though you might find it interesting
anyway!).

There are two parts to making our RouteGuide service do its job:

  • Implementing the service interface generated from our service definition:
    doing the actual "work" of our service.
  • Running a gRPC server to listen for requests from clients and return the
    service responses.

You can find our example RouteGuide server in
route_guide/route_guide_server.cc. Let's
take a closer look at how it works.

Implementing RouteGuide

As you can see, our server has a RouteGuideImpl class that implements the
generated RouteGuide::Service interface:

class RouteGuideImpl final : public RouteGuide::Service {
...
}

In this case we're implementing the synchronous version of RouteGuide, which
provides our default gRPC server behaviour. It's also possible to implement an
asynchronous interface, RouteGuide::AsyncService, which allows you to further
customize your server's threading behaviour, though we won't look at this in
this tutorial.

RouteGuideImpl implements all our service methods. Let's look at the simplest
type first, GetFeature, which just gets a Point from the client and returns
the corresponding feature information from its database in a Feature.

  Status GetFeature(ServerContext* context, const Point* point,
                    Feature* feature) override {
    feature->set_name(GetFeatureName(*point, feature_list_));
    feature->mutable_location()->CopyFrom(*point);
    return Status::OK;
  }

The method is passed a context object for the RPC, the client's Point protocol
buffer request, and a Feature protocol buffer to fill in with the response
information. In the method we populate the Feature with the appropriate
information, and then return with an OK status to tell gRPC that we've
finished dealing with the RPC and that the Feature can be returned to the
client.

Now let's look at something a bit more complicated - a streaming RPC.
ListFeatures is a server-side streaming RPC, so we need to send back multiple
Features to our client.

Status ListFeatures(ServerContext* context, const Rectangle* rectangle,
                    ServerWriter<Feature>* writer) override {
  auto lo = rectangle->lo();
  auto hi = rectangle->hi();
  long left = std::min(lo.longitude(), hi.longitude());
  long right = std::max(lo.longitude(), hi.longitude());
  long top = std::max(lo.latitude(), hi.latitude());
  long bottom = std::min(lo.latitude(), hi.latitude());
  for (const Feature& f : feature_list_) {
    if (f.location().longitude() >= left &&
        f.location().longitude() <= right &&
        f.location().latitude() >= bottom &&
        f.location().latitude() <= top) {
      writer->Write(f);
    }
  }
  return Status::OK;
}

As you can see, instead of getting simple request and response objects in our
method parameters, this time we get a request object (the Rectangle in which
our client wants to find Features) and a special ServerWriter object. In the
method, we populate as many Feature objects as we need to return, writing them
to the ServerWriter using its Write() method. Finally, as in our simple RPC,
we return Status::OK to tell gRPC that we've finished writing responses.

If you look at the client-side streaming method RecordRoute you'll see it's
quite similar, except this time we get a ServerReader instead of a request
object and a single response. We use the ServerReaders Read() method to
repeatedly read in our client's requests to a request object (in this case a
Point) until there are no more messages: the server needs to check the return
value of Read() after each call. If true, the stream is still good and it
can continue reading; if false the message stream has ended.

while (stream->Read(&point)) {
  ...//process client input
}

Finally, let's look at our bidirectional streaming RPC RouteChat().

  Status RouteChat(ServerContext* context,
                   ServerReaderWriter<RouteNote, RouteNote>* stream) override {
    std::vector<RouteNote> received_notes;
    RouteNote note;
    while (stream->Read(&note)) {
      for (const RouteNote& n : received_notes) {
        if (n.location().latitude() == note.location().latitude() &&
            n.location().longitude() == note.location().longitude()) {
          stream->Write(n);
        }
      }
      received_notes.push_back(note);
    }

    return Status::OK;
  }

This time we get a ServerReaderWriter that can be used to read and write
messages. The syntax for reading and writing here is exactly the same as for our
client-streaming and server-streaming methods. Although each side will always
get the other's messages in the order they were written, both the client and
server can read and write in any order — the streams operate completely
independently.

Starting the server

Once we've implemented all our methods, we also need to start up a gRPC server
so that clients can actually use our service. The following snippet shows how we
do this for our RouteGuide service:

void RunServer(const std::string& db_path) {
  std::string server_address("0.0.0.0:50051");
  RouteGuideImpl service(db_path);

  ServerBuilder builder;
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  builder.RegisterService(&service);
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;
  server->Wait();
}

As you can see, we build and start our server using a ServerBuilder. To do this, we:

  1. Create an instance of our service implementation class RouteGuideImpl.
  2. Create an instance of the factory ServerBuilder class.
  3. Specify the address and port we want to use to listen for client requests
    using the builder's AddListeningPort() method.
  4. Register our service implementation with the builder.
  5. Call BuildAndStart() on the builder to create and start an RPC server for
    our service.
  6. Call Wait() on the server to do a blocking wait until process is killed or
    Shutdown() is called.

Creating the client

In this section, we'll look at creating a C++ client for our RouteGuide
service. You can see our complete example client code in
route_guide/route_guide_client.cc.

Creating a stub

To call service methods, we first need to create a stub.

First we need to create a gRPC channel for our stub, specifying the server
address and port we want to connect to without SSL:

grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials());

Now we can use the channel to create our stub using the NewStub method
provided in the RouteGuide class we generated from our .proto.

public:
 RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db)
     : stub_(RouteGuide::NewStub(channel)) {
   ...
 }

Calling service methods

Now let's look at how we call our service methods. Note that in this tutorial
we're calling the blocking/synchronous versions of each method: this means
that the RPC call waits for the server to respond, and will either return a
response or raise an exception.

Simple RPC

Calling the simple RPC GetFeature is nearly as straightforward as calling a
local method.

  Point point;
  Feature feature;
  point = MakePoint(409146138, -746188906);
  GetOneFeature(point, &feature);

...

  bool GetOneFeature(const Point& point, Feature* feature) {
    ClientContext context;
    Status status = stub_->GetFeature(&context, point, feature);
    ...
  }

As you can see, we create and populate a request protocol buffer object (in our
case Point), and create a response protocol buffer object for the server to
fill in. We also create a ClientContext object for our call - you can
optionally set RPC configuration values on this object, such as deadlines,
though for now we'll use the default settings. Note that you cannot reuse this
object between calls. Finally, we call the method on the stub, passing it the
context, request, and response. If the method returns OK, then we can read the
response information from the server from our response object.

std::cout << "Found feature called " << feature->name()  << " at "
          << feature->location().latitude()/kCoordFactor_ << ", "
          << feature->location().longitude()/kCoordFactor_ << std::endl;

Streaming RPCs

Now let's look at our streaming methods. If you've already read Creating the
server
some of this may look very familiar - streaming RPCs are
implemented in a similar way on both sides. Here's where we call the server-side
streaming method ListFeatures, which returns a stream of geographical
Features:

std::unique_ptr<ClientReader<Feature> > reader(
    stub_->ListFeatures(&context, rect));
while (reader->Read(&feature)) {
  std::cout << "Found feature called "
            << feature.name() << " at "
            << feature.location().latitude()/kCoordFactor_ << ", "
            << feature.location().longitude()/kCoordFactor_ << std::endl;
}
Status status = reader->Finish();

Instead of passing the method a context, request, and response, we pass it a
context and request and get a ClientReader object back. The client can use the
ClientReader to read the server's responses. We use the ClientReaders
Read() method to repeatedly read in the server's responses to a response
protocol buffer object (in this case a Feature) until there are no more
messages: the client needs to check the return value of Read() after each
call. If true, the stream is still good and it can continue reading; if
false the message stream has ended. Finally, we call Finish() on the stream
to complete the call and get our RPC status.

The client-side streaming method RecordRoute is similar, except there we pass
the method a context and response object and get back a ClientWriter.

    std::unique_ptr<ClientWriter<Point> > writer(
        stub_->RecordRoute(&context, &stats));
    for (int i = 0; i < kPoints; i++) {
      const Feature& f = feature_list_[feature_distribution(generator)];
      std::cout << "Visiting point "
                << f.location().latitude()/kCoordFactor_ << ", "
                << f.location().longitude()/kCoordFactor_ << std::endl;
      if (!writer->Write(f.location())) {
        // Broken stream.
        break;
      }
      std::this_thread::sleep_for(std::chrono::milliseconds(
          delay_distribution(generator)));
    }
    writer->WritesDone();
    Status status = writer->Finish();
    if (status.IsOk()) {
      std::cout << "Finished trip with " << stats.point_count() << " points\n"
                << "Passed " << stats.feature_count() << " features\n"
                << "Travelled " << stats.distance() << " meters\n"
                << "It took " << stats.elapsed_time() << " seconds"
                << std::endl;
    } else {
      std::cout << "RecordRoute rpc failed." << std::endl;
    }

Once we've finished writing our client's requests to the stream using Write(),
we need to call WritesDone() on the stream to let gRPC know that we've
finished writing, then Finish() to complete the call and get our RPC status.
If the status is OK, our response object that we initially passed to
RecordRoute() will be populated with the server's response.

Finally, let's look at our bidirectional streaming RPC RouteChat(). In this
case, we just pass a context to the method and get back a ClientReaderWriter,
which we can use to both write and read messages.

std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream(
    stub_->RouteChat(&context));

The syntax for reading and writing here is exactly the same as for our
client-streaming and server-streaming methods. Although each side will always
get the other's messages in the order they were written, both the client and
server can read and write in any order — the streams operate completely
independently.

Try it out!

Build client and server:

$ make

Run the server, which will listen on port 50051:

$ ./route_guide_server

Run the client (in a different terminal):

$ ./route_guide_client

grpc官方hello world的入门教程

gRPC C++ Hello World Tutorial

Install gRPC

Make sure you have installed gRPC on your system. Follow the
BUILDING.md instructions.

Get the tutorial source code

The example code for this and our other examples lives in the examples
directory. Clone this repository to your local machine by running the
following command:

$ git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc

Change your current directory to examples/cpp/helloworld

$ cd examples/cpp/helloworld/

Defining a service

The first step in creating our example is to define a service: an RPC
service specifies the methods that can be called remotely with their parameters
and return types. As you saw in the
overview above, gRPC does this using protocol
buffers
. We
use the protocol buffers interface definition language (IDL) to define our
service methods, and define the parameters and return
types as protocol buffer message types. Both the client and the
server use interface code generated from the service definition.

Here's our example service definition, defined using protocol buffers IDL in
helloworld.proto. The Greeting
service has one method, hello, that lets the server receive a single
HelloRequest
message from the remote client containing the user's name, then send back
a greeting in a single HelloReply. This is the simplest type of RPC you
can specify in gRPC - we'll look at some other types later in this document.

syntax = "proto3";

option java_package = "ex.grpc";

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;
}

Generating gRPC code

Once we've defined our service, we use the protocol buffer compiler
protoc to generate the special client and server code we need to create
our application. The generated code contains both stub code for clients to
use and an abstract interface for servers to implement, both with the method
defined in our Greeting service.

To generate the client and server side interfaces:

$ make helloworld.grpc.pb.cc helloworld.pb.cc

Which internally invokes the proto-compiler as:

$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto

Writing a client

  • Create a channel. A channel is a logical connection to an endpoint. A gRPC
    channel can be created with the target address, credentials to use and
    arguments as follows

    auto channel = CreateChannel("localhost:50051", InsecureChannelCredentials());
    
  • Create a stub. A stub implements the rpc methods of a service and in the
    generated code, a method is provided to created a stub with a channel:

    auto stub = helloworld::Greeter::NewStub(channel);
    
  • Make a unary rpc, with ClientContext and request/response proto messages.

    ClientContext context;
    HelloRequest request;
    request.set_name("hello");
    HelloReply reply;
    Status status = stub->SayHello(&context, request, &reply);
    
  • Check returned status and response.

    if (status.ok()) {
      // check reply.message()
    } else {
      // rpc failed.
    }
    

For a working example, refer to greeter_client.cc.

Writing a server

  • Implement the service interface

    class GreeterServiceImpl final : public Greeter::Service {
      Status SayHello(ServerContext* context, const HelloRequest* request,
          HelloReply* reply) override {
        std::string prefix("Hello ");
        reply->set_message(prefix + request->name());
        return Status::OK;
      }
    };
    
    
  • Build a server exporting the service

    GreeterServiceImpl service;
    ServerBuilder builder;
    builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
    std::unique_ptr<Server> server(builder.BuildAndStart());
    

For a working example, refer to greeter_server.cc.

Writing asynchronous client and server

gRPC uses CompletionQueue API for asynchronous operations. The basic work flow
is

  • bind a CompletionQueue to a rpc call
  • do something like a read or write, present with a unique void* tag
  • call CompletionQueue::Next to wait for operations to complete. If a tag
    appears, it indicates that the corresponding operation is complete.

Async client

The channel and stub creation code is the same as the sync client.

  • Initiate the rpc and create a handle for the rpc. Bind the rpc to a
    CompletionQueue.

    CompletionQueue cq;
    auto rpc = stub->AsyncSayHello(&context, request, &cq);
    
  • Ask for reply and final status, with a unique tag

    Status status;
    rpc->Finish(&reply, &status, (void*)1);
    
  • Wait for the completion queue to return the next tag. The reply and status are
    ready once the tag passed into the corresponding Finish() call is returned.

    void* got_tag;
    bool ok = false;
    cq.Next(&got_tag, &ok);
    if (ok && got_tag == (void*)1) {
      // check reply and status
    }
    

For a working example, refer to greeter_async_client.cc.

Async server

The server implementation requests a rpc call with a tag and then wait for the
completion queue to return the tag. The basic flow is

  • Build a server exporting the async service

    helloworld::Greeter::AsyncService service;
    ServerBuilder builder;
    builder.AddListeningPort("0.0.0.0:50051", InsecureServerCredentials());
    builder.RegisterService(&service);
    auto cq = builder.AddCompletionQueue();
    auto server = builder.BuildAndStart();
    
  • Request one rpc

    ServerContext context;
    HelloRequest request;
    ServerAsyncResponseWriter<HelloReply> responder;
    service.RequestSayHello(&context, &request, &responder, &cq, &cq, (void*)1);
    
  • Wait for the completion queue to return the tag. The context, request and
    responder are ready once the tag is retrieved.

    HelloReply reply;
    Status status;
    void* got_tag;
    bool ok = false;
    cq.Next(&got_tag, &ok);
    if (ok && got_tag == (void*)1) {
      // set reply and status
      responder.Finish(reply, status, (void*)2);
    }
    
  • Wait for the completion queue to return the tag. The rpc is finished when the
    tag is back.

    void* got_tag;
    bool ok = false;
    cq.Next(&got_tag, &ok);
    if (ok && got_tag == (void*)2) {
      // clean up
    }
    

To handle multiple rpcs, the async server creates an object CallData to
maintain the state of each rpc and use the address of it as the unique tag. For
simplicity the server only uses one completion queue for all events, and runs a
main loop in HandleRpcs to query the queue.

For a working example, refer to greeter_async_server.cc.

posted @ 2018-11-25 18:56  coding-for-self  阅读(2363)  评论(0编辑  收藏  举报