這篇文章主要介紹如何基于gRPC工程編譯生成的檔案進行實際應用開發,代碼參照 grpc\examples\cpp\helloworld 目錄下的示例代碼,
1. 撰寫proto檔案
創建example.proto檔案如下圖:
syntax = "proto3";
package pandora;
// The greeting service definition.
service GreeterEx {
// Sends a greeting
rpc Say (Request) returns (Reply) {}
}
// The request message
message Request {
string content = 1;
}
// The response message containing the greetings
message Reply {
string message = 1;
}
2. 生成pb檔案
拷貝 C:\Program Files\grpc\bin\protoc.exe 和 grpc\build_x64\Debug\grpc_cpp_plugin.exe 到相同目錄下,

可以看到,在output目錄下生成了4個pb檔案,
3. 創建gRPC工程
在VS創建C++空專案 grpc_svr 和 grpc_cli ,因為我們生成的gRPC為x64,所以工程也需選擇x64,
grpc_svr 的 main.cpp
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include "example.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using pandora::GreeterEx;
using pandora::Reply;
using pandora::Request;
class GreeterServiceImpl final : public GreeterEx::Service {
Status Say(ServerContext* context, const Request* request,
Reply* reply) override {
reply->set_message(request->content() + "world");
return Status::OK;
}
};
void RunServer() {
std::string server_address("localhost:50000");
GreeterServiceImpl service;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
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;
}
grpc_cli 的 main.cpp
#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 pandora::GreeterEx;
using pandora::Reply;
using pandora::Request;
class GreeterClient {
public:
GreeterClient(std::shared_ptr<Channel> channel)
: stub_(GreeterEx::NewStub(channel)) {}
std::string Say(const std::string& content) {
Request request;
request.set_content(content);
Reply reply;
ClientContext context;
// The actual RPC.
Status status = stub_->Say(&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<GreeterEx::Stub> stub_;
};
int main(int argc, char** argv) {
GreeterClient greeter(
grpc::CreateChannel("localhost:50000", grpc::InsecureChannelCredentials()));
std::string reply = greeter.Say("hello");
std::cout << "Greeter received: " << reply << std::endl;
char c = 0;
std::cin >> c;
return 0;
}
首先,添加4個pb檔案到兩個專案中;
其次,添加參考的grpc檔案及lib庫到工程中:



最后,生成工程,
報錯1:

解決:

將 grpc\third_party\abseil-cpp 的目錄添加進去,
啟動運行,彈出錯誤:

解決:
在 C:\Program Files\grpc\bin 下找到zlibd.dll,拷貝至exe同目錄下,
再次運行,客戶端收到服務端的reply資訊!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/279865.html
標籤:區塊鏈
下一篇:FIL算力挖礦系統開發搭建原理
