1. 程式人生 > >grpc安裝與簡單demo測試

grpc安裝與簡單demo測試

grpc安裝:
由於工作需要,最近在學google的rpc開源框架;由於本人菜鳥一枚,而且是從Windows開發轉到Linux;由於之前版本控制使用的是svn,沒有使用過git,所以從GitHub上下載程式碼是通過瀏覽器下載zip格式的壓縮檔案,折騰了好幾天了,發現了各種坑,各種缺少庫,各種不知道怎麼把這個庫給裝機器上。對於一個不熟悉Linux的人來說,這個體驗簡直糟糕透了。沒辦法了又在機器上裝了git,按照下面這個連結從新安裝,但驚喜就這麼發生了,特麼的居然可以安裝上grpc!!!
grpc安裝請參考該連結:https://blog.csdn.net/u012023606/article/details/54584282


其中有個檔案我改了一下,因為之前裝的時候碰到過缺少openssl這個庫

測試程式碼:
測試程式碼參考該連線:https://blog.csdn.net/u012023606/article/details/54583526
上述連結中的Makefile檔案第10行有問題,由於本人菜鳥,不知該怎麼修改,故從githum上下載來的程式碼中的hello例子中拷貝了一個makefile過來並簡單修改了一下。

example.proto:

syntax = "proto3";  

message SearchRequest  
{  
    string Request = 1;  
}  

message
SearchResponse { string Response = 2; } service SearchService { rpc Search (SearchRequest) returns (SearchResponse); }

client.cpp:

#include <iostream>  
#include <memory>  
#include <string>  

#include <grpc++/grpc++.h>  
#include <grpc/support/log.h>  
#include "example.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; }

server.cpp:

#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 "example.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;  
}  

Makefile:

HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
           -lgrpc++_reflection\
           -ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
           -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
           -ldl
endif
PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`

PROTOS_PATH = ./

vpath %.proto $(PROTOS_PATH)

all: client server

client: example.pb.o example.grpc.pb.o client.o
    $(CXX) $^ $(LDFLAGS) -o [email protected]

server: example.pb.o example.grpc.pb.o server.o
    $(CXX) $^ $(LDFLAGS) -o [email protected]

.PRECIOUS: %.grpc.pb.cc
%.grpc.pb.cc: %.proto
    $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<

.PRECIOUS: %.pb.cc
%.pb.cc: %.proto
    $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<

clean:
    rm -f *.o *.pb.cc *.pb.h client server 


# The following is to test your system and ensure a smoother experience.
# They are by no means necessary to actually compile a grpc-enabled software.

PROTOC_CMD = which $(PROTOC)
PROTOC_CHECK_CMD = $(PROTOC) --version | grep -q libprotoc.3
PLUGIN_CHECK_CMD = which $(GRPC_CPP_PLUGIN)
HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false)
ifeq ($(HAS_PROTOC),true)
HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false)
endif
HAS_PLUGIN = $(shell $(PLUGIN_CHECK_CMD) > /dev/null && echo true || echo false)

SYSTEM_OK = false
ifeq ($(HAS_VALID_PROTOC),true)
ifeq ($(HAS_PLUGIN),true)
SYSTEM_OK = true
endif
endif