1、grpc概述html
GRPC是google开源的一个高性能、跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x +。GRPC与thrift、avro-rpc等其实在整体原理上并无太大的区别,简而言之GRPC并无太多突破性的创新。c++
对于开发者而言:git
1)须要使用protobuf定义接口,即.proto文件github
2)而后使用compile工具生成特定语言的执行代码,好比JAVA、C/C++、Python等。相似于thrift,为了解决跨语言问题。网络
3)启动一个Server端,server端经过侦听指定的port,来等待Client连接请求,一般使用Netty来构建,GRPC内置了Netty的支持。多线程
4)启动一个或者多个Client端,Client也是基于Netty,Client经过与Server创建TCP长连接,并发送请求;Request与Response均被封装成HTTP2的stream Frame,经过Netty Channel进行交互。并发
2、编译框架
一、所需工具:操做系统:Win7 64位 、Visual Studio 2015 、CMake工具
二、编译步骤:性能
1.1 在 https://github.com/grpc/grpc/tree/v1.0.1 上下载 grpc-1.0.1.zip 文件(下载路径:https://github.com/grpc/grpc/archive/v1.0.1.zip)文件,将文件解压到 grpc-1.0.1。
1.2 切换到 third_party 目录,分别下载依赖的其它项目,下达完成以后将依赖项分别解压到 third_party 下的对应目录下。
如 在2017.01.17日,tag v1.0.1 对应的依赖项连接以下:
https://codeload.github.com/grpc/grpc/zip/v1.0.1
https://codeload.github.com/google/boringssl/zip/c880e42ba1c8032d4cdde2aba0541d8a9d9fa2e9
https://codeload.github.com/gflags/gflags/zip/05b155ff59114735ec8cd089f669c4c3d8f59029
https://codeload.github.com/google/googletest/zip/c99458533a9b4c743ed51537e25989ea55944908
https://codeload.github.com/google/protobuf/zip/1a586735085e817b1f52e53feec92ce418049f69
https://codeload.github.com/madler/zlib/zip/50893291621658f355bc5b4d450a8d06a563053d
参考readme用CMAKE生成工程文件,编译便可。
2.一、下载protobuf依赖项gmock和gtest
下载gmock(https://github.com/google/googlemock/archive/release-1.7.0.zip)和gtest(https://github.com/google/googletest/archive/release-1.7.0.zip),分别解压到grpc-1.0.1\third_party\protobuf\gmock 和 grpc-1.0.1\third_party\protobuf\gmock\gtest\ 目录下。
2.二、使用CMake编译protobuf
首先打开vs2015开发人员命令提示符窗口(使用 "VS2015 x86 本机工具命令提示符" 工具),切换到对应的protobuf目录。
cd grpc-1.0.1\third_party\protobuf\cmake
mkdir build & cd build & mkdir install
mkdir debug & cd debug
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../install ../..
nmake && nmake install
生成完成,在grpc-1.0.1\third_party\protobuf\cmake\build\install\lib 目录下面有对应的Lib文件。在cmake目录下面mkdir debug,而后把install/lib目录下的全部库文件拷贝的debug路径,并把后缀d去掉。例如protobuf生成的库名称为libprotocd.lib,应该更名成libprotoc.lib。其余的依次类推。后面编译grpc会用到这些库。
在vsprojects里有建好的工程文件,下载nuget.exe,用于依赖包的网络下载。主要是依赖于openssl和zlib库。在编译grpc时,出现编译boringssl,出现不少错误,能够把工程移除能够直接在 grpc.sln 文件中删除 boringssl 工程。
3.一、下载nuget
下载nuget.exe (路径:https://nuget.org/nuget.exe ),并将期复制到 grpc-1.0.1\vsprojects\ 目录下。
3.二、使用nuget下载依赖包
cd grpc-1.0.1\vsprojects
nuget restore grpc.sln
3.三、编译grpc
打开 grpc-1.0.1\vsprojects\grpc.sln 文件,删除boringssl工程,不然后面编译grpc时会报错。若不使用ssl,boringssl能够不编译。
msbuild grpc.sln /p:Configuration=Debug
grpc-1.0.1\vsprojects\Debug\ 目录下会生成grpc相关文件。
3.三、编译grpc_cpp_plugin
msbuild grpc_protoc_plugins.sln /p:Configuration=Debug
grpc-1.0.1\vsprojects\Debug\ 目录下会生成grpc_cpp_plugin相关文件。
参考readme,使用cmake编译。
cd grpc-1.0.1\third_party\zlib\
cmake CMakeLists.txt
再用 vs2015 打开 zlib.sln编译便可,文件生成在 Debug 目录。
3、编写测试例子
将proto.exe、helloworld.proto、grpc_cpp_plugin.exe拷贝到一个文件夹中,grpc_cpp_plugin.exe是gRPC的protoc插件,生成方法参考个人《编译gRPC》一文。
建立一个bat文件,包含如下命令:
protoc.exe -I=. --grpc_out=. --plugin=protoc-gen-grpc=.\grpc_cpp_plugin.exe helloworld.proto protoc.exe -I=. --cpp_out=. helloworld.proto
生成了两套文件
hellowworld.pb.h hellowworld.pb.cc hellowworld.grpc.pb.h hellowworld.grpc.pb.cc
其中前两个是protoc生成的,后两个是插件生成的。
在vsprojects目录建立空的WIN32 C++工程grpc_helloworld.sln,在目录grpc_helloworld下面有两个文件夹。目录结构以下图:
grpc-1.0.1\vsprojects\grpc_helloworld.sln
grpc-1.0.1\vsprojects\grpc_helloworld\grpc_helloworld.vcxproj
grpc-1.0.1\vsprojects\grpc_helloworld\grpc_helloworld.vcxproj.filters
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.pb.h
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.pb.cc
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.grpc.pb.h
grpc-1.0.1\vsprojects\grpc_helloworld\protobuf\helloworld.grpc.pb.cc
grpc-1.0.1\vsprojects\grpc_helloworld\client\greeter_client.cc3
三、设置头文件
D:\XXXX\grpc\include; D:\XXXX\grpc\third_party\protobuf\src; $(ProjectDir)..\proto
三个头文件目录,一个是gRPC的头文件,一个是protobuf的头文件,一个是刚生成的的访问类路径。
路径
D:\XXXX\grpc\third_party\protobuf\cmake\Release; D:\XXXX\grpc\vsprojects\Release; D:\XXXX\grpc\third_party\zlib\solution\Release; D:\XXXX\grpc\vsprojects\packages\grpc.dependencies.openssl.1.0.204.1\build\native\lib\v120\Win32\Release\static
四个库文件路径:protobuf库路径、grpc库路径、zlib库路径、openssl库路径。
没有使用boringssl,openssl的库是从NuGet下载的package中找到的。
库文件
libprotobuf.lib; grpc.lib; gpr.lib; grpc++.lib; Ws2_32.lib; libeay32.lib; ssleay32.lib; zlibstatic.lib; %(AdditionalDependencies)
若是出错,则在工程里作以下设置:
预处理器设定:
_DEBUG;_WIN32_WINNT=0x600;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
代码生成–>运行库 设置为 "多线程调试 (/MTd)"。顺利的话就能够生成对应的exe了。