gRPC是可以在任何环境中运行的现代开源高性能RPC框架。它可以通过可插拔的支持来有效地连接数据中心内和跨数据中心的服务,以实现负载平衡,跟踪,运行状况检查和身份验证。它也适用于分布式计算的最后一英里,以将设备,移动应用程序和浏览器连接到后端服务。
安装protocol buffer 编译器 mac:
其他系统可以尝试编译安装
安装gprc go get -u google.golang.org/grpc
安装protoc-gen-go插件 go get -u github.com/golang/protobuf/protoc-gen-go
使用 新建hello目录,进入后执行:
protoc --proto_path hello/ --go_out=plugins=grpc:hello hello.proto
会看到hello目录下生成了hello.pb.go文件。
当然,其中的 hello.proto
是预先自定义在hello文件夹下的,如:
syntax = "proto3"; //语法声明 package hello; //包名 // 定义服务 service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } // 请求数据格式 message HelloRequest { string name = 1; } // 响应数据格式 message HelloReply { string message = 1; }
server 新建server目录, golang例子代码来自:https://github.com/grpc/grpc-go/tree/master/examples/helloworld
// main.go package main import ( "context" "log" "net" "google.golang.org/grpc" pb "local.com/sai/game/grpc/hello" ) const ( port = ":50051" ) // server is used to implement helloworld.GreeterServer. type server struct { pb.UnimplementedGreeterServer } // SayHello implements helloworld.GreeterServer func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { log.Printf("Received: %v", in.GetName()) return &pb.HelloReply{Message: "Hello " + in.GetName()}, 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{}) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } }
client go client // client.go package main import ( "context" "log" "os" "time" "google.golang.org/grpc" pb "local.com/sai/game/grpc/hello" ) const ( address = "127.0.0.1:50051" defaultName = "puresai" ) func main() { // Set up a connection to the server. conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) // Contact the server and print out its response. name := defaultName if len(os.Args) > 1 { name = os.Args[1] } ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.GetMessage()) }`
php client 扩展安装
下载安装合适版本的扩展即可,记得别忘记在php.ini中加入:
extension=grpc.so extension=protobuf.so
自动生成代码 protoc --php_out=client hello/hello.proto
会看到client目录下生成了GPBMetadata和Hello两个目录。
如果你对grpc相对较熟练,可以直接进行代码编写:
<?php require __DIR__ . '/vendor/autoload.php'; class Client extends \Grpc\BaseStub{ public function __construct($hostname, $opts, $channel = null) { parent::__construct($hostname, $opts, $channel); } /** * rpc SayHello(HelloRequest) returns (HelloReply) {} * 方法名尽量和 (gprc 定义 Greeter 服务)的方法一样 * 用于请求和响应该服务 */ public function SayHello(\Hello\HelloRequest $argument){ // (/hello.Greeter/SayHello) 是请求服务端那个服务和方法,基本和 proto 文件定义一样 return $this->_simpleRequest('/hello.Greeter/SayHello', $argument, ['\Hello\HelloReply', 'decode'] ); } } //用于连接 服务端 $client = new \Client('127.0.0.1:50051', [ 'credentials' => Grpc\ChannelCredentials::createInsecure() ]); //实例化 TestRequest 请求类 $request = new \Hello\HelloRequest(); $request->setName("fairy"); //调用远程服务 $get = $client->SayHello($request)->wait(); //返回数组 //$reply 是 TestReply 对象 //$status 是数组 list($reply, $status) = $get; echo $reply->getMessage().PHP_EOL; // print_r($client->SayHello($request));
当然,也可以使用grpc_php_plugin插件生成。
grpc_php_plugin插件 clone太慢可以使用码云
git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc cd grpc #这一步很慢,暂未找到什么好方法 git submodule update --init make grpc_php_plugin
新建php-client,再来自动生成:
protoc -I=./hello hello.proto --php_out=./php-client/ --grpc_out=php-client/ --plugin=protoc-gen-grpc=/Users/wangzetao/www/grpc1/bins/opt/grpc_php_plugin
会发现比上面自动生成多了一个GreeterClient.php
<?php // client.php require __DIR__ . '/vendor/autoload.php'; //用于连接 服务端 $client = new \Hello\GreeterClient('127.0.0.1:50051', [ 'credentials' => Grpc\ChannelCredentials::createInsecure() ]); //实例化 TestRequest 请求类 $request = new \Hello\HelloRequest(); $request->setName("world"); //调用远程服务 $get = $client->SayHello($request)->wait(); //返回数组 //$status 是数组 list($reply, $status) = $get; echo $reply->getMessage().PHP_EOL; // print_r($client->SayHello($request));
运行测试
go run grpc/server/main.go
go run grpc/client/main.go
go run grpc/client/client.php
go run grpc/client/php-client.php
grpc初体验完成了,本次只是小小的使用了一下子,后续感兴趣的话可以深入学习一下。文中如有错误,欢迎指出交流。
技术文章也发布在自己的公众号【爱好历史的程序员】,欢迎扫码关注,谢谢!