gRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C#。

gRPC 基于 HTTP/2 标准设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

环境部署:

1、查看centos版本:

2、安装grpc的提前准备(g++, autoconf等)

yum install -y gcc-c++ autoconf libtool
yum groupinstall -y "Development Tools"

3、下载grpc源码和相关子模块

git clone https://github.com/grpc/grpc.git
cd grpc
git submodule update --init

4、编译安装protobuf(grpc需要用到的组件)

网上有很多方法是使用curl的方式直接从github上使用脚本下载,但是新版(libprotoc 3.6.1)好像不支持修改文件autogen.sh,当然这一步完全可以借助third_party/protobuf/README.txt来部署protobuf,这里我采用直接从github上下载的方式部署:

https://github.com/protocolbuffers/protobuf/releases/


直接终端命令:(在目录third_party下)

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz

解压后:

mv protobuf-all-3.6.1 protobuf
cd protobuf
./autogen.sh
./configure
make
make check
make install

如果make check成功,则显示:

上述操作如果出现了错误,可以查看同级目录下的config.log里面的日志。比如gcc的版本过低等问题。

之后我们可以使用命令:

protoc --version

来验证protobuf是否安装成功,如果出现:

command not found

则需要添加环境变量或者将生成的protoc二进制拷贝到/usr/bin/目录下。

5、编译安装grpc

首先在进到如下的目录下:

然后分别执行命令:

 make
make install

6、demo测试

官方自带有个HelloWorld的Demo,可以编译这个Demo看是否我们已经安装成功了。

执行:

make

如果报错:

则需要如下两条语句:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
pkg-config --libs protobuf grpc++ grpc

此时再次make发现:

然后分别启动服务器和客户端

./greeter_server
./greeter_clien

效果:(说明部署成功)

补充:

gcc升级到5.3.1:(centos7.3下)

yum install centos-release-scl

yum install devtoolset-4-toolchain

scl enable devtoolset-4 bash

gcc --version        

上述内容参考:

http://blog.dreamlikes.cn/archives/555

https://www.jianshu.com/p/efc9167e7044

【grpc配置问题汇总】

1、grpc默认是使用动态库,如果使用静态库编译的话,编译能通过,但是运行时,会报错:

[admin@iz2zehztwxfrcdbc0z worker]$ ./worker_server 127.0.0.1: 127.0.0.1:
E0304 ::33.725495889 uri_parser.cc:] bad uri.scheme: '127.0.0.1:21111'
E0304 ::33.725607759 uri_parser.cc:] ^ here
E0304 ::33.725615018 resolver_registry.cc:] don't know how to resolve '127.0.0.1:' or 'dns:///127.0.0.1:21111'
E0304 ::33.725665096 resolver_registry.cc:] don't know how to resolve 'dns:///127.0.0.1:21111' or 'dns:///dns:///127.0.0.1:21111'
E0304 ::33.725687178 channel.cc:] channel stack builder failed: {"created":"@1551679173.725672811","description":"resolver creation failed","file":"src/core/ext/filters/client_channel/resolving_lb_policy.cc","file_line":}
Register fail, error_code: , error_message: lame client channel

参考github上的相似问题:https://github.com/grpc/grpc/issues/11366

即需要自己初始化grpc,直接调用grpc_init()

就我们的helloworld例子来说,代码应该变成:

server端: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 ;
}

client端: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();
}

centos 7下部署grpc的更多相关文章

  1. Centos环境下部署游戏服务器-软件安装

    这篇文章主要介绍一下游戏服务器需要安装的软件和需要修改的配置.现介绍下项目,本项目服务器端是c++ + mysql组合,客户端是as写的,需要安装的服务为Mysql,Php,Apache, 以及一个n ...

  2. Centos环境下部署游戏服务器-自动化

    计算机是没有智力的,只会接受指令,运行指令,得出结果.因此就算你让它重复做一件事多少次,它也学不会自动去做这件事.对于重复繁琐的事情,做为一个懒惰的程序员,必须告诉机器去做这件事情,然后就行了,而不是 ...

  3. Centos 7 下部署集群式阿波罗

    apollo工作原理 用户通过浏览器登录Portal管理界面 >> 通过Admin server对配置进行修改 >> 应用程序主动向config server配置注意:Port ...

  4. Centos环境下部署游戏服务器-常用命令

         图1     在Linux的世界,如果你不玩命令,那你见了同行都不好意思和人家打招呼.同时服务器正常状况下放在远端,一般都是开ssh登录服务器,相信远程桌面的人很少见吧.这篇文章说说Linu ...

  5. Centos环境下部署游戏服务器-iptables

    简介:   图1        Centos做为服务器级操作系统,防火墙是不可缺少的.防火墙的主要功能为控制进出网络包,防火墙就如小区门卫的工作职责,检查出入小区居民的身份,如果不符合小区门卫管理条例 ...

  6. Centos环境下部署游戏服务器-SVN

    版本控制工具的文章已经被写滥了,所以本篇文章不想介绍如何安装Svn如何可视化操作这些东西.本篇文章讲述我自己对Svn的理解,以及在命令行下操作.为啥不应可视化界面?有两方面的原因,远程登录到服务器都是 ...

  7. Centos环境下部署游戏服务器-权限

    部署Web服务器的时候,在"DocumentRoot"指向的根目录新建一个文件夹,然后将网页和资源放在这个文件夹里,通过地址http://192.168.0.100/Res/ind ...

  8. Centos环境下部署游戏服务器-编译

    游戏服务器是在windows环境开发的,相关跨平台的东西在这里不谈了,只谈如何将Visual Studio 工程转换到Linux下编译.这里涉及到的软件分别为:Centos版本为6.4,Visual ...

  9. Centos环境下部署游戏服务器-简介

    一.前言      在接触这个操作系统之前我一直使用的是ubuntu和mac os,这次由于游戏是测试版本,没有专业的运维人员去做这件事情,只能我这个稍微懂一点linux的人来做这件事情了.由于涉及到 ...

随机推荐

  1. centos7 redis配置

    https://www.cnblogs.com/web424/p/6796993.html

  2. React Native多语言

    https://medium.com/@danielsternlicht/adding-localization-i18n-g11n-to-a-react-native-project-with-rt ...

  3. Grafana分析Nginx日志

    配置Groub by -Terms时报错,提示需要设置fielddata=true,报错内容大概如下: "Fielddata is disabled on text fields by de ...

  4. Haskell语言学习笔记(82)Extensible effects

    安装 extensible-effects $ cabal install extensible-effects Installed extensible-effects-3.0.0.0 Extens ...

  5. MTIM(中间人攻击)

    所谓的MITM攻击就是通过拦截正常的网络通信数据,并进行数据篡改和嗅探,而通信的双方却毫不知情. 信息篡改 当主机A.和主机B通信时,都由主机C来为其“转发”,如图一,而A.B之间并没有真正意思上的直 ...

  6. LeetCode OJ 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. PL/SQL编码规范的一些建议

    由于业务的复杂多变,我们编写完的程序,在后期肯定要被修改,而且修改的人很可能不是自己.这种情况我们都遇到过. 而且,看别人的代码可能会觉得很痛苦:为什么他要这样写相关逻辑?为什么变量名称要这样定义?换 ...

  8. app与jvm 反向代理时config的设置(用于在web页面显示npm(就如tomcat)产生的页面)

    dev: { // Various Dev Server settings contentBase: ROOT, host: ip, port: 8084,    //此端口为任意设置,不重复即可,为 ...

  9. JS代码注释

    1.css和js都可以使用/**/进行注释 2.html使用<!---->注释 3.单行js代码可以使用//进行注释 <!DOCTYPE html> <html lang ...

  10. Lucene 全文检索引擎

    Apache Lucene PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程, 仅供各位程序猿们参考,这 ...