Git Product home page Git Product logo

proto-grpc-demo's Introduction

Demo of Using Protocol Buffers and gRPC by C++

1. Rre-requisites

编译、运行此项目代码需要预安装 Protocol BuffersgRPC

1.1. Protocol Buffers

1.2. gRPC

2. Demo Project

2.1. Compiling Source

执行下述命令编译此项目:

mkdir -p build && rm -rf build/* &&                             \
pushd build && /usr/bin/cmake .. &&                             \
/usr/bin/cmake --build . --target clean -- -j 16 &&             \
/usr/bin/cmake --build . --target proto_2_cxx -- -j 16 &&       \
/usr/bin/cmake --build . --target grpc_2_cxx -- -j 16 &&        \
/usr/bin/cmake --build . --target rpc_server_demo -- -j 16 &&   \
/usr/bin/cmake --build . --target rpc_client_demo -- -j 16 &&   \
popd

2.2. Run Demo

执行build/src/server/rpc_server_demo <IPV4:PORT>启动 RPC 服务端:

+-------------------------------+
| RPC-SERVER-DEMO               |
| date:   2021.05.24            |
| author: [email protected] |
+-------------------------------+

RPC Server listen on IPV4:PORT

执行build/src/client/rpc_client_demo <IPV4:PORT>启动 RPC 客户端:

+-------------------------------+
| RPC-CLIENT-DEMO               |
| date:   2021.05.24            |
| author: [email protected] |
+-------------------------------+

RPC Client connect to IPV4:PORT

---> [Point]
latitude: 302131223
longitude: 1202193223

<--- [Feature]
name: "point_2"
location {
  latitude: 302131223
  longitude: 1202193223
}

2.3. Working Sequence

此项目的工作时序如下:

  • 客户端发送给服务端 序列化 的请求数据,接收服务端返回的 序列化 的应答数据,并 反序列化 为一个 C++ 中的对象,进一步处理。

  • 服务端接收客户端发送的 序列化 的请求数据,反序列化 为一个 C++ 中的对象,进一步处理后,返回一个 序列化 的应答数据。

  • 其中数据序列化均方式为 Protocol Buffers,通信框架为 gRPC

2.4. Code Structure

此项目的代码结构如下:

.
├── CMakeLists.txt
├── src
│   ├── CMakeLists.txt
│   ├── client
│   │   ├── CMakeLists.txt
│   │   ├── demo.cpp
│   │   ├── rpc_client.cpp 
│   │   └── rpc_client.h
│   ├── proto
│   │   ├── gen
│   │   │   ├── route_guide.grpc.pb.cc
│   │   │   ├── route_guide.grpc.pb.h
│   │   │   ├── route_guide.pb.cc
│   │   │   └── route_guide.pb.h
│   │   └── route_guide.proto
│   └── server
│       ├── CMakeLists.txt
│       ├── demo.cpp
│       ├── rpc_server.cpp
│       ├── rpc_server.h
│       ├── rpc_service.cpp
│       └── rpc_service.h
└── tools
    └── cmake
        ├── use_absl.cmake
        ├── use_grpc.cmake
        ├── use_hiksdk.cmake
        └── use_proto.cmake

2.5 Tips

在你的 CMakeLists.txt 中引入 use_proto.cmake,设置.proto源文件目录proto_in_dir和生成 C++ 文件目录proto_out_dir,之后项目将增添了一个名为proto_2_cxxtarget

可以参考 CMakeLists.txt

set(proto_in_dir ${PROJECT_SOURCE_DIR}/src/proto)           ## contains of .proto files
set(proto_out_dir ${PROJECT_SOURCE_DIR}/src/proto/gen)      ## contains of .cc, .h files
include(${PROJECT_SOURCE_DIR}/tools/cmake/use_proto.cmake)

编译名为proto_2_cxxtarget

/usr/bin/camke --build . --target proto_2_cxx -- -j 16

proto-grpc-demo's People

Contributors

duruyao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.