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了。