grpc安装

来源:互联网 发布:玩客云官方抢购软件 编辑:程序博客网 时间:2024/06/06 10:04

本文转载自:http://tangmi.me/grpc-experience

grpc安装

1. 下载源码

$ git clone https://github.com/grpc/grpc.git grpc; cd grpc;

2. 更新第三方源码

$ git submodule update --init

注意:执行这一步更新命令前,需要修改.gitmodules文件,我已经通过goog code “一键export to github“ 功能 把gflags项目源码导入到了github(google code无法通过git访问),修改后的文件如下:

[submodule "third_party/zlib"]        path = third_party/zlib        url = https://github.com/madler/zlib[submodule "third_party/openssl"]        path = third_party/openssl        url = https://github.com/openssl/openssl.git        branch = OpenSSL_1_0_2-stable[submodule "third_party/protobuf"]        path = third_party/protobuf        url = https://github.com/google/protobuf.git        branch = v3.0.0-alpha-2[submodule "third_party/gflags"]        path = third_party/gflags        url = https://github.com/tangmi360/gflags.git

3. 编译并安装

$ make$ sudo make install prefix=/usr/local/

到此,grpc已经成功安装在系统中。

protobuf 3.0.0安装

1. 进入third_party目录

$ cd third_party/protobuf

2. 通过autogen.sh脚本生成configure

protobuf从github拉下的源码默认是没有configure文件,需要通过执行autogen.sh来生成
不过需要修改下脚本,修改后脚本片断如下(22-25行被注释掉了,26行是新加)

20 if test ! -e gtest; then21   echo "Google Test not present.  Fetching gtest-1.7.0 from the web..."22   #curl -O https://googletest.googlecode.com/files/gtest-1.7.0.zip23   #unzip -q gtest-1.7.0.zip24   #rm gtest-1.7.0.zip25   #mv gtest-1.7.0 gtest26   git clone https://github.com/tangmi360/googletest.git gtest27 fi

3. 编译并安装

$ ./autogen.sh$ ./configure --prefix=/usr/local$ make$ make check$ sudo make install

4. 添加动态库到ldconf配置

$ sudo vim /etc/ld.so.conf.d/grpc.conf$ 添加一行 "/usr/local/lib"$ sudo ldconfig

Hello C++ gRPC!

1. 下载 grpc-common

该项目是grpc的使用示例程序以及帮助文档

$ git clone https://github.com/grpc/grpc-common.git

2. 生成rpc接口代码

$ cd grpc-common/cpp/helloworld/$ make helloworld.pb.cc

其实生成接口代码执行的是这条命令:

$ protoc -I ../../protos --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/helloworld.proto

3. 代码示例

greeter_server.cc

#include <iostream>#include <memory>#include <string>#include <grpc/grpc.h>#include <grpc++/server.h>#include <grpc++/server_builder.h>#include <grpc++/server_context.h>#include <grpc++/server_credentials.h>#include <grpc++/status.h>#include "helloworld.pb.h"using grpc::Server;using grpc::ServerBuilder;using grpc::ServerContext;using grpc::Status;using helloworld::HelloRequest;using helloworld::HelloReply;using helloworld::Greeter;class GreeterServiceImpl final : public Greeter::Service {    Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {        std::string prefix("Hello ");        reply->set_message(prefix + request->name());        return Status::OK;    }};void RunServer() {    std::string server_address("0.0.0.0:50051");    GreeterServiceImpl 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) {    grpc_init();    RunServer();    grpc_shutdown();    return 0;}

greeter_client.cc

#include <iostream>#include <memory>#include <string>#include <grpc/grpc.h>#include <grpc++/channel_arguments.h>#include <grpc++/channel_interface.h>#include <grpc++/client_context.h>#include <grpc++/create_channel.h>#include <grpc++/credentials.h>#include <grpc++/status.h>#include "helloworld.pb.h"using grpc::ChannelArguments;using grpc::ChannelInterface;using grpc::ClientContext;using grpc::Status;using helloworld::HelloRequest;using helloworld::HelloReply;using helloworld::Greeter;class GreeterClient {  public:    GreeterClient(std::shared_ptr<ChannelInterface> channel) : stub_(Greeter::NewStub(channel)) {}    std::string SayHello(const std::string& user) {        HelloRequest request;        request.set_name(user);        HelloReply reply;        ClientContext context;        Status status = stub_->SayHello(&context, request, &reply);        if (status.IsOk()) {            return reply.message();        } else {            return "Rpc failed";        }    }    void Shutdown() { stub_.reset(); }  private:    std::unique_ptr<Greeter::Stub> stub_;};int main(int argc, char** argv) {    grpc_init();    GreeterClient greeter(    grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(),        ChannelArguments()));    std::string user("world");    std::string reply = greeter.SayHello(user);    std::cout << "Greeter received: " << reply << std::endl;    greeter.Shutdown();    grpc_shutdown();}

4. 编译hello程序

$ make

5. 运行

$ ./greeter_server$ ./greeter_client

输出结果:

Hello world

最后

本文记录了一下安装编译过程,并简单体验了一下c++版本的hello程序。


原创粉丝点击