Commit 5c94886a authored by tufengqi's avatar tufengqi

新增客户端读取服务请求参数逻辑

parent aeb01c63
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
namespace fpf\thrift; namespace fpf\thrift;
use Yii;
use fpf\response\ResponseApi;
use fpf\response\BaseConstant;
class ThriftServiceFactoryProxy class ThriftServiceFactoryProxy
{ {
private $instance = null; private $instance = null;
...@@ -17,7 +21,30 @@ class ThriftServiceFactoryProxy ...@@ -17,7 +21,30 @@ class ThriftServiceFactoryProxy
self::BINARY => 'application/thrift-binary', self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json', self::JSON => 'application/thrift-json',
]; ];
$this->socket = new THttpClient('service.local.zhaobi.com', 80, '/market/BaseMarketService/'); $check_ret = $this->checkService($class_name);
if (true === $check_ret[BaseConstant::ERROR]) {
throw new \Exception($check_ret[BaseConstant::MSG]);
}
$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_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_check_arr = $check_ret[BaseConstant::MSG];
$service_name_space_str = $service_check_arr[0];
$service_str = $service_check_arr[1];
if (empty($service_module_hosts[$service_name_space_str . '-' . $service_str])) {
$service_config = $service_default_hosts;
} else {
$service_config = $service_module_hosts[$service_name_space_str . '-' . $service_str];
}
$count_service = count($service_config);
if ($count_service > 1) {
$service_index = rand(0, $count_service - 1);
} else {
$service_index = 0;
}
$service_real = $service_config[$service_index];
$this->socket = new THttpClient($service_real['host'], $service_real['port'], '/' . $service_name_space_str . '/' . $service_str . '/');
$this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]); $this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]);
$this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024); $this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024);
if (self::BINARY === $content_type) { if (self::BINARY === $content_type) {
...@@ -28,6 +55,29 @@ class ThriftServiceFactoryProxy ...@@ -28,6 +55,29 @@ class ThriftServiceFactoryProxy
$this->instance = new $class_name($protocol); $this->instance = new $class_name($protocol);
} }
private function checkService($class_name)
{
$tem_arr = explode('\\', trim($class_name, '\\'));
$error = '';
if (3 !== count($tem_arr)) {
$error = 'service split must be have 3 items';
goto doEnd;
}
if ('service' !== $tem_arr[0]) {
$error = 'service first spit item must be \'service\'';
goto doEnd;
}
if (!preg_match("/^([a-zA-Z0-9])+Client$/", $class_name[2], $matches)) {
$error = 'service must ending with \'Client\'';
goto doEnd;
}
doEnd :
if ($error) {
return ResponseApi::arrFail($error);
}
return ResponseApi::arrSuccess([$tem_arr[1], $matches[1]]);
}
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
$this->transport->open(); $this->transport->open();
......
{ {
"name": "system", "name": "system",
"type": "library", "type": "library",
"description": "一些公司内部开发的类",
"require": {}, "require": {},
"require-dev": {}, "require-dev": {},
"repositories": {}, "repositories": {},
......
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
$service_domain_name = 'service.local.zhaobi.com'; $service_domain_name = 'service.local.zhaobi.com';
//$service_domain_name = 'service.local.zhaobi.com'; //$service_domain_name = 'service.local.zhaobi.com';
//$service_domain_name = 'service.zhaobi.com'; //$service_domain_name = 'service.zhaobi.com';
$config['service_default_protocol'] = 'binary'; // binary json $config['service_default_protocol'] = 0; // 0:binary 1:json
$service_default_hosts = [ $config['service_default_hosts'] = [
['host' => $service_domain_name, 'port' => 80, 'timeout' => 10, 'connect_timeout' => 100, 'request_timeout' => 200] [
'host' => $service_domain_name, 'port' => 80, 'timeout' => 10, 'connect_timeout' => 100,
'request_timeout' => 200, 'protocol' => 0
],
]; ];
$config['service_default_hostname'] = $service_domain_name;
$config['service_default_failure_policy'] = [ $config['service_default_failure_policy'] = [
'default_connect_timeout' => 1000, // ms 'default_connect_timeout' => 1000, // ms
'default_request_timeout' => 2000, // ms 'default_request_timeout' => 2000, // ms
...@@ -14,4 +16,4 @@ $config['service_default_failure_policy'] = [ ...@@ -14,4 +16,4 @@ $config['service_default_failure_policy'] = [
'failover_retry_times' => 2, 'failover_retry_times' => 2,
'final_fail_return' => 'false', // false or exception, 返回false或者抛异常 'final_fail_return' => 'false', // false or exception, 返回false或者抛异常
]; ];
$config['service_module_hosts']['market-BaseMarketService'] = $service_default_hosts; $config['service_module_hosts']['market-BaseMarketService'] = $config['service_default_hosts'];
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