我是靠谱客的博主 受伤春天,最近开发中收集的这篇文章主要介绍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 $@

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

最后

以上就是受伤春天为你收集整理的grpc安装与简单demo测试的全部内容,希望文章能够帮你解决grpc安装与简单demo测试所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部