将每个服务的 client 改为单例,修正重入问题
This commit is contained in:
parent
5aaeaa0499
commit
6c54737d13
|
@ -5,25 +5,15 @@ namespace PdInternalApi;
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $currentApp;
|
protected $service_name;
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
public function __construct($config)
|
public function __construct($service_name, $config)
|
||||||
{
|
{
|
||||||
|
$this->service_name = $service_name;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $app
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function app($app)
|
|
||||||
{
|
|
||||||
if (isset($this->config[$app]))
|
|
||||||
$this->currentApp = $app;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用api,如果状态码不为200则抛出异常
|
* 调用api,如果状态码不为200则抛出异常
|
||||||
* @param $uri
|
* @param $uri
|
||||||
|
@ -33,8 +23,7 @@ class Client
|
||||||
*/
|
*/
|
||||||
public function call($uri, $params)
|
public function call($uri, $params)
|
||||||
{
|
{
|
||||||
$config = array_merge(['timeout' => 3],
|
$config = array_merge(['timeout' => 3], $this->config);
|
||||||
$this->config[$this->currentApp]);
|
|
||||||
$secret = $config['secret'];
|
$secret = $config['secret'];
|
||||||
unset($config['secret']);
|
unset($config['secret']);
|
||||||
$client = new \GuzzleHttp\Client($config);
|
$client = new \GuzzleHttp\Client($config);
|
||||||
|
@ -42,10 +31,10 @@ class Client
|
||||||
$params['timestamp'] = time();
|
$params['timestamp'] = time();
|
||||||
$params['sign'] = sign($params, $secret);
|
$params['sign'] = sign($params, $secret);
|
||||||
$resp = $client->post($uri, ['form_params' => $params]);
|
$resp = $client->post($uri, ['form_params' => $params]);
|
||||||
if ($resp->getStatusCode() == 200) {
|
if ($resp->getStatusCode() != 200) {
|
||||||
return \GuzzleHttp\json_decode($resp->getBody(), true);
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
return \GuzzleHttp\json_decode($resp->getBody(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,11 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->app->configure('internal_api');
|
$this->app->configure('internal_api');
|
||||||
$this->app->singleton('internal.api', function () {
|
foreach (config('internal_api.client') as $service_name => $config) {
|
||||||
return new Client(config('internal_api.client'));
|
$this->app->singleton('internal.api.' . $service_name, function () use ($service_name, $config) {
|
||||||
});
|
return new Client($service_name, $config);
|
||||||
foreach (config('internal_api.client') as $key => $config) {
|
|
||||||
$this->app->singleton('internal.api.' . $key, function () use ($key) {
|
|
||||||
return $this->app['internal.api']->app($key);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user