前言
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 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。
本例系统为 CentOS Linux release 7.5.1804 (Core) ,具体实现如下:
安装GO(已安装跳过)
1、安装yum 源
yum install epel -y
2、然后使用 yum 安装 Golang:
yum install go -y
查看版本
go version
#go version go1.9.4 linux/amd64
3、配置环境变量
在 /etc/profile 添加:
export GOPATH=/home/go
export PATH=$PATH:$GOPATH/bin
然后执行 source /etc/profile 使之生效,创建GOPATH目录
mkdir /home/go
安装protobuf
1、安装相关软件
协议编译器是用C ++编写的。如果您使用的是C ++,请按照C ++安装说明在C ++运行时安装protoc。
yum install autoconf automake libtool gcc gcc-c++
2、 下载protobuf,并安装
去到Protocol Buffers下载最新版本(Version3.0.0 beta2),然后解压到本地。
tar -zxvf protobuf-all-3.5.1.tar.gz
cd protobuf-3.5.1
./configure
make
make install
3、查看protobuf 版本
protoc --version
显示 libprotoc 3.5.1 ,证明成功。
4、然后安装golang protobuf直接使用golang的get即可
go get -u github.com/golang/protobuf/proto // golang protobuf 库
go get -u github.com/golang/protobuf/protoc-gen-go //protoc --go_out 工具
安装golang的grpc包
能翻墙的同学执行以下命令的就可以简单实现
go get google.golang.org/grpc
不能翻墙同学也别急,也能实现,解决办法如下。
1、安装grpc 需要的相关的报,不然会报错。
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/net.git --depth 1
git clone https://github.com/golang/text.git --depth 1
cd $GOPATH/src/google.golang.org
git clone https://github.com/google/go-genproto.git genproto --depth 1
2、建立相关目录,进入目录
mkdir -p $GOPATH/src/google.golang.org/
cd $GOPATH/src/google.golang.org
3、从Github上克隆其他的仓库
git clone https://github.com/grpc/grpc-go.git grpc
4、 安装仓库
cd $GOPATH/src/
go install google.golang.org/grpc
安装php的grpc扩展
1、下载地址: http://pecl.php.net/package/gRPC
wget http://pecl.php.net/get/grpc-1.12.0.tgz
tar -zxvf grpc-1.12.0.tgz
cd grpc-1.12.0
phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
# 添加grpc.so到php.ini配置
vim /usr/local/php/etc/php.ini
extension = "grpc.so"
php -m | grep "grpc"
#出现 grpc 证明安装成功
运行 GO 服务端 和GO客户端
1、默认相关实例已存在相关目录
cd $GOPATH/src/google.golang.org/grpc/examples/helloworld/
ll #浏览目录下的文件
drwxr-xr-x 2 root root 21 Jun 7 15:20 greeter_client
drwxr-xr-x 2 root root 21 Jun 7 15:20 greeter_server
drwxr-xr-x 2 root root 54 Jun 7 15:20 helloworld
drwxr-xr-x 2 root root 47 Jun 7 15:20 mock_helloworld
greeter_server 为GO服务端程序
greeter_client 为GO客户端
2、编译,运行GO服务端
cd $GOPATH/src/google.golang.org/grpc/examples/helloworld/greeter_server
go build -o greeter_server main.go
#运行服务端
./greeter_server &
3、编译,运行GO客户端
cd $GOPATH/src/google.golang.org/grpc/examples/helloworld/greeter_client
go build -o greeter_client main.go
#运行GO客户端
./greeter_client
出现如下信息证明成功
2018/06/07 16:43:24 Greeting: Hello world
PHP 客户端
参考 https://github.com/grpc/grpc 下example/php
#打开想要执行的项目目录
cd /var/www
git clone https://github.com/grpc/grpc.git
git submodule update --init
#如果只用php grpc client,此处不用make和make install
#make
#make install
#为了生成grpc client stub code,最好make grpc_php_plugin
make grpc_php_plugin
cd grpc/example/php
curl -sS https://getcomposer.org/installer | php
php composer.phar install
composer安装后会生成vendor和autoload.php文件,可自行选择添加到自己的项目中。
运行 ./greeter_proto_gen.sh
生成两个目录(GPBMetadata,Helloworld),相关PHP文件
新建 greeter_client.php
<?php
require dirname(__FILE__).'/vendor/autoload.php';
// The following includes are needed when using protobuf 3.1.0
// and will suppress warnings when using protobuf 3.2.0+
include_once dirname(__FILE__).'/GPBMetadata/Helloworld.php';
include_once dirname(__FILE__).'/Helloworld/GreeterClient.php';
include_once dirname(__FILE__).'/Helloworld/HelloRequest.php';
include_once dirname(__FILE__).'/Helloworld/HelloReply.php';
function greet($name)
{
$client = new Helloworld\GreeterClient('127.0.0.1:50051', [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$request = new Helloworld\HelloRequest();
$request->setName($name);
list($reply, $status) = $client->SayHello($request)->wait();
$message = $reply->getMessage();
return $message;
}
$name = !empty($argv[1]) ? $argv[1] : 'world';
echo "the greet: ".greet($name)."\n";
运行
php greeter_client.php
结果为:
the greet: Hello world
说明运行成功
参考资料:
官方网站:http://www.grpc.io/
官方文档:http://www.grpc.io/docs/
中文翻译:http://doc.oschina.net/grpc
http://dreamlikes.cn/archives/555