grpc - use golang to take you through a set of RPC services

Next, PHP is used as the client to call the server of golang.

Install the grpc go plugin

The grpc PHP plugin can help us to automatically generate the client stub class library (equivalent to the API file), which is convenient for us to introduce and call directly. Otherwise, it is not convenient to only generate the entity class of service / request / response.

# Download grpc library to local
cd ~ && git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
# Update submodule dependencies
cd grpc && git submodule update --init
# Here we only compile php plug-ins if we want to compile all make & & make install
make grpc_php_plugin
# Plug-in path
ll ./bins/opt/grpc_php_plugin

Generate PHP client

If you want to use it with the best language PHP in the world (PHP can only be used as C-side), you only need to install grpc and protobuf extension to generate PHP version library.

###Generate php Client Library

#There will be no client stub class
protoc -I. --php_out=plugins=grpc:./user user.proto

# There will be a client stub class
protoc -I. \
--php_out=./user \
--grpc_out=./user \
--plugin=protoc-gen-grpc=/root/grpc/bins/opt/grpc_php_plugin \
user.proto

# View the generated service class library
[root@localhost grpc]# tree user
user
├── GPBMetadata
│   └── User.php
├── User
│   ├── UserClient.php
│   ├── UserDeleteRequest.php
│   ├── UserDeleteResponse.php
│   ├── UserEntity.php
│   ├── UserIndexRequest.php
│   ├── UserIndexResponse.php
│   ├── UserPostRequest.php
│   ├── UserPostResponse.php
│   ├── UserViewRequest.php
│   └── UserViewResponse.php
└── user.pb.go

PHP GRPC extension and dependent installation

#Installation extension
pecl install grpc
pecl install protobuf

Use composer to manage dependency loading.

mkdir grpc-php-client && cd grpc-php-client
# Using composer to manage projects
composer init

# Client library files for installing grpc/protobuf
composer require grpc/grpc
composer require google/protobuf

# Create a new grpc service Library Directory and move the generated PHP client files here
mkdir grpc && mv $GOPATH/src/grpc/user/* ./grpc
# Register the client file of psr4 auto load service
vi composer.json
{
    "name": "root/php-client",
    "require": {
        "grpc/grpc": "^1.19",
        "google/protobuf": "^3.7"
    },
    "autoload": {
        "psr-4": {
            "User\\": "./grpc/User/",
            "GPBMetadata\\": "./grpc/GPBMetadata/"
        }
    }

}
# Update composer loader
composer dump-autoload

PHP client code instance

After installing the grpc extension and dependency Library of php, we can write the code.

<?php

require_once __DIR__ . '/vendor/autoload.php';

use User\UserClient;
use User\UserEntity;
use User\UserIndexRequest;
use User\UserIndexResponse;
use User\UserViewRequest;
use User\UserViewResponse;
use User\UserPostRequest;
use User\UserPostResponse;
use User\UserDeleteRequest;
use User\UserDeleteResponse;

// Create client instance
$userClient = new UserClient('10.10.31.211:50051', [
    'credentials' => Grpc\ChannelCredentials::createInsecure()
]);


$userIndexRequest = new UserIndexRequest();
$userIndexRequest->setPage(1);
$userIndexRequest->setPageSize(12);

// Send request returns an array [response object corresponding to service interface, request status object]
list($userIndexResponse, $statusObj) = $userClient->UserIndex($userIndexRequest)->wait();

if (0 != $statusObj->code) {
    throw new \Exception($statusObj->details, $statusObj->code);
}

echo printf("index request end: err %d msg %s" . PHP_EOL, $userIndexResponse->getErr(), $userIndexResponse->getMsg());
/* @var [UserEntity, UserEntity] */
$data = $userIndexResponse->getData();
foreach ($data as $row) {
    echo $row->getName(), $row->getAge() . PHP_EOL;
}

// The rest is simple
// $userClient->UserView();
// $userClient->UserPost();
// $userClient->UserDelete();

Go server and client

Keywords: PHP git Google curl

Added by starphp on Sat, 09 Nov 2019 00:01:02 +0200