接着前一篇《gRPC C++ windows程序实践(一)_永远的魔术1号-CSDN博客》这篇文章主要介绍如何基于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同目录下。