Commit d8fcfc90 authored by tufengqi's avatar tufengqi

fix

parent 699aded9
...@@ -9,8 +9,10 @@ use fpf\response\BaseConstant; ...@@ -9,8 +9,10 @@ use fpf\response\BaseConstant;
class ThriftServiceFactoryProxy class ThriftServiceFactoryProxy
{ {
private $instance = null; private $instance = null;
private $transport = null; private $request_base_params = null;
private $socket = null; private $tem_class_name = '';
private $class_name = '';
private $service_real_config;
const BINARY = 0; const BINARY = 0;
const JSON = 1; const JSON = 1;
...@@ -18,6 +20,7 @@ class ThriftServiceFactoryProxy ...@@ -18,6 +20,7 @@ class ThriftServiceFactoryProxy
{ {
$service_default_protocol = Yii::$app->fpf->getConfig('service_default_protocol', 'thrift_service'); $service_default_protocol = Yii::$app->fpf->getConfig('service_default_protocol', 'thrift_service');
$service_default_hosts = Yii::$app->fpf->getConfig('service_default_hosts', 'thrift_service'); $service_default_hosts = Yii::$app->fpf->getConfig('service_default_hosts', 'thrift_service');
$service_default_failure_policy = Yii::$app->fpf->getConfig('service_default_failure_policy', 'thrift_service');
$service_module_hosts = Yii::$app->fpf->getConfig('service_module_hosts', 'thrift_service'); $service_module_hosts = Yii::$app->fpf->getConfig('service_module_hosts', 'thrift_service');
$check_ret = $this->checkService($class_name); $check_ret = $this->checkService($class_name);
if (true === $check_ret[BaseConstant::ERROR]) { if (true === $check_ret[BaseConstant::ERROR]) {
...@@ -26,6 +29,8 @@ class ThriftServiceFactoryProxy ...@@ -26,6 +29,8 @@ class ThriftServiceFactoryProxy
$service_check_arr = $check_ret[BaseConstant::MSG]; $service_check_arr = $check_ret[BaseConstant::MSG];
$service_name_space_str = $service_check_arr[0]; $service_name_space_str = $service_check_arr[0];
$service_str = $service_check_arr[1]; $service_str = $service_check_arr[1];
$this->tem_class_name = '\\service\\' . $service_name_space_str . '\\' . $service_str;
$this->class_name = $class_name;
if (empty($service_module_hosts[$service_name_space_str . '-' . $service_str])) { if (empty($service_module_hosts[$service_name_space_str . '-' . $service_str])) {
$service_config = $service_default_hosts; $service_config = $service_default_hosts;
} else { } else {
...@@ -47,32 +52,14 @@ class ThriftServiceFactoryProxy ...@@ -47,32 +52,14 @@ class ThriftServiceFactoryProxy
self::BINARY => 'application/thrift-binary', self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json', self::JSON => 'application/thrift-json',
]; ];
if (defined('IS_SWOOLE_SERVICE') && IS_SWOOLE_SERVICE === true) { $this->service_real_config = $service_real;
$this->socket = new SwoftHttpClient( $this->request_base_params = [
$service_real['host'], 'host' => $service_real['host'],
$service_real['port'], 'port' => $service_real['port'],
'/' . $service_name_space_str . '/' . $service_str . '/', 'uri' => '/' . $service_name_space_str . '/' . $service_str . '/',
'http', 'content_type_str' => $content_type_mapping[$content_type],
$service_real 'content_type' => $content_type,
); ];
$this->socket->addHeaders(['Contents-Type' => $content_type_mapping[$content_type]]);
} else {
$this->socket = new TCurlClient(
$service_real['host'],
$service_real['port'],
'/' . $service_name_space_str . '/' . $service_str . '/',
'http',
$service_real
);
$this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]);
}
$this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024);
if (self::BINARY === $content_type) {
$protocol = new \Thrift\Protocol\TBinaryProtocol($this->transport, $strict_read = true, $strict_write = true);
} elseif (self::JSON === $content_type) {
$protocol = new \Thrift\Protocol\TJSONProtocol($this->transport);
}
$this->instance = new $class_name($protocol);
} }
private function checkService($class_name) private function checkService($class_name)
...@@ -100,10 +87,36 @@ class ThriftServiceFactoryProxy ...@@ -100,10 +87,36 @@ class ThriftServiceFactoryProxy
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
$this->transport->open(); if (defined('IS_SWOOLE_SERVICE') && IS_SWOOLE_SERVICE === true) {
$this->socket->setFuncUri($name); $socket = new SwoftHttpClient(
$this->request_base_params['host'],
$this->request_base_params['port'],
$this->request_base_params['uri'],
'http',
$service_real
);
$socket->addHeaders(['Contents-Type' => $content_type_mapping[$content_type]]);
} else {
$socket = new TCurlClient(
$this->request_base_params['host'],
$this->request_base_params['port'],
$this->request_base_params['uri'],
'http',
$this->service_real_config
);
$socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]);
}
$transport = new \Thrift\Transport\TBufferedTransport($socket, 1024, 1024);
if (self::BINARY === $content_type) {
$protocol = new \Thrift\Protocol\TBinaryProtocol($transport, $strict_read = true, $strict_write = true);
} elseif (self::JSON === $content_type) {
$protocol = new \Thrift\Protocol\TJSONProtocol($transport);
}
$this->instance = new $this->class_name($protocol);
$transport->open();
$socket->setFuncUri($name);
$ret = call_user_func_array([$this->instance, $name], $arguments); $ret = call_user_func_array([$this->instance, $name], $arguments);
$this->transport->close(); $transport->close();
return $ret; return $ret;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment