PHP Wrapper implementation

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.