gRPC study

gRPC主要有4种请求/响应模式,分别是:

(1) 简单模式(Simple RPC)

这种模式最为传统,即客户端发起一次请求,服务端响应一个数据,这和大家平时熟悉的RPC没有什么大的区别,所以不再详细介绍。

(2) 服务端数据流模式(Server-side streaming RPC)

这种模式是客户端发起一次请求,服务端返回一段连续的数据流。典型的例子是客户端向服务端发送一个股票代码,服务端就把该股票的实时数据源源不断的返回给客户端。

(3) 客户端数据流模式(Client-side streaming RPC)

与服务端数据流模式相反,这次是客户端源源不断的向服务端发送数据流,而在发送结束后,由服务端返回一个响应。典型的例子是物联网终端向服务器报送数据。

(4) 双向数据流模式(Bidirectional streaming RPC)

顾名思义,这是客户端和服务端都可以向对方发送数据流,这个时候双方的数据可以同时互相发送,也就是可以实现实时交互。典型的例子是聊天机器人。

use c++

windows

To prepare for cmake + Microsoft Visual C++ compiler build

Install Visual Studio 2015 or 2017 (Visual C++ compiler will be used).
Install Git.
Install CMake.
Install Active State Perl (choco install activeperl) - required by boringssl
Install Go (choco install golang) - required by boringssl
Install yasm and add it to PATH (choco install yasm) - required by boringssl
(Optional) Install Ninja (choco install ninja)

下载指定版本
gRPC

git clone --recursive -b v1.23.0 https://github.com/grpc/grpc

// 如果有子模块失败,使用下面命令更新
git submodule update --init --recursive

// 编译软件

md .build
cd .build
cmake .. -G "Visual Studio 14 2015"
cmake --build . --config Release

生成代码

route_guide.pb.h - 消息类的头文件
route_guide.pb.cc - 其中包含消息类的实现
route_guide.grpc.pb.h - 服务类的头文件
route_guide.grpc.pb.cc - 其中包含服务类的实现

"protoc.exe" -I="D:\project\rpcDemo\rpcDemo" --grpc_out="D:\project\rpcDemo\rpcDemo" --plugin=protoc-gen-grpc="grpc_cpp_plugin.exe" "D:\project\rpcDemo\rpcDemo\\rpc_api.proto"

"D:\\project\\grpc\\.build\\third_party\\protobuf\\Release\\protoc.exe" -I="D:\project\rpcDemo\rpcDemo" --cpp_out="D:\project\rpcDemo\rpcDemo" "D:\project\rpcDemo\rpcDemo\rpc_api.proto"


protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto

protoc –grpc_out=. –plugin=protoc-gen-grpc=grpc_cpp_plugin.exe ./rpc_api.proto

protoc -I ../../protos --cpp_out=. ../../protos/route_guide.proto

rpcClient

// for rpc client
#include <grpcpp/grpcpp.h>

using grpc::Channel;
using grpc::ClientContext;


class rpcClient {
public:
rpcClient(std::shared_ptr<Channel> channel)
: stub_(apiService::apiService::NewStub(channel)) {}

// Assembles the client's payload, sends it and presents the response back
// from the server.
std::string SendMsg(const std::string& msg) {
// Data we are sending to the server.
apiService::Request request;
request.set_value(msg);

// Container for the data we expect from the server.
apiService::Response reply;

// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;

// The actual RPC.
Status status = stub_->apiTrans(&context, request, &reply);

// Act upon its status.
if (status.ok()) {
return reply.value();
}
else {
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "";
}
}

private:
std::unique_ptr<apiService::apiService::Stub> stub_;
};

void rpcClient()
{
rpcClient greeter(grpc::CreateChannel("localhost:53061", grpc::InsecureChannelCredentials()));
std::string user("world");
std::string reply = greeter.SendMsg(user);
std::cout << "Greeter received: " << reply << std::endl;
}

问题

warning C4003: not enough actual parameters for macro ‘min’
包含rpc的头文件要在windows前面

包含库文件

libprotobufd.lib
gpr.lib
grpc.lib
grpc++.lib
Ws2_32.lib
zlibstaticd.lib
caresd.lib
address_sortingd.lib

#include <grpc/grpc.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/security/server_credentials.h>
#undef max
#undef min
#include "rpc_api.grpc.pb.h"
#include "rpc_api.pb.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;

“Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)”

需要在项目设置里面定义 _WIN32_WINNT=0x600 宏