grpc之php客户端实现请求golang服务端
- 安装protoc 登陆网站 https://github.com/protocolbuffers/protobuf/releases,下载最新版. 解压到目录 /home/wms/Downloads/,然后添加环境变量.
unzip -q protoc-24.3-linux-x86_64.zip
echo export PATH=$PATH:/home/wms/Downloads/protoc-24.3/bin>>/etc/profile
source /etc/profile
protoc --version
- 安装php
sudo apt-get install -y php8.1-cli php8.1-fpm php-pear php-dev libtool make gcc cmake
- 安装grpc扩展
pecl install grpc
- 安装protobuf扩展
sudo pecl install protobuf-3.24.0
- 安装php生成代码插件
git clone https://github.com/grpc/grpc
cd grpc
git submodule update --init
mkdir -p cmake/build
cd cmake/build
cmake ../..
make protoc grpc_php_plugin
sudo cp grpc_php_plugin /usr/bin/grpc_php_plugin
- 新建项目和proto文件 carrier.proto
syntax = "proto3";
//option java_package = "com.colobu.rpctest";
package carrier;
option go_package = "/";
// The greeting service definition.
service Carrier {
// Sends a greeting
rpc GetCarrier (CarrierRequest) returns (CarrierMsg) {}
rpc GetCarriers (CarriersRequest) returns(CarriersReply){}
}
// The request message containing the user's name.
message CarrierRequest {
string Name = 1;
}
// The response message containing the greetings
message CarriersRequest{
int64 Page = 1;
int64 PageSize = 2;
}
message CarriersReply{
int64 Page = 1;
int64 PageSize = 2;
repeated CarrierMsg CarrierInfo = 3;
}
message CarrierMsg{
int64 Id = 1;
string Name = 2;
}
- 生成代码
sudo protoc --proto_path=protos \
--php_out=greeter \
--grpc_out=greeter \
--plugin=protoc-gen-grpc=/usr/bin/grpc_php_plugin ./protos/greeter.proto
生成文件结构 8. 新建composer.json
{
"name": "ddd/grpc",
"description": "description",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "wms",
"email": "email@example.com"
}
],
"require": {
"grpc/grpc": "^v1.39.0",
"google/protobuf": "^v3.12.2"
},
"autoload": {
"psr-4": {
"GPBMetadata\\": "GPBMetadata/",
"Carrier\\": "Carrier/"
}
}
}
composer install
- 项目根目录新建client.php
<?php
require dirname(__FILE__) . '/vendor/autoload.php';
$client = new Carrier\CarrierClient('192.168.1.12:50051', [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$carrierRequest = new Carrier\CarrierRequest();
$carrierRequest->setName("e邮宝");
$meaData = array(
'accessToken' => array("65794a3163325679546d46745a534936496e647463794973496e426863334e5862334a6b496a6f694d54497a4e445532496e303d")
);
list($reply, $status) = $client->GetCarrier($carrierRequest,$meaData)->wait();
if($status->code === 0){
print_r($reply->getName());
print_r($reply->getId());
print_r($status->code);
print_r(json_encode($reply));
print_r(json_encode($status));
}else{
print_r($status->code);
print_r($status->details);
}
$carriersRequest = new \Carrier\CarriersRequest();
$carriersRequest->setPage(1);
$carriersRequest->setPageSize(10);
list($reply1, $status1) = $client->GetCarriers($carriersRequest,$meaData)->wait();
if($status->code === 0){
print_r($reply1->getPage());
print_r($reply1->getPageSize());
print_r($reply1->getCarrierInfo()[0]->getName());
print_r($status1->code);
print_r(json_encode($reply1));
print_r(json_encode($status1));
}else{
print_r($status1->code);
print_r($status1->details);
}