Linux C++ gPRC使用说明

2021-12-21  本文已影响0人  Mr_Michael

一、基础概念

1.RPC

RPC 代指远程过程调用(Remote Procedure Call),它的调用包含了传输协议和编码(对象序列号)协议等等。允许运行于一台计算机的程序调用另一台计算机的子程序,而开发人员无需额外地为这个交互作用编程。)

1)RPC 框架

一个完整的 RPC 框架,应包含负载均衡、服务注册和发现、服务治理等功能,并具有可拓展性便于流量监控系统等接入。

常见 RPC 框架:

\ 跨语言 多 IDL 服务治理 注册中心 服务管理
gRPC × × × ×
Thrift × × × ×
Rpcx ×
Dubbo ×

2)RPC优点

简单、通用、安全、效率。

3)RPC 生命周期

4)同步与异步

2.Protobuf

Protocol Buffers 是一种与语言、平台无关,可扩展的序列化结构化数据的方法,用.proto文件表示,常用于通信协议,数据存储等等。相较于 JSON、XML,它更小、更快、更简单,因此也更受开发人员的青眯。

protoc 是 protobuf 协议的编译器,一个.proto文件会生成一个.h和一个.cc文件。

1)语法

详细介绍参考: Language Guide (proto3)

syntax ="proto3";
service SearchService{
    rpc Search(SearchRequest) returns (SearchResponse);
}
message SearchRequest{
  string query =1;
  int32 page_number =2;
  int32 result_per_page =3;
}
message SearchResponse{
...
}

2)数据类型

.proto Type C++ Type Java Type Go Type PHP Type
double double double float64 float
float float float float32 float
int32 int32 int int32 integer
int64 int64 long int64 integer/string
uint32 uint32 int uint32 integer
uint64 uint64 long uint64 integer/string
sint32 int32 int int32 integer
sint64 int64 long int64 integer/string
fixed32 uint32 int uint32 integer
fixed64 uint64 long uint64 integer/string
sfixed32 int32 int int32 integer
sfixed64 int64 long int64 integer/string
bool bool boolean bool boolean
string string String string string
bytes string ByteString []byte string

3)Protobuf对比 XML的优势

3.gRPC

gRPC(gRPC Remote Procedure Calls) 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计,它采用了 Protobuf 作为 IDL(Interface description language)。

1)特点

2)通信过程

image

3)服务定义

gRPC 允许定义四种服务方法

4)gRPC 与 REST 对比

5)gRPC支持的语言与平台

Language OS Compilers / SDK
C/C++ Linux, Mac GCC 4.9+, Clang 3.4+
C/C++ Windows 7+ Visual Studio 2015+
C# Linux, Mac .NET Core, Mono 4+
C# Windows 7+ .NET Core, NET 4.5+
Dart Windows, Linux, Mac Dart 2.12+
Go Windows, Linux, Mac Go 1.13+
Java Windows, Linux, Mac JDK 8 recommended (Jelly Bean+ for Android)
Kotlin Windows, Linux, Mac Kotlin 1.3+
Node.js Windows, Linux, Mac Node v8+
Objective-C macOS 10.10+, iOS 9.0+ Xcode 7.2+
PHP Linux, Mac PHP 7.0+
Python Windows, Linux, Mac Python 3.5+
Ruby Windows, Linux, Mac Ruby 2.3+

6)使用 API

.proto文件中的服务定义开始,gRPC 提供了生成客户端和服务器端代码的协议缓冲区编译器插件。gRPC 用户通常在客户端调用这些 API,并在服务器端实现相应的 API。

二、安装gRPC与protoc

gRPC Server 和 Client互相通讯,需要使用到如下库:

1.CMake编译与安装gRPC

# 选择一个目录来保存本地安装的软件包
export MY_INSTALL_DIR=$HOME/.local
# 将本地bin文件夹添加到路径变量
export PATH="$MY_INSTALL_DIR/bin:$PATH"

# 需要 3.13 或更高版本的cmake
sudo apt install -y cmake
# 或
wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.19.6/cmake-3.19.6-Linux-x86_64.sh
~/.local/bin/cmake -version
cmake version 3.19.6

# 安装构建 gRPC 所需的基本工具
sudo apt install -y build-essential autoconf libtool pkg-config

# 克隆grpc 
git clone --recurse-submodules -b v1.42.0 https://github.com/grpc/grpc

# 构建和安装 gRPC 和协议缓冲区
$ cd grpc
$ mkdir -p cmake/build
$ pushd cmake/build
$ ~/.local/bin/cmake -DgRPC_INSTALL=ON \
      -DgRPC_BUILD_TESTS=OFF \
      -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \
      ../..
$ make -j
$ make install
$ popd

2.安装Protocol Buffers v3

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.1/protobuf-all-3.19.1.zip
unzip protobuf-all-3.19.1.zip
cd protobuf-3.19.1/
./configure
make
make install
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
# 查看版本
$ protoc --version
libprotoc 3.19.1

三、构建并使用C++ gRPC示例

1.示例程序目录

基于https://github.com/grpc/grpc

$ tree grpc/examples/cpp/ -L 2
examples/cpp/
├── cmake
│   └── common.cmake
├── compression
│   ├── BUILD
│   ├── CMakeLists.txt
│   ├── greeter_client.cc
│   ├── greeter_server.cc
│   ├── Makefile
│   └── README.md
├── helloworld      # 示例服务端与客户端程序
│   ├── BUILD
│   ├── cmake
│   ├── cmake_externalproject
│   ├── CMakeLists.txt
│   ├── cocoapods
│   ├── greeter_async_client2.cc
│   ├── greeter_async_client.cc
│   ├── greeter_async_server.cc
│   ├── greeter_callback_client.cc
│   ├── greeter_callback_server.cc
│   ├── greeter_client.cc   # gRPC Client程序
│   ├── greeter_server.cc  # gRPC Server程序
│   ├── Makefile
│   ├── README.md
│   ├── xds_greeter_client.cc
│   └── xds_greeter_server.cc
├── keyvaluestore
│   ├── BUILD
│   ├── caching_interceptor.h
│   ├── client.cc
│   ├── CMakeLists.txt
│   └── server.cc
├── load_balancing
│   ├── BUILD
│   ├── CMakeLists.txt
│   ├── greeter_client.cc
│   ├── greeter_server.cc
│   ├── Makefile
│   └── README.md
├── metadata
│   ├── BUILD
│   ├── CMakeLists.txt
│   ├── greeter_client.cc
│   ├── greeter_server.cc
│   ├── Makefile
│   └── README.md
├── README.md
└── route_guide
    ├── BUILD
    ├── CMakeLists.txt
    ├── helper.cc
    ├── helper.h
    ├── Makefile
    ├── README.md
    ├── route_guide_callback_client.cc
    ├── route_guide_callback_server.cc
    ├── route_guide_client.cc
    ├── route_guide_db.json
    └── route_guide_server.cc
    
$ tree examples/protos/ # 协议缓冲区文件
examples/protos/
├── auth_sample.proto
├── BUILD
├── hellostreamingworld.proto
├── helloworld.proto
├── keyvaluestore.proto
├── README.md
└── route_guide.proto

2.使用协议缓冲区文件

gRPC 服务是使用协议缓冲区定义的,存放路径:examples/protos。服务器客户端存根都有一个SayHello()RPC 方法。

examples/protos/helloworld.proto

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

协议缓冲区文件通过protoc工具生成服务类和消息类的代码

1)服务类文件

#include "helloworld.pb.h"

namespace helloworld {
    
 class StubInterface {
     class Stub final : public StubInterface {
       public:
        class async final :
          public StubInterface::async_interface {
         public:
          void SayHello(::grpc::ClientContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response, std::function<void(::grpc::Status)>) override;
          void SayHello(::grpc::ClientContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response, ::grpc::ClientUnaryReactor* reactor) override;
          void SayHelloAgain(::grpc::ClientContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response, std::function<void(::grpc::Status)>) override;
          void SayHelloAgain(::grpc::ClientContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response, ::grpc::ClientUnaryReactor* reactor) override;
         private:
          friend class Stub;
          explicit async(Stub* stub): stub_(stub) { }
          Stub* stub() { return stub_; }
          Stub* stub_;
        };
        class async* async() override { return &async_stub_; }
     };
     // 定义客户端可以创建根存Stub的方法
     static std::unique_ptr<Stub> NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options = ::grpc::StubOptions());
 };
    
  // 定义服务端需要实现的方法
 class Service : public ::grpc::Service {
   public:
    Service();
    virtual ~Service();
    // Sends a greeting
    virtual ::grpc::Status SayHello(::grpc::ServerContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response);
    // Sends another greeting
    virtual ::grpc::Status SayHelloAgain(::grpc::ServerContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response);
  };   
}

2)消息类文件

提供对protobuf定义的消息进行读写等操作的方法

// HelloRequest

// string name = 1;
inline void HelloRequest::clear_name() {
  name_.ClearToEmpty();
}
inline const std::string& HelloRequest::name() const {
  // @@protoc_insertion_point(field_get:helloworld.HelloRequest.name)
  return _internal_name();
}
template <typename ArgT0, typename... ArgT>
inline PROTOBUF_ALWAYS_INLINE
void HelloRequest::set_name(ArgT0&& arg0, ArgT... args) {
 
 name_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
  // @@protoc_insertion_point(field_set:helloworld.HelloRequest.name)
}
inline std::string* HelloRequest::mutable_name() {
  std::string* _s = _internal_mutable_name();
  // @@protoc_insertion_point(field_mutable:helloworld.HelloRequest.name)
  return _s;
}
inline const std::string& HelloRequest::_internal_name() const {
  return name_.Get();
}
inline void HelloRequest::_internal_set_name(const std::string& value) {
  
  name_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArenaForAllocation());
}
inline std::string* HelloRequest::_internal_mutable_name() {
  
  return name_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArenaForAllocation());
}
inline std::string* HelloRequest::release_name() {
  // @@protoc_insertion_point(field_release:helloworld.HelloRequest.name)
  return name_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaForAllocation());
}
inline void HelloRequest::set_allocated_name(std::string* name) {
  if (name != nullptr) {
    
  } else {
    
  }
  name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name,
      GetArenaForAllocation());
  // @@protoc_insertion_point(field_set_allocated:helloworld.HelloRequest.name)
}



// HelloReply

// string message = 1;
inline void HelloReply::clear_message() {
  message_.ClearToEmpty();
}
inline const std::string& HelloReply::message() const {
  // @@protoc_insertion_point(field_get:helloworld.HelloReply.message)
  return _internal_message();
}
template <typename ArgT0, typename... ArgT>
inline PROTOBUF_ALWAYS_INLINE
void HelloReply::set_message(ArgT0&& arg0, ArgT... args) {
 
 message_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
  // @@protoc_insertion_point(field_set:helloworld.HelloReply.message)
}
inline std::string* HelloReply::mutable_message() {
  std::string* _s = _internal_mutable_message();
  // @@protoc_insertion_point(field_mutable:helloworld.HelloReply.message)
  return _s;
}
inline const std::string& HelloReply::_internal_message() const {
  return message_.Get();
}
inline void HelloReply::_internal_set_message(const std::string& value) {
  
  message_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArenaForAllocation());
}
inline std::string* HelloReply::_internal_mutable_message() {
  
  return message_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArenaForAllocation());
}
inline std::string* HelloReply::release_message() {
  // @@protoc_insertion_point(field_release:helloworld.HelloReply.message)
  return message_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaForAllocation());
}
inline void HelloReply::set_allocated_message(std::string* message) {
  if (message != nullptr) {
    
  } else {
    
  }
  message_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), message,
      GetArenaForAllocation());
  // @@protoc_insertion_point(field_set_allocated:helloworld.HelloReply.message)
}

3.gRPC Server

greeter_server.cc

// greeter_server.cc
#include <iostream>
#include <memory>
#include <string>

#include <grpcpp/grpcpp.h>

#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;

// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request,
                  HelloReply* reply) override {
    std::string prefix("Hello ");
    reply->set_message(prefix + request->name());
    return Status::OK;
  }
};

void RunServer() {
  std::string server_address("0.0.0.0:50051");
  GreeterServiceImpl service;

  ServerBuilder builder;
  // Listen on the given address without any authentication mechanism.
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  // Register "service" as the instance through which we'll communicate with
  // clients. In this case it corresponds to an *synchronous* service.
  builder.RegisterService(&service);
  // Finally assemble the server.
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  // Wait for the server to shutdown. Note that some other thread must be
  // responsible for shutting down the server for this call to ever return.
  server->Wait();
}

int main(int argc, char** argv) {
  RunServer();

  return 0;
}

4.gRPC Client

greeter_client.cc

#include <iostream>
#include <memory>
#include <string>

#include <grpcpp/grpcpp.h>

#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;

class GreeterClient {
 public:
  GreeterClient(std::shared_ptr<Channel> channel)
      : stub_(Greeter::NewStub(channel)) {}

  // Assembles the client's payload, sends it and presents the response back
  // from the server.
  std::string SayHello(const std::string& user) {
    // Data we are sending to the server.
    HelloRequest request;
    request.set_name(user);

    // Container for the data we expect from the server.
    HelloReply reply;

    // Context for the client. It could be used to convey extra information to
    // the server and/or tweak certain RPC behaviors.
    ClientContext context;

    // The actual RPC.
    Status status = stub_->SayHello(&context, request, &reply);

    // Act upon its status.
    if (status.ok()) {
      return reply.message();
    } else {
      std::cout << status.error_code() << ": " << status.error_message()
                << std::endl;
      return "RPC failed";
    }
  }

 private:
  std::unique_ptr<Greeter::Stub> stub_;
};

int main(int argc, char** argv) {
  // Instantiate the client. It requires a channel, out of which the actual RPCs
  // are created. This channel models a connection to an endpoint (in this case,
  // localhost at port 50051). We indicate that the channel isn't authenticated
  // (use of InsecureChannelCredentials()).
  GreeterClient greeter(grpc::CreateChannel(
      "localhost:50051", grpc::InsecureChannelCredentials()));
  std::string user("world");
  std::string reply = greeter.SayHello(user);
  std::cout << "Greeter received: " << reply << std::endl;

  return 0;
}

5.编译示例工程

编译helloworld程序

$ cd examples/cpp/helloworld
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
$ make -j
$ popd

编译所得文件

$ tree grpc/examples/cpp/helloworld/cmake/build/ -L 1
cmake/build/
├── CMakeCache.txt
├── CMakeFiles
├── cmake_install.cmake
├── greeter_async_client
├── greeter_async_client2
├── greeter_async_server
├── greeter_callback_client
├── greeter_callback_server
├── greeter_client
├── greeter_server
├── helloworld.grpc.pb.cc       # 生成服务实现类代码
├── helloworld.grpc.pb.h        # 生成服务实现类的头文件 
├── helloworld.pb.cc    # 生成消息实现类代码
├── helloworld.pb.h     # 生产消息实现类头文件
├── libhw_grpc_proto.a
└── Makefile

测试示例

$ cd cmake/build

# 运行服务器
$ ./greeter_server

# 从不同的终端运行客户端并查看客户端输出:
$ ./greeter_client
Greeter received: Hello world

四、更新 gRPC 服务

1.修改.proto文件

对helloworld.proto添加一个SayHelloAgain()具有相同请求和响应类型的新方法:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

2.重新生成 gRPC 代码

cd examples/cpp/helloworld/cmake/build
make -j

3.更新应用程序

有新生成的服务器和客户端代码,但仍然需要在示例应用程序的人工编写部分中实现和调用新方法。

上一篇 下一篇

猜你喜欢

热点阅读