从零开始基于go-thrift创建一个RPC服务 (2)

我们先使用go test写客户端代码:
client_test.go

package main import ( "context" "fmt" "github.com/apache/thrift/lib/go/thrift" "sample/gen-go/Sample" "testing" ) var ctx = context.Background() func GetClient() *Sample.GreeterClient { addr := ":9090" var transport thrift.TTransport var err error transport, err = thrift.NewTSocket(addr) if err != nil { fmt.Println("Error opening socket:", err) } //protocol var protocolFactory thrift.TProtocolFactory protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() //no buffered var transportFactory thrift.TTransportFactory transportFactory = thrift.NewTTransportFactory() transport, err = transportFactory.GetTransport(transport) if err != nil { fmt.Println("error running client:", err) } if err := transport.Open(); err != nil { fmt.Println("error running client:", err) } iprot := protocolFactory.GetProtocol(transport) oprot := protocolFactory.GetProtocol(transport) client := Sample.NewGreeterClient(thrift.NewTStandardClient(iprot, oprot)) return client } //GetUser func TestGetUser(t *testing.T) { client := GetClient() rep, err := client.GetUser(ctx, 100) if err != nil { t.Errorf("thrift err: %v\n", err) } else { t.Logf("Recevied: %v\n", rep) } } //SayHello func TestSayHello(t *testing.T) { client := GetClient() user := &Sample.User{} user.Name = "thrift" user.Address = "address" rep, err := client.SayHello(ctx, user) if err != nil { t.Errorf("thrift err: %v\n", err) } else { t.Logf("Recevied: %v\n", rep) } }

首先确保服务端已运行,然后运行测试用例:

$ go test -v === RUN TestGetUser --- PASS: TestGetUser (0.00s) client_test.go:53: Recevied: Response({ErrCode:1 ErrMsg:user not exist. Data:map[]}) === RUN TestSayHello --- PASS: TestSayHello (0.00s) client_test.go:69: Recevied: Response({ErrCode:0 ErrMsg:success Data:map[User:{"id":0,"name":"thrift","avatar":"","address":"address","mobile":""}]}) PASS ok sample 0.017s

接下来我们使用php实现客户端:
client.php

<?php error_reporting(E_ALL); $ROOT_DIR = realpath(dirname(__FILE__) . '/lib-php/'); $GEN_DIR = realpath(dirname(__FILE__)) . '/gen-php/'; require_once $ROOT_DIR . '/Thrift/ClassLoader/ThriftClassLoader.php'; use Thrift\ClassLoader\ThriftClassLoader; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TSocket; use Thrift\Transport\TBufferedTransport; use \Thrift\Transport\THttpClient; $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift', $ROOT_DIR); $loader->registerDefinition('Sample', $GEN_DIR); $loader->register(); try { if (array_search('--http', $argv)) { $socket = new THttpClient('localhost', 8080, '/server.php'); } else { $socket = new TSocket('localhost', 9090); } $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new \Sample\GreeterClient($protocol); $transport->open(); try { $user = new \Sample\User(); $user->id = 100; $user->name = "test"; $user->avatar = "avatar"; $user->address = "address"; $user->mobile = "mobile"; $rep = $client->SayHello($user); var_dump($rep); $rep = $client->GetUser(100); var_dump($rep); } catch (\tutorial\InvalidOperation $io) { print "InvalidOperation: $io->why\n"; } $transport->close(); } catch (TException $tx) { print 'TException: ' . $tx->getMessage() . "\n"; } ?>

在运行PHP客户端之前,需要引入thrift的php库文件。我们下载下来的thrift源码包里面就有:

~/Downloads/thrift-0.12.0/lib/php/ ├── Makefile.am ├── Makefile.in ├── README.apache.md ├── README.md ├── coding_standards.md ├── lib ├── src ├── test └── thrift_protocol.ini

我们在当前项目里新建lib-php目录,并需要把整个php下的代码复制到lib-php目录:

$ cp -rp ~/Downloads/thrift-0.12.0/lib/php/* ./lib-php/

然后需要修改/lib-php/里的lib目录名为Thrift,否则后续会一直提示Class 'Thrift\Transport\TSocket' not found。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzdxy.html