grpc+protobuf 的C++ service 实例解析
来源:互联网 发布:数据库用来做什么 编辑:程序博客网 时间:2024/05/18 00:51
这篇文章将会简单的描述一下grpc+protobuf 的C++ service的搭建过程,告诉读者在linux系统下怎样实现一个service接口的流程。
一、.proto文件的
实现一个简单的helloworld回显功能,首先需要一个.proto文件,我将它命名为example.proto,文件内容如下:
syntax = "proto3";message SearchRequest{string Request = 1;}message SearchResponse{string Response = 2;}service SearchService { rpc Search (SearchRequest) returns (SearchResponse);}
二、自动生成代码
使用example.proto文件自动生成grpc和protobuf的代码
protoc --cpp_out=./ examples.protoprotoc --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin examples.proto这两个命令将会生成四个文件,examples.grpc.pb.cc、examples.grpc.pb.h、examples.pb.cc、examples.pb.h。
三、服务器代码
#include <iostream>#include <memory>#include <string>#include <grpc++/grpc++.h>#include <grpc/grpc.h>#include <grpc++/server.h>#include <grpc++/server_builder.h>#include <grpc++/server_context.h>#include "examples.grpc.pb.h"using grpc::Server;using grpc::ServerBuilder;using grpc::ServerContext;using grpc::Status;class SearchRequestImpl final : public SearchService::Service { Status Search(ServerContext* context, const SearchRequest* request, SearchResponse* reply) override { std::string prefix("Hello "); reply->set_response(prefix + request->request()); return Status::OK; }};void RunServer() { std::string server_address("0.0.0.0:50051"); SearchRequestImpl 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;}
四、客户端代码
#include <iostream>#include <memory>#include <string>#include <grpc++/grpc++.h>#include <grpc/support/log.h>#include "examples.grpc.pb.h"using grpc::Channel;using grpc::ClientAsyncResponseReader;using grpc::ClientContext;using grpc::CompletionQueue;using grpc::Status;class ExampleClient { public: explicit ExampleClient(std::shared_ptr<Channel> channel) : stub_(SearchService::NewStub(channel)) {} std::string Search(const std::string& user) { SearchRequest request; request.set_request(user); SearchResponse reply; ClientContext context; CompletionQueue cq; Status status; std::unique_ptr<ClientAsyncResponseReader<SearchResponse> > rpc( stub_->AsyncSearch(&context, request, &cq)); rpc->Finish(&reply, &status, (void*)1); void* got_tag; bool ok = false; GPR_ASSERT(cq.Next(&got_tag, &ok)); GPR_ASSERT(got_tag == (void*)1); GPR_ASSERT(ok); if (status.ok()) { return reply.response(); } else { return "RPC failed"; } } private: std::unique_ptr<SearchService::Stub> stub_;};int main(int argc, char** argv) { ExampleClient client(grpc::CreateChannel( "localhost:50051", grpc::InsecureChannelCredentials())); std::string user("world"); std::string reply = client.Search(user); // The actual RPC call! std::cout << "client received: " << reply << std::endl; return 0;}
五、Makefile文件
subdir = ./export PKG_CONFIG_PATH=/usr/local/lib/pkgconfigSOURCES = $(wildcard $(subdir)*.cc)SRCOBJS = $(patsubst %.cc,%.o,$(SOURCES))CC = g++%.o:%.cc$(CC) -std=c++11 -I/usr/local/include -pthread -c $< -o $@all: client serverclient:examples.grpc.pb.o examples.pb.o examples_client.o$(CC) $^ -L/usr/local/lib `pkg-config --libs grpc++ grpc` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -lprotobuf -lpthread -ldl -lssl -o $@server:examples.grpc.pb.o examples.pb.o examples_server.o$(CC) $^ -L/usr/local/lib `pkg-config --libs grpc++ grpc` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -lprotobuf -lpthread -ldl -lssl -o $@#chmod 777 $@clean:sudo rm *.o
运行./server启动service,在另一个端口运行./client 打印出:client received: Hello world表示两边已通,grpc+protobuf 搭建完成。
0 0
- grpc+protobuf 的C++ service 实例解析
- Protobuf和GRPC(一)
- protobuf+grpc+examples
- 基于HTTP/2和protobuf的RPC框架GRPC
- ProtoBuf 与 gRPC 你需要知道的知识
- ProtoBuf 与 gRPC 你需要知道的知识
- ProtoBuf 与 gRPC 你需要知道的知识
- Go(GoLang)配置Grpc+ProtoBuf所需的一些资源
- 优秀的protobuf实例
- ProtoBuf的实例
- protobuf + grpc 使用入门 一
- protobuf + grpc 使用入门 二
- gRPC Service Config
- gRPC:Google开源的基于HTTP/2和ProtoBuf的通用RPC框架
- 谷歌发布的首款基于HTTP/2和protobuf的RPC框架:GRPC
- 如何解析超长的protobuf
- galang 学习之grpc+ protobuf(一)
- galang 学习之grpc+ protobuf(二)
- 执行后台耗时操作的封装-----改进版
- You must use a newer version of the Android Gradle plugin. The minimum supported version is 0.12.0 a
- maven 将依赖jar包打包
- cstring和string的区别
- 多线程-9、dispatch源
- grpc+protobuf 的C++ service 实例解析
- 极光推送 使用实例 (一)服务端
- 解决Ubuntu nat模式下网络无法连接的问题
- EasyUI输入框输入纯数字
- 获取wifi和基站定位的基础信息
- html表单
- OSG-VS2015编译第三方库
- Linux网卡能接收不能发送bug eth0 tx packets:0
- 微信公众平台后台数据如何分析