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
#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_;
};
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;
}
【ubuntu|grpc安装与简单demo测试】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 serverclient: 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=. $ /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
推荐阅读
- 废材自救记录|虚拟机-无法安装vmware tools的解决方法
- Ubuntu|Ubuntu 3D桌面
- 问题解决(Ubuntu和Windows系统的时间不同步)
- vmware12虚拟机 以及苹果系统、乌班图和红帽 百度网盘下载连接
- 服务器|Mysql开启关闭
- Linux系统管理|ubuntu软件包管理命令的详细用法
- grpc- python调用c++的rpc服务
- QML|如何在QML应用中实现一个Splash画面
- QML|如何在QML应用中启动Scope
- cmake|cmake - 如何生成grpc的*.pb.cc和*.pb.h文件