3.8 KiB
gRPC
gRPC (gRPC Remote Procedure Calls) is an open-source Remote Procedure Call (RPC) framework that runs on various programming languages, including C++. gRPC is designed to be high-performance, efficient, and scalable, making it ideal for microservice architectures and other applications with high performance requirements.
gRPC uses the Protocol Buffers (Protobuf) serialization format for message exchange and method definition. Protocol Buffers enable more efficient and smaller serialization compared to other formats like JSON or XML.
Protocol Buffers
In gRPC, you start by defining service definitions and message structures in .proto
files. You can define data structures and service interfaces using a compact, language-neutral, platform-neutral binary format.
Here's an example of how that might look:
syntax = "proto3";
package example;
// The gRPC service definition
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The Request message definition
message HelloRequest {
string name = 1;
}
// The Reply message definition
message HelloReply {
string message = 1;
}
After defining the .proto
file, you use the protoc
compiler to generate the corresponding C++ code for your application.
gRPC C++ Server
To create a gRPC server in C++, you first need to implement the service interface generated by the protoc
compiler. Here's an example implementation for the Greeter
service:
#include <grpcpp/grpcpp.h>
#include "example.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using example::HelloRequest;
using example::HelloReply;
using example::Greeter;
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;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
RunServer();
return 0;
}
gRPC C++ Client
Similarly, to create a gRPC C++ client, you use the generated code from protoc
compiler and connect to a server:
#include <grpcpp/grpcpp.h>
#include "example.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using example::HelloRequest;
using example::HelloReply;
using example::Greeter;
class GreeterClient {
public:
GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {}
std::string SayHello(const std::string& user) {
HelloRequest request;
request.set_name(user);
HelloReply reply;
ClientContext context;
Status status = stub_->SayHello(&context, request, &reply);
if (status.ok()) {
return reply.message();
} else {
std::cout << "RPC failed" << std::endl;
return "RPC failed";
}
}
private:
std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char** argv) {
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;
}
This is a basic example demonstrating the client-server communication using gRPC in C++. More advanced features like bi-directional streaming, error handling, and authentication can also be used in gRPC. For more information, you can refer to the gRPC C++ documentation.