使用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

 

posted @ 2019-07-01 16:14  杉枫  阅读(6159)  评论(0编辑  收藏  举报