grpc c++语言hello world
2021-03-08 本文已影响0人
一路向后
1.定义proto
syntax = "proto3";
option java_package = "io.grpc.examples";
package helloworld;
//问候服务定义
service Greeter {
//发送消息
rpc SayHello(HelloRequest) returns (HelloReply){}
}
//请求消息包含name
message HelloRequest {
string name = 1;
}
//响应消息包含message
message HelloReply {
string message = 1;
}
2.生成helloworld.pb.h和helloworld.pb.cc
$ protoc --cpp_out=. helloworld.proto
3.生成helloworld.grpc.pb.h和helloworld.grpc.pb.cc
$ protoc --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin helloworld.proto
4.服务端代码server.c
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::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;
}
5.客户端代码client.c
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
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;
//客户端RPC上下文
ClientContext context;
//调用rpc函数
Status status = stub_->SayHello(&context, request, &reply);
//返回结果处理
if(status.ok())
{
return reply.message();
}
else
{
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
return "RPC failed";
}
}
private:
std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char** argv)
{
GreeterClient greeter(grpc::CreateChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials()));
std::string user("world");
std::string reply = greeter.SayHello(user);
std::cout << "Greeter received: " << reply << std::endl;
return 0;
}
6.编译服务端及客户端
$ g++ -std=c++11 -o server server.c helloworld.pb.cc helloworld.grpc.pb.cc -I/usr/local/include -L/usr/local/lib -lgrpc++ -lprotobuf
$ g++ -std=c++11 -o client client.c helloworld.pb.cc helloworld.grpc.pb.cc -I/usr/local/include -L/usr/local/lib -lgrpc++ -lprotobuf
7.运行服务端及客户端
$ ./server
Server listening on 0.0.0.0:50051
$ ./client
Greeter received: Hello world