-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_php.php
122 lines (90 loc) · 3.38 KB
/
test_php.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
define('THRIFT_ROOT', __DIR__);
require_once THRIFT_ROOT . '/Applications/ThriftRpc/Lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Exception\TException;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\TSocket;
$loader = new ThriftClassLoader();
// 参考: https://thrift.apache.org/tutorial/php
$loader->registerNamespace('Thrift', THRIFT_ROOT . '/Applications/ThriftRpc/Lib');
$loader->registerDefinition('geoip_service', THRIFT_ROOT . "/gen-php/");
$loader->registerDefinition('rpc_thrift', THRIFT_ROOT . "/gen-php/");
$loader->register();
class TestCode {
function testDirectRPC() {
try {
$socket = new TSocket('localhost', 5563);
// $transport = new TBufferedTransport($socket, 1024, 1024);
$transport = new \Thrift\Transport\TFramedTransport($socket, true, true);
$protocol = new TBinaryProtocol($transport);
$client = new \geoip_service\GeoIpServiceClient($protocol);
$transport->open();
$client->ping();
print "ping()\n";
$data = $client->IpToGeoData("120.52.139.7");
var_dump($data);
$transport->close();
} catch
(TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
}
function testProxiedRPC() {
try {
// 127.0.0.1:5550
// /usr/local/rpc_proxy/proxy.sock
// 直接使用rpc proxy进行通信
// $socket = new TSocket('localhost', 5550);
$socket = new TSocket('/usr/local/rpc_proxy/proxy.sock');
$service_name = "ipgeo";
// $transport = new TBufferedTransport($socket, 1024, 1024);
$transport = new \Thrift\Transport\TFramedTransport($socket, true, true);
// 指定后端的服务
$protocol = new TMultiplexedProtocol(new TBinaryProtocol($transport), $service_name);
$client = new \geoip_service\GeoIpServiceClient($protocol);
$transport->open();
$client->ping();
print "ping()\n";
$data = $client->IpToGeoData("120.52.139.7");
var_dump($data);
$transport->close();
} catch
(TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
}
function testProxiedRPCHelloworld() {
try {
// 127.0.0.1:5550
// /usr/local/rpc_proxy/proxy.sock
foreach (glob(__DIR__ . '/Applications/ThriftRpc/Services/HelloWorld/*.php') as $start_file) {
// echo $start_file;
require_once $start_file;
}
// 直接使用rpc proxy进行通信
$socket = new TSocket('tcp://localhost', 5550);
// $socket = new TSocket('/usr/local/rpc_proxy/proxy.sock');
$service_name = "hello";
// $transport = new TBufferedTransport($socket, 1024, 1024);
$transport = new \Thrift\Transport\TFramedTransport($socket, true, true);
// 指定后端的服务
$protocol = new TMultiplexedProtocol(new TBinaryProtocol($transport), $service_name);
$client = new \Services\HelloWorld\HelloWorldClient($protocol);
$transport->open();
// $client->ping();
// print "ping()\n";
$data = $client->sayHello("wwww");
var_dump($data);
$transport->close();
} catch
(TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
}
}
$testCode = new TestCode();
// $testCode->testDirectRPC();
// $testCode->testProxiedRPC();
$testCode->testProxiedRPCHelloworld();