c++实现一个简易的rpc服务
时间: 2024-03-05 10:47:32 浏览: 18
RPC(Remote Procedure Call)是一种远程调用协议,它允许客户端程序调用远程服务器上的函数或方法。C++可以使用一些库来实现RPC服务,其中比较流行的有 gRPC 和 Apache Thrift。
以下是一个使用 gRPC 的简易 RPC 服务的示例:
1. 首先,需要安装 gRPC 和 Protocol Buffers:
```
sudo apt install -y build-essential autoconf libtool pkg-config grpc libgrpc++-dev protobuf-compiler-grpc
```
2. 创建一个 Protocol Buffers 文件 `example.proto`,定义 RPC 服务的接口:
```
syntax = "proto3";
package example;
service ExampleService {
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
```
3. 使用 Protocol Buffers 编译器生成 C++ 代码:
```
protoc --grpc_out=. --cpp_out=. example.proto
```
4. 实现 RPC 服务的接口:
```
#include <iostream>
#include <memory>
#include <string>
#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::HelloResponse;
using example::ExampleService;
class ExampleServiceImpl final : public ExampleService::Service {
Status SayHello(ServerContext* context, const HelloRequest* request,
HelloResponse* response) override {
std::string prefix("Hello ");
response->set_message(prefix + request->name());
return Status::OK;
}
};
void RunServer() {
std::string server_address("0.0.0.0:50051");
ExampleServiceImpl service;
grpc::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;
}
```
5. 编译并运行服务器代码:
```
g++ -std=c++11 -I. -I/usr/local/include -L/usr/local/lib example.pb.cc example.grpc.pb.cc example_server.cc -lgrpc++ -lgrpc -lgpr -lprotobuf -lpthread -o example_server
./example_server
```
6. 编写客户端代码:
```
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "example.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using example::HelloRequest;
using example::HelloResponse;
using example::ExampleService;
class ExampleClient {
public:
ExampleClient(std::shared_ptr<Channel> channel)
: stub_(ExampleService::NewStub(channel)) {}
std::string SayHello(const std::string& name) {
HelloRequest request;
request.set_name(name);
HelloResponse response;
ClientContext context;
Status status = stub_->SayHello(&context, request, &response);
if (status.ok()) {
return response.message();
} else {
return "RPC failed";
}
}
private:
std::unique_ptr<ExampleService::Stub> stub_;
};
int main(int argc, char** argv) {
ExampleClient client(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
std::string name("World");
std::string reply = client.SayHello(name);
std::cout << "Received: " << reply << std::endl;
return 0;
}
```
7. 编译并运行客户端代码:
```
g++ -std=c++11 -I. -I/usr/local/include -L/usr/local/lib example.pb.cc example.grpc.pb.cc example_client.cc -lgrpc++ -lgrpc -lgpr -lprotobuf -lpthread -o example_client
./example_client
```
以上是一个简易的使用 gRPC 实现的 RPC 服务和客户端的示例。