grpcインストールと簡単なdemoテスト

13021 ワード

grpcインストール:仕事の必要性のため、最近googleのrpcオープンソースフレームワークを勉強しています.私の菜鳥は1枚なので、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   
#include   
#include   

#include   
#include   

#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)  
        : 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 > 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<: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   
#include   
#include   

#include   
#include   
#include   
#include   
#include   

#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(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 $@

server: example.pb.o example.grpc.pb.o server.o
    $(CXX) $^ $(LDFLAGS) -o $@

.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