c++使用grpc生成helloworld服务器程序

130 篇文章 1 订阅
85 篇文章 0 订阅

c++生成helloworld服务器程序

1.定义proto

(详细见:grpc\examples\protos\helloworld.proto)

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}


2. 生成访问代码

将proto.exe、helloworld.proto、grpc_cpp_plugin.exe拷贝到一个文件夹中,grpc_cpp_plugin.exe是gRPC的protoc插件,生成方法参考上文。

创建一个bat文件,包含以下命令:

protoc.exe -I=. --grpc_out=. --plugin=protoc-gen-grpc=.\grpc_cpp_plugin.exe helloworld.proto
protoc.exe -I=. --cpp_out=. helloworld.proto

生成了两套文件

hellowworld.pb.h 声明生成的消息类的头文件
hellowworld.pb.cc  包含消息类的实现
hellowworld.grpc.pb.h 声明你生成的服务类的头文件
hellowworld.grpc.pb.cc 包含服务类的实现

其中前两个是protoc生成的,后两个是插件生成的。

这些包括:

  • 所有的填充,序列化和获取我们请求和响应消息类型的 protocol buffer 代码
  • 名为 Greeter的类,包含
    • 为了客户端去调用定义在 Greeter服务的远程接口类型(或者 存根 )
    • 让服务器去实现的两个抽象接口,同时包括定义在 Greeter中的方法。

详细见:https://doc.oschina.net/grpc?t=57966

生成服务器端代码

3. 创建C++项目

依照grpc\examples\cpp\helloworld下client,server例子
greeter_async_client.cc
greeter_async_client2.cc
greeter_async_server.cc
greeter_client.cc
greeter_server.cc

4. 设置头文件

将刚刚生成的文件放入工程源代码目录$(SolutionDir)并包含进工程
GrpcTest\GrpcTest\src\protobuf
将:grpc\include,grpc\third_party\protobuf\src中的文件拷贝到include目录(自己的库目录)
三个头文件目录,一个是刚生成的的访问类路径,一个是gRPC的头文件,一个是protobuf的头文件.

5. 设置库

将:
grpc\third_party\protobuf\cmake\Release;
grpc\vsprojects\Release;
grpc\third_party\zlib\solution\Release;
grpc\vsprojects\packages\grpc.dependencies.openssl.1.0.204.1\build\native\lib\v120\Win32\Release\static

放入lib目录(自己的lib库目录)

四个库文件路径:protobuf库路径、grpc库路径、zlib库路径、openssl库路径。
没有使用boringssl,openssl的库是从NuGet下载的package中找到的。

库文件

libprotobuf.lib;
grpc.lib;
gpr.lib;
grpc++.lib;
Ws2_32.lib;

6. 编译C++项目

将gRPC的C++ example的代码拷贝到我们刚创建的项目中,编译,出现一些error:

错误A:

error C1189: #error :"Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"port_platform.h 59 Server_Cpp

解决:在项目属性中的Preprocessor Definitions中添加_WIN32_WINNT=0x600

错误B:

error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check ****

解决:在项目属性中的Preprocessor Definitions中添加

_SCL_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS

错误C:

error LNK2038: mismatch detected for 'RuntimeLibrary': value

解决:只需要主程序和静态库都采用同一种Runtime Libray编译即可。
在项目属性C/C++中的 代码生成 的 运行库 选择 Multi-threaded(/MT)


OpenSSl我是添加的NuGet库来解决的

点击项目右键=>管理NuGet程序包

点击浏览=>输入grpc.dependencies.openssl

找到grpc.dependencies.openssl =>点击安装(我是用的v1.0.204.1版本)

完成,

编辑通过

c++生成helloworld client程序


依照

c++生成helloworld服务器程序

流程,


我使用的
greeter_client.cc
greeter_server.cc
两个测试来做的测试


说明:

附上一个编译好的版本,里面带了测试程序(helloworld)

http://download.csdn.net/detail/xie1xiao1jun/9630779

点击打开链接

好的,以下是一个简单的 C++ gRPC 通信程序,并使用 CMake 进行编译: 首先,需要安装 gRPC 和 Protobuf。 接着,创建一个名为 `hello.proto` 的 Protobuf 文件,内容如下: ```protobuf syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } ``` 然后,在命令行中进入到包含 `hello.proto` 文件的目录,并执行以下命令: ```bash $ grpc_cpp_plugin --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` hello.proto ``` 该命令将生成 `hello.grpc.pb.h` 和 `hello.grpc.pb.cc` 文件。 接着,创建一个名为 `server.cpp` 的服务器端代码,内容如下: ```cpp #include <iostream> #include <memory> #include <string> #include <grpc++/grpc++.h> #include "hello.grpc.pb.h" using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using helloworld::Greeter; using helloworld::HelloRequest; using helloworld::HelloReply; // 实现 Greeter 服务类 class GreeterServiceImpl final : public Greeter::Service { Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { std::string prefix("Hello "); reply->set_message(prefix + request->name()); return Status::OK; } }; // 启动服务器 void RunServer() { std::string server_address("0.0.0.0:50051"); GreeterServiceImpl service; // 构建服务器 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; } ``` 该代码将创建一个 `GreeterServiceImpl` 类,实现 `Greeter` 服务类,然后启动一个 gRPC 服务器,监听在 `0.0.0.0:50051` 地址。 接着,创建一个名为 `client.cpp` 的客户端代码,内容如下: ```cpp #include <iostream> #include <memory> #include <string> #include <grpc++/grpc++.h> #include "hello.grpc.pb.h" using grpc::Channel; using grpc::ClientContext; using grpc::Status; using helloworld::Greeter; using helloworld::HelloRequest; using helloworld::HelloReply; class GreeterClient { public: GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} std::string SayHello(const std::string& user) { HelloRequest request; request.set_name(user); HelloReply reply; ClientContext context; // 调用远程服务 Status status = stub_->SayHello(&context, request, &reply); if (status.ok()) { return reply.message(); } else { return "RPC failed"; } } private: std::unique_ptr<Greeter::Stub> stub_; }; int main(int argc, char** argv) { GreeterClient greeter(grpc::CreateChannel( "localhost:50051", grpc::InsecureChannelCredentials())); std::string user("world"); std::string reply = greeter.SayHello(user); std::cout << "Greeter received: " << reply << std::endl; return 0; } ``` 该代码将创建一个 `GreeterClient` 类,用于调用远程服务。它将连接到 `localhost:50051` 地址,并调用 `SayHello` 方法,将 `world` 作为参数传递给远程服务。 最后,创建一个名为 `CMakeLists.txt` 的 CMake 文件,内容如下: ```cmake cmake_minimum_required(VERSION 3.0.0) project(grpc-example VERSION 0.1.0) set(CMAKE_CXX_STANDARD 11) add_executable(server server.cpp hello.grpc.pb.cc hello.pb.cc) add_executable(client client.cpp hello.grpc.pb.cc hello.pb.cc) find_package(Protobuf REQUIRED) find_package(gRPC REQUIRED) target_include_directories(server PUBLIC ${Protobuf_INCLUDE_DIRS}) target_link_libraries(server ${Protobuf_LIBRARIES} gRPC::grpc++) target_include_directories(client PUBLIC ${Protobuf_INCLUDE_DIRS}) target_link_libraries(client ${Protobuf_LIBRARIES} gRPC::grpc++) ``` 该文件将编译 `server.cpp` 和 `client.cpp`,并链接 Protobuf 和 gRPC 库。 在命令行中进入到包含 `CMakeLists.txt` 文件的目录,并执行以下命令: ```bash $ mkdir build $ cd build $ cmake .. $ make ``` 该命令将编译并链接代码,生成 `server` 和 `client` 可执行文件。 现在,可以在命令行中运行 `./server` 启动服务器,然后在另一个终端窗口中运行 `./client` 调用远程服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值