使用grpc C++功能

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

 

rz 遠程上傳文件html

 

本地開發環境搭建:java

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

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

三、編譯例子程序,可以正確編譯運行說明程序沒有問題。git

四、經過例子程序擴展進行本身業務開發。github

 

線上部署主要docker環境下:redis

 

 

pkg-config:docker

簡介:緩存

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命令升級可能很差用,須要本身安裝新版本

http://www.javashuo.com/article/p-ecmpoyko-de.html

http://www.javashuo.com/article/p-qpyinwkr-dq.html

 

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

http://www.javashuo.com/article/p-nbiumows-dq.html

 

其餘:

google 開源項目風格

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

redis帶註釋鏈接

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

相關文章
相關標籤/搜索