1. What is gRPC
gRPC (Google Remote Procedure Call) is an open source high-performance remote procedure call (RPC) framework developed by Google for implementing cross-language service communication in distributed systems. It is based on the HTTP/2 protocol and uses Protocol Buffers (protobuf) as the data exchange format. gRPC is suitable for providing efficient inter-service communication in multilingual environments, especially in microservice architectures.
2. gRPC's workflow
1. Service definition:
Use Protocol Buffers to define service interfaces and methods, including input parameters and return values. For example, the following is a simple service definition example:
// syntax = "proto3"; // Declare the package name in protobufpackage message; service Greeter { rpc SayHello(HelloRequest) returns (HelloResponse); } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; }
2. Generate code:
Use the tools provided by gRPC to generate corresponding code based on the proto file
protoc -I="." --grpc_out="." --plugin=protoc-gen-grpc=/usr/bin/grpc_cpp_plugin protoc -I="." --cpp_out="."
3. Implement the server:
The server implements the defined service logic to handle client requests
4. Client calls:
The client initiates a remote call using the generated stub. The call process is similar to the local method call.
5. Message Serialization and Transmission
The client serializes the message into binary format and sends it to the server via HTTP/2. After receiving the request, the server deserializes it and executes the corresponding logic, and then returns the result to the client.
3. Server side (C++)
Implementing retroreflection server: The content sent between the client and the server is consistent
using namespace std; using namespace boost::asio; using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using message::HelloResponse; using message::HelloRequest; using message::Greeter; class gRPCServerImpl final : public Greeter::Service { public: gRPCServerImpl(){}; Status SayHello(ServerContext* context, const HelloRequest* request, HelloResponse* response){ cout << "gRPC Server received message: " << request->name() << endl; response->set_message(request->name()); return Status::OK; } }; void RunServer(std::string port) { std::string server_address("0.0.0.0:" + port); gRPCServerImpl service; // Create and start gRPC server grpc::ServerBuilder builder; // Listen to ports and add services (server_address, grpc::InsecureServerCredentials()); (&service); unique_ptr<grpc::Server> server(()); cout << "Server listening on " << server_address << endl; boost::asio::io_context io_context; boost::asio::signal_set signals(io_context, SIGINT, SIGTERM); signals.async_wait([&server](const boost::system::error_code& error, int signal_number){ if(!error){ server->Shutdown(); cout << "Server ShutDown!" << endl; } }); thread([&io_context](){ io_context.run(); }).detach(); server->Wait(); io_context.stop(); } int main() { try{ std::string port = "10086"; RunServer(port); }catch(exception& e){ cerr << () << endl; } return 0; }
4. Client (c++)
using namespace std; using namespace boost::asio; using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using message::HelloResponse; using message::HelloRequest; using message::Greeter; void gRPCclient() { grpc::ClientContext io_context; HelloResponse Rep; HelloRequest Req; std::string address_server("192.168.49.130:10086"); auto channle = grpc::CreateChannel(address_server, grpc::InsecureChannelCredentials()); auto stub = Greeter::NewStub(channle); std::string message; std::cout << "Please enter: "; cin >> message; Req.set_name(message); Status status = stub->SayHello(&io_context, Req, &Rep); if(()){ cout << () << endl; }else{ cout << "mistake" << endl; } } int main() { gRPCclient(); return 0; }
This is the end of this article about C++ using grpc to implement retroreflection servers. For more related content of C++ grpc to reflex servers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!