lgy514

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
GRPC
 
2.cd examples/cpp/route_guide
3.安装生成服务器和客户端的接口代码相关工具
4.gRPC环境安装
4.1 protobuf(属于4.2里面)
sudo apt-get install autoconf automake libtool curl make g++ unzip
tar -xzvf protobuf-cpp-3.7.1.tar.gz
cd protobuf-3.7.1
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
OK
4.2 GRPC
sudo apt-get install build-essential autoconf libtool pkg-config
sudo apt-get install libgflags-dev libgtest-dev sudo apt-get install clang libc++-dev
$ sudo apt-get install build-essential autoconf libtool curl git $ git clone https://github.com/grpc/grpc.git $ cd grpc $ git submodule update --init
 
cd third_party/protobuf/ $ ./autogen.sh $ ./configure --prefix=/data/bin/protobuf/protobuf-dev $ make -j4 $ sudo make install
cd ../../
CONFIG=dbg make -j4 $ sudo CONFIG=dbg make install
 
5 测试案例
cd examples/cpp/route_guide
make route_guide.grpc.pb.cc route_guide.pb.cc
make
./route_guide_server
./route_guide_client
 
6 编写demo
 
sudo apt-get install libssl-dev
 
proto文件的
 
syntax = "proto3"; message SearchRequest { string Request = 1; } message SearchResponse { string Response = 2; } service SearchService { rpc Search (SearchRequest) returns (SearchResponse); }
 
 
编译命令
2. protoc --cpp_out=./ examples.proto 4. protoc --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin examples.proto
 
examples_server.cc
 
#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; }
 
examples_client.cc
#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/pkgconfig SOURCES = $(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 server client: 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 搭建完成。
posted on 2019-06-18 11:14  lgy514  阅读(155)  评论(0编辑  收藏  举报