使用grpc C++功能


grpc c++開發需要安裝相關工具以及框架才能進行開發。

 

rz 遠程上傳文件

 

本地開發環境搭建:

1、編譯相關工具 pkg-config autoconf automake Libtool shtool gflags等,后邊會進行相關介紹,介紹文章來自於網絡。

2、需要安裝grpc編譯按照后邊文章編譯並進行安裝,protocol buffer建議按照第三方插件安裝避免與grpc安裝版本不匹配。

3、編譯例子程序,能夠正確編譯運行說明程序沒有問題。

4、通過例子程序擴展進行自己業務開發。

 

線上部署主要docker環境下:

 

 

pkg-config:

簡介:

https://www.tianmaying.com/tutorial/pkgconfig

 

makefile文件:

介紹

https://seisman.github.io/how-to-write-makefile/introduction.html

 

autoconf automake

介紹

http://www.laruence.com/2009/11/18/1154.html

 

Libtool

介紹

https://zh.wikipedia.org/wiki/Libtool

https://www.ibm.com/developerworks/cn/aix/library/1007_wuxh_libtool/index.html

 

shtool

介紹

https://www.gnu.org/software/shtool/

 

gflags

介紹

https://blog.csdn.net/jcjc918/article/details/50876613

 

Protocol Buffer

介紹

https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html

https://colobu.com/2015/01/07/Protobuf-language-guide/

 

升級G++版本 通過yum命令升級可能不好用,需要自己安裝新版本

https://www.cnblogs.com/wanpengcoder/p/5218583.html

https://blog.csdn.net/centnethy/article/details/81284657

 

brew是mac下安裝工具命令

 

安裝grpc相關

A、https://tcspecial.iteye.com/blog/2437365

一. 准備編譯環境

安裝各種依賴庫,詳見:Pre-requisites

Shell代碼   收藏代碼
  1. brew install autoconf automake libtool shtool gflags  

 

二. 安裝protobuf3

Shell代碼   收藏代碼
  1. git clone https://github.com/google/protobuf.git  
  2. cd protobuf  
  3. git checkout v3.5.0  
  4. sh ./autogen.sh  
  5. ./configure --prefix=/usr/local/protobuf/    
  6. make && make install  

 

三. 安裝grpc

Shell代碼   收藏代碼
  1. git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc  
  2. cd grpc  
  3. git submodule update --init  
  4. make && make install   

 編譯成功后會在/usr/local/bin/ 生成grpc各語言插件,如grpc_cpp_plugin,grpc_php_plugin等。

 

四. helloworld教程

詳見:gRPC C++ Hello World

 

4.1 編譯proto

Proto代碼   收藏代碼
  1. syntax = "proto3";  
  2.   
  3. option java_package = "ex.grpc";  
  4.   
  5. package helloworld;  
  6.   
  7. // The greeting service definition.  
  8. service Greeter {  
  9.   // Sends a greeting  
  10.   rpc SayHello (HelloRequest) returns (HelloReply) {}  
  11. }  
  12.   
  13. // The request message containing the user's name.  
  14. message HelloRequest {  
  15.   string name = 1;  
  16. }  
  17.   
  18. // The response message containing the greetings  
  19. message HelloReply {  
  20.   string message = 1;  
  21. }  

 

4.2 生成stub

Shell代碼   收藏代碼
  1. protoc --cpp_out=. helloworld.proto  
  2. 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

 

https://www.jianshu.com/p/3479272f90bb

在着手 C++ 的 TensorFlow serving mnist client 的過程中,不斷采坑,被環境安裝折磨的不行。現本着學習回顧,特總結,方便后面同學避免在環境搭建上出現問題。本次完全新建了個環境,在新環境上實驗成功。系統為: Ubuntu 16.04.

如果你只是單純的想安裝 protobuf 那么對於安裝的 protobuf 版本並沒有要求。但是如果要安裝 gRPC 的話,那么需要和 gRPC 版本有所對應,否則私自安裝個 protobuf 並沒有太大意義,因為 clone 下來的 grpc 文件夾里就有對應的文件夾,在這里安裝 protobuf 可以保證安裝 grpc 不出錯。安裝 grpc 不建議先單獨編譯安裝 protobuf,但是本着學習的目的,下面依次介紹了單獨安裝 protobuf 和安裝 grpc&protobuf 的方法。

安裝 protobuf

1.下載 protobuf 並解壓。下載地址:https://github.com/google/protobuf/releases

2.進入解壓后的文件目錄,執行如下操作:

  • ./configure

通常建議安裝到 /usr/local 目錄下,執行 configure 時,指定 --prefix=/usr/local/protobuf 即可

  • make

  • make check

  • sudo make install

3.安裝成功后,將它的 binlib 目錄分別加入到 PATH 和 LD_LIBRARY_PATH 環境變量,以方便直接調用。

設置環境變量過程:編輯 /etc/profile,在文件末尾添加:

export PATH=$PATH:/usr/local/protobuf/bin export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib 

安裝 gRPC

1.安裝依賴:

  • 首先安裝 pkg-configsudo apt-get install pkg-config

  • 然后安裝依賴文件:

sudo apt-get install autoconf automake libtool make g++ unzip
sudo apt-get install libgflags-dev libgtest-dev
sudo apt-get install clang libc++-dev

2.下載GRPC

git clone https://github.com/grpc/grpc.git cd grpc git submodule update --init //更新第三方源碼 

4.安裝 protobuf 源碼:

cd third_party/protobuf/
git submodule update --init --recursive    //確保克隆子模塊,更新第三方源碼 ./autogen.sh //生成配置腳本 ./configure //生成Makefile文件,為下一步的編譯做准備,可以加上安裝路徑:--prefix=path make //從Makefile讀取指令,然后編譯 make check //可能會報錯,但是不影響 
sudo make install

從 Makefile 讀取指令,安裝到指定位置,默認為 /usr/local/,也可以指定安裝目錄:--prefix=path。卸載的命令為 make uninstall

相關命令說明:

  • make clean:清除編譯產生的可執行文件及目標文件 (object file,*.o)

  • make distclean:除了清除可執行文件和目標文件外,把 configure 所產生的 Makefile 也清除掉。

  • sudo ldconfig:更新共享庫緩存

  • which protoc:查看軟件的安裝位置

  • protoc --version:檢查是否安裝成功

5.安裝GRPC

cd ../..  //進入 grpc 根目錄 make //從Makefile讀取指令,然后編譯 
sudo make install

從 Makefile 讀取指令,安裝到指定位置,默認為 /usr/local/,具體的位置在 binlib 目錄下。

6.測試

在 gRPC 目錄下:

cd examples/cpp/helloworld/
make                //編譯 ./greeter_server //服務器 ./greeter_client //客戶端 

出現 Greeter received: Hello world 則表示安裝成功。



作者:鄭爽_Shaun
鏈接:https://www.jianshu.com/p/3479272f90bb
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。
 
第一步安裝protobuf不是必須的,因為第三方里面有protobuf安裝。--------備注
 

 

 C:

安裝Homebrew mac下集成安裝環境

https://blog.csdn.net/yemao_guyue/article/details/80575532

安裝protobuf權限問題

https://blog.csdn.net/ccbrid/article/details/79169440

https://blog.csdn.net/qq_25147897/article/details/78544395

 

D: 

編譯demo

https://github.com/grpc/grpc/tree/master/examples/cpp/helloworld 

編譯demo問題

https://www.jianshu.com/p/cdbdda59e99f

拷貝protobuf頭文件到usr/local下

mv protoc3/include/* /usr/local/include/

pkg-config編譯問題

https://github.com/pjreddie/darknet/issues/916

 

E:linux下安裝grpc

https://www.cnblogs.com/xuelisheng/p/10316431.html

 

其他:

google 開源項目風格

https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/

redis帶注釋連接

https://github.com/huangz1990/redis-3.0-annotated

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM