grpc简易教程 go server+php client

来源:互联网 发布:网络变压器品牌 编辑:程序博客网 时间:2024/06/07 08:39

GRPC是google开源的一个高性能、跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,支持多种开发语言。
对于开发者而言:
1)需要使用protobuf定义接口,即.proto文件
2)然后使用compile工具生成特定语言的执行代码,比如JAVA、C/C++、Python等。类似于thrift,为了解决跨语言问题。
3)启动一个Server端,server端通过侦听指定的port,来等待Client链接请求。

本文以golang和php为例展开教程,使用golang做rpc server,php做rpc client,实现跨语言调用。

前期准备

golang的grpc包

golang的rpc包,作为golang rpc server需要这个包

go get google.golang.org/grpc

php的grpc扩展

下载地址: http://pecl.php.net/package/gRPC
安装过程:

phpize./configure --with-php-config=/usr/local/php/bin/php-configmakemake install# 添加grpc.so到php.ini配置vim /usr/local/php/etc/php.iniextension = "grpc.so"php -m | grep "grpc"grpc

protoc

命令行下将proto协议文件转换为多种语言对应格式的工具

下载地址:https://github.com/google/protobuf
安装

./configuremake make installprotoc --helpUsage: protoc [OPTION] PROTO_FILESParse PROTO_FILES and generate output based on the options given:...

尝试运行

golang的 rpcserver

  • (1). 可直接用go get 的例子
go get google.golang.org/grpc/examples/helloworld#将会编译好greeter_server和greeter_clientgreeter_server#greeter_server启动后挂起...

在另一个命令行运行:

greeter_client "this is my message"2017/08/30 21:55:25 Greeting: Hello this is my message
  • (2). 手动生成go文件运行
    假如我们有一个proto文件helloworld.proto:
syntax = "proto3";option java_multiple_files = true;option java_package = "io.grpc.examples.helloworld";option java_outer_classname = "HelloWorldProto";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 greetingsmessage HelloReply {  string message = 1;}

通过protoc将helloworld.proto转换生成对应的go文件:

protoc --go_out=./ helloworld.proto

得到helloworld.pb.go,不用编辑这个文件
这个文件属于package helloworld,你要适当放好他所在目录。
手动编辑greeter_server.go:

package mainimport (    "log"    "net"    "golang.org/x/net/context"    "google.golang.org/grpc"    pb "/helloworld"    "google.golang.org/grpc/reflection")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}, 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{})    // Register reflection service on gRPC server.    reflection.Register(s)    if err := s.Serve(lis); err != nil {        log.Fatalf("failed to serve: %v", err)    }}

手动编辑greeter_client.go:

package mainimport (    "log"    "os"    "golang.org/x/net/context"    "google.golang.org/grpc"    pb "google.golang.org/grpc/examples/helloworld/helloworld")const (    address     = "localhost:50051"    defaultName = "world")func main() {    // Set up a connection to the server.    conn, err := grpc.Dial(address, grpc.WithInsecure())    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]    }    r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})    if err != nil {        log.Fatalf("could not greet: %v", err)    }    log.Printf("Greeting: %s", r.Message)}

编译运行:

go build greeter_server.go && ./greeter_server &go build greeter_client.go && ./greeter_client "Message from My Build"2017/08/30 22:04:56 Greeting: Hello Message from My Build

php运行grpc client

参考 github.com/grpc/grpc/example/php
将此目录clone下来

git clone github.com/grpc/grpccd grpc/example/phpcurl -sS https://getcomposer.org/installer | phpphp composer.phar install

composer安装后会生成vendor和autoload.php文件,可自行选择添加到自己的项目中。
helloword.pb.php文件可以由protoc自定义生成:

protoc --php_out=./ helloworld.proto

php greeter_client.php编辑内容:

require dirname(__FILE__).'/vendor/autoload.php';// The following includes are needed when using protobuf 3.1.0// and will suppress warnings when using protobuf 3.2.0+include_once dirname(__FILE__).'/helloworld.pb.php';include_once dirname(__FILE__).'/helloworld_grpc_pb.php';function greet($name){    $client = new Helloworld\GreeterClient('127.0.0.1:50051', [        'credentials' => Grpc\ChannelCredentials::createInsecure(),    ]);    $request = new Helloworld\HelloRequest();    $request->setName($name);    list($reply, $status) = $client->SayHello($request)->wait();    $message = $reply->getMessage();    return $message;}$name = !empty($argv[1]) ? $argv[1] : 'world';echo "the greet: ".greet($name)."\n";

运行:

php ./greeter_client.phpthe greet: Hello world

祝大家学习grpc顺利~