接着前一篇《gRPC C++ windows程序实践(一)_永远的魔术1号-CSDN博客》这篇文章主要介绍如何基于gRPC工程编译生成的文件进行实际应用开发,代码参照 grpcexamplescpphelloworld 目录下的示例代码。
1. 编写proto文件
创建example.proto文件如下图:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19syntax = "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 Filesgrpcbinprotoc.exe 和 grpcbuild_x64Debuggrpc_cpp_plugin.exe 到相同目录下。
可以看到,在output目录下生成了4个pb文件。
3. 创建gRPC工程
在VS创建C++空项目 grpc_svr 和 grpc_cli ,因为我们生成的gRPC为x64,所以工程也需选择x64。
grpc_svr 的 main.cpp
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57#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:
解决:
将 grpcthird_partyabseil-cpp 的目录添加进去。
启动运行,弹出错误:
解决:
在 C:Program Filesgrpcbin 下找到zlibd.dll,拷贝至exe同目录下。
再次运行,客户端收到服务端的reply信息!
最后
以上就是迷人裙子最近收集整理的关于gRPC C++ windows程序实践(二)1. 编写proto文件2. 生成pb文件3. 创建gRPC工程的全部内容,更多相关gRPC内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复