Background
Make a thrift client wrapper to implement the retry logic for the server.
"Key Points"
1. Wrapper requirements are as convenient as using a client.
2. When a server is dead, you can randomly select another one to retry.
3. Several key features of PHP are used: __call (Magic function, which is invoked when an object function is accessed), Reflectionclass reflection classes and their member functions Newinstanceargs, Call_user_ Func_array callback function.
Just look at the code (some bull wrote, not me):
#!/usr/bin/env php <?php namespace wrapper;
Error_reporting (E_all);
Require_once '/usr/local/cellar/thrift/0.9.1/thrift/classloader/thriftclassloader.php ';
Use Thrift\classloader\thriftclassloader; $GEN _dir = Realpath (dirname (__file__). /..').'
/gen-php ';
$loader = new Thriftclassloader ();
$loader->registernamespace (' Thrift ', '/usr/local/cellar/thrift/0.9.1/');
$loader->registerdefinition (' Xiaoju ', $GEN _dir);
$loader->register ();
Use Thrift\protocol\tbinaryprotocol;
Use Thrift\transport\tsocket;
Use thrift\transport\thttpclient;
Use Thrift\transport\tbufferedtransport;
Use thrift\exception\texception; Class Retrywrapper {public function __construct ($classname, $hosts) {$this->clazz = new \reflectionclass (
$classname);
$this->hosts = $hosts;
The Public Function __call ($method, $args) {Shuffle ($this->hosts); foreach ($this->hosts as $key => $host) {try {return $this->inner_calL ($host, $method, $args);
The catch (Texception $ex) {$msg = $ex->getmessage ();
if (!strstr ($msg, ' Tsocket ')) {throw $ex;
}} throw new Texception ("All server down!");
The Public Function Inner_call ($host, $method, $args) {$tmp = Explode (":", $host);
$socket = new Tsocket ($tmp [0], (int) $tmp [1]);
$transport = new Tbufferedtransport ($socket, 1024, 1024);
$protocol = new Tbinaryprotocol ($transport);
$client = $this->clazz->newinstanceargs (Array ($protocol));
$transport->open ();
$result = Call_user_func_array (Array ($client, $method), $args);
$transport->close ();
return $result;
}} $hosts = Array (' localhost:9090 ', ' localhost:9091 ');
$wrapper = new Retrywrapper ("\xxx\xx\messageserviceclient", $hosts, 3); $data = Array (' Businessid ' => 300100001, ' phones ' => array (' 2 ', ' 2 ', ' 3 '),' Message ' => ' Asdfqer ');
$message = new \xxx\xx\message ($data);
Print $wrapper->sendmessage ($message);
print "\ n"; ?>
It's still pretty neat, just record it.