Mac under GRPC (Golang Server + PHP client) practice

Source: Internet
Author: User
Tags php server php source code

At present, micro-service is so popular, RPC framework is also blossoming, this article describes the Mac under the GRPC development environment, where the server side uses Golang, the client uses PHP.

Service side

Golang GRPC Installation

Here is a reference, because GRPC on GitHub file directory changes, so directly according to the official installation, there will be 404 resources can not be found problems.
You need to install GOLANG+GRPC locally first. For specific reference: Golang installation Grpc
After the operation, run at Terminal: Protoc--version, can see the version information
Meanwhile, the Protoc-gen-go command should also have been installed

View Protoc Version Information

PHP GRPC Client

There are several extensions that are recommended for official use.
Of course, the other one is pretty good, Datto's protobuf extension. Using Datto this time, I use Perl according to its document installation failed, since the official OK, also do not bother to toss again.
The official installation process, just give the GitHub URL on the detailed instructions, according to the steps to go, OK.
After installation, in order to use under Nginx, it should be installed GRPC this extension:

PHP grpc Extension installation successfully viewed

Start Demo

The official gave a Helloworld.proto demo, we look like, add a, directory structure as follows: Demo directory structure

App and Meta, put the PROTOC compiled after the server go to the source code.
GRPC, compile Protoc into PHP required tools and source code (ie: grpc_php_plugin)
Phpclient, is Protoc compiled PHP source code, the following vendor folder is composer loaded grpc/grpc PHP Client Connection server source

On the source

Create folder first: mkdir Grpc_demo

Source Code of Meta.proto

syntax = "proto3";package meta;service Agent {  rpc GetInfo (MetaRequest) returns (MetaResponse);}message MetaRequest {  string appId = 1;}message MetaResponse {  string appId = 1;  string title = 2;  string name = 3;  string logo = 4;}

Compile into Golang file:

protoc --go_out=plugins=grpc:./meta ./meta.proto

Compile into PHP file:

protoc --proto_path=./ \       --php_out=phpClient/meta \       --grpc_out=phpClient/meta \       --plugin=protoc-gen-grpc=./grpc/bins/opt/grpc_php_plugin \       meta.proto

Write the Main.go file: (Here Helloworld.proto the demo is also added)

Package Mainimport ("Log" "Net" PB "Grpc_demo/app" meta "Grpc_demo/meta" "Golang.org/x/net/context" "Go Ogle.golang.org/grpc ") const (port =": 50051 ")//server is used to implement HelloWorld. Greeterserver.type Server struct{}//SayHello implements HelloWorld. Greeterserverfunc (S *server) SayHello (CTX context. Context, in *PB. Hellorequest) (*PB. Helloreply, error) {return &PB. Helloreply{message: "Hello" + in. Name + "--from jacky_chen"}, Nil}func (S *server) GetInfo (CTX context. Context, in *meta. Metarequest) (*meta. Metaresponse, error) {return &meta. Metaresponse {appid:in. AppId, Title: "My title", Name: "My Name", Logo: "Https://xxx.com",}, Nil} func main () {LIS, ERR: = Net. Listen ("TCP", port) if err! = Nil {log. Fatalf ("Failed to listen:%v", err)} s: = Grpc. NewServer () PB. Registergreeterserver (S, &server{}) meta. Registeragentserver (S, &server{}) S.serve (LIS)}

agent_client.php Source:

<?phprequire dirname(__FILE__).'/../vendor/autoload.php';print_r(get_declared_classes());exit;include_once dirname(__FILE__).'/Meta/AgentClient.php';include_once dirname(__FILE__).'/Meta/MetaRequest.php';include_once dirname(__FILE__).'/Meta/MetaResponse.php';include_once dirname(__FILE__).'/GPBMetadata/Meta.php';$appId = "123";try {    $client = new Meta\AgentClient('localhost:50051', [        'credentials' => Grpc\ChannelCredentials::createInsecure(),    ]);    $request = new Meta\MetaRequest();    $request->setAppId($appId);    list($reply, $status) = $client->GetInfo($request)->wait();    echo $reply->serializeToJsonString();} catch (\Throwable | \Error | \Exception $e) {    echo  "error : " . $e->getMessage();}

To run the service side:

go run main.go

To run the client:

Results


PHP calls the Go RPC service

Because of the PHP itself (the official is based on PHP-FPM, very memory-resident), so GRPC officially does not have PHP server. Of course, GRPC, based on the Swoole development, already has a server on GitHub.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.