grpc安装

10 篇文章 0 订阅

本文转载自: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; then
21   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.zip
23   #unzip -q gtest-1.7.0.zip
24   #rm gtest-1.7.0.zip
25   #mv gtest-1.7.0 gtest
26   git clone https://github.com/tangmi360/googletest.git gtest
27 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程序。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值