`
tcspecial
  • 浏览: 893403 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

gRPC C++

阅读更多

 

一. 准备编译环境

安装各种依赖库,详见:Pre-requisites

brew install autoconf automake libtool shtool gflags

 

二. 安装protobuf3

git clone https://github.com/google/protobuf.git
cd protobuf
git checkout v3.5.0
sh ./autogen.sh
./configure --prefix=/usr/local/protobuf/  
make && make install

 

三. 安装grpc

git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
cd grpc
git submodule update --init
make && make install 

 编译成功后会在/usr/local/bin/ 生成grpc各语言插件,如grpc_cpp_plugin,grpc_php_plugin等。

 

四. helloworld教程

详见:gRPC C++ Hello World

 

4.1 编译proto

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

 

4.2 生成stub

protoc --cpp_out=. helloworld.proto
protoc --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin helloworld.proto 

 

4.3 编译运行

Makefile是通过pkg-config方式来查找protobuf, grpc库位置,可直接修改Makefile 指定protobuf, grpc库位置编译。

./greeter_server

./greeter_client 

客户端打印hello world

 

5. 协议分析

grpc使用http2作为通信协议,http2使用多路复用,二进制帧进行数据传输,效率更高。

http2 报文详见:RFC 7540 HTTP/2

 

6.1 setting

每个stream固定9个字节头 + Payload,用于设置连接参数。

setting frame

 

 

 6.2 HEADERS

打开一个流,发起http请求。

http/1中的 method: POST\r\n,http2为len + :method + len + :post,且各头部字段均为小写。

header frame

 

 6.3 DATA

http1 header与data通过\r\n分隔,http2则为各个独立的stream。

data frame

 

 

 参考链接:

grpc cpp demo

http/2 详解

 

 

 

 

 

  • 大小: 383.8 KB
  • 大小: 363.2 KB
  • 大小: 424.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics