Say first environment

  • MacOS 10.16
  • brew
  • PHP 7.4 +

preface

It is difficult to find a suitable RPC framework relative to PHP to study microservice governance. Using curl or guzzle to request domain names from each other is insane. Since most services are in PHP, there is no resident daemon in the background to send heartbeat. Therefore, the selection of RPC is also a very serious problem. I have read yar, Dubbo and other agreements, but they are not ideal. Yar itself is an extension to PHP. But there is no support for service governance, or just a tool for remote procedure calls. Dubbo mainly supports Java, while other languages support it, but it is essentially the same for PHP as Curl. Today, record the process of PHP + GRPC

The installation

Official documentation: grpc. IO /docs/ Langua… Support PHP server and client. Follow the documentation as follows:

$ pecl install protobuf
$ pecl install grpc
Copy the code

The above protobuf is the protocol used by GRPC communication. Like thrift, a cross-language structure definition tool? (blind)

Once installed, follow the documentation to install a PHP-Protoc plug-in that generates PHP-specific classes and services

$ git clonehttps://github.com/grpc/grpc - b v1.33.2 $cd grpc
$ git submodule update --init
$ make grpc_php_plugin
Copy the code

The first installation has been reported

Can't exec "aclocal": No to the file or directory at/usr/local/Cellar/autoconf / 2.69 / share/autoconf/Autom4te/FileUtils PM line 326.Copy the code

And it turns out automake wasn’t installed. All right.

$ brew install automake
$ brew install protobuf # I accidentally installed this, I don't know if it will affect the future
Copy the code

Run the preceding command again. The compilation and installation are complete.

First Example

Continue with the documentation and execute the submoudle in the codebase:

$ cd examples/php
$ ./greeter_proto_gen.sh
Copy the code

This should generate the predefined PHP file, directory structure is as follows

I @ wdeMacBook - Pro: ~ / Code/GRPC - try/GRPC/examples/PHP | ee5b762f33 ⚡ ⇒ ll total 40 DRWXR xr - 3 x 16 11:11 I staff 96 11 b GPBMetadata drwxr-xr-x 5 I staff 160B 11 16 11:11 Helloworld -rw-r--r-- 1 I staff 1.0k 11 16 10:42 readme. md-rw-r --r-- 1 i staff 348B 11 16 10:42 composer.json drwxr-xr-x 11 i staff 352B 11 16 10:42echo
-rw-r--r--   1 i  staff   1.5K 11 16 10:42 greeter_client.php
-rwxr-xr-x   1 i  staff   728B 11 16 10:42 greeter_proto_gen.sh
drwxr-xr-x   6 i  staff   192B 11 16 10:42 route_guide
-rwxr-xr-x   1 i  staff   691B 11 16 10:42 run_greeter_client.sh
Copy the code

Then execute Composer Install to install the dependencies.

$ cd ../node
Copy the code

The tutorial uses Node as a server.

$ npm install 
Copy the code

First installation error, ok. Error: The gRPC binary module was not installed. This may be fixed by running “NPM rebuild” I tm. After various NPM install, it is complete. then

$ cd dynamic_codegen
$ node greeter_server.js
Copy the code

Visual inspection already up, no prompt to open another window, execute

$ ./run_greeter_client.sh
PHP Warning:  PHP Startup: Unable to load dynamic library 'grpc.so' (tried: /usr/local/lib/php/pecl/20190902/grpc.so (dlopen(/usr/local/lib/php/pecl/20190902/grpc.so, 9): image not found), /usr/local/lib/php/pecl/20190902/grpc.so.so (dlopen(/usr/local/lib/php/pecl/20190902/grpc.so.so, 9): image not found)) in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library 'grpc.so' (tried: /usr/local/lib/php/pecl/20190902/grpc.so (dlopen(/usr/local/lib/php/pecl/20190902/grpc.so, 9): image not found), /usr/local/lib/php/pecl/20190902/grpc.so.so (dlopen(/usr/local/lib/php/pecl/20190902/grpc.so.so, 9): image not found)) in Unknown on line 0
PHP Fatal error:  Uncaught Error: Class 'Grpc\ChannelCredentials' not found in /Users/i/Code/grpc-try/grpc/examples/php/greeter_client.php:30
Stack trace:
#0 /Users/i/Code/grpc-try/grpc/examples/php/greeter_client.php(44): greet('localhost:50051', 'world')
#1 {main}
  thrown in /Users/i/Code/grpc-try/grpc/examples/php/greeter_client.php on line 30

Fatal error: Uncaught Error: Class 'Grpc\ChannelCredentials' not found in /Users/i/Code/grpc-try/grpc/examples/php/greeter_client.php:30
Stack trace:
#0 /Users/i/Code/grpc-try/grpc/examples/php/greeter_client.php(44): greet('localhost:50051', 'world')
#1 {main}
  thrown in /Users/i/Code/grpc-try/grpc/examples/php/greeter_client.php on line 30
Copy the code

. 29.. Who does the official website say “congratulation”. Php-m did not see my GRPC. Let’s try it again… Long compilation journey this time saw did I just installed a lonely this time execution

$ ./run_greeter_client.sh
PHP Warning:  Module 'grpc' already loaded in Unknown on line 0

Warning: Module 'grpc' already loaded in Unknown on line 0
Hello world
Copy the code

OK, this is seeing the return of the server. (BTW, I added extension to php.ini when installing PHP. I just want to delete it again. The reason is the statement executed in the script, see for yourself.

Take a look at the code

Vscode opens the PHP directory, HelloWorld and GPBMetadata are both generated by protobuf. The proto file is in the protos upper directory and looks like this:

Licensed under the Apache License, Version 2.0 (the "License"); // Copyright 2015 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // HTTP: / / http://www.apache.org/licenses/LICENSE-2.0 / / / / Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. syntax = "proto3"; option java_multiple_files = true; option java_package = "io.grpc.examples.helloworld"; option java_outer_classname = "HelloWorldProto"; option objc_class_prefix = "HLW"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }Copy the code

The package name helloWorld is defined, the service corresponds to the PHP namespace, and the message corresponds to the structure. That corresponds to PHP’s HelloWorld directory. It can be loaded using composer or require_once. Look at the function


      

require dirname(__FILE__).'/vendor/autoload.php';

function greet($hostname.$name)
{
    $client = new Helloworld\GreeterClient($hostname['credentials' => Grpc\ChannelCredentials::createInsecure(),
    ]);
    $request = new Helloworld\HelloRequest();
    $request->setName($name);
    list($response.$status) = $client->SayHello($request)->wait();
    if ($status->code ! == Grpc\STATUS_OK) {echo "ERROR: " . $status->code . "," . $status->details . PHP_EOL;
        exit(1);
    }
    echo $response->getMessage() . PHP_EOL;
}

$name=!empty($argv[1])?$argv[1] : 'world';
$hostname=!empty($argv[2])?$argv[2] : 'localhost:50051';
greet($hostname.$name);
Copy the code

This can be instantiated directly from the protobuf Client. The corresponding methods and everything are already in there. That’s right. That’s the one we want.

Then we need to consider the erection of the service side, the erection of the service center. Say no more, study again.