-
Notifications
You must be signed in to change notification settings - Fork 2
客户端
simon edited this page Jan 15, 2019
·
6 revisions
在config/app.php
中增加
'providers' => [
CrCms\Foundation\ConnectionPool\PoolServiceProvider::class,
CrCms\Foundation\Client\ClientServiceProvider::class,
]
在config/client.php
的connections
中增加如下测试配置
'http' => [
'driver' => 'http',
'host' => 'crcms.cn',
'port' => 80,
//资源连接器的配置,请参考guzzlehttp
'settings' => [
'timeout' => 1,
],
],
在config/pool.php
的connections
中增加配置(可选增加)
'client' => [
'max_idle_number' => 50,//最大空闲数
'min_idle_number' => 15,//最小空闲数
'max_connection_number' => 20,//最大连接数
],
//实例化并使用连接池
$client = $this->app->make('client.manager')->connection('http', true);
//发送请求,连接成功后则会多态调用connection同的可用实例方法
$client = $client->request('/', []);
//获取当前连接
dump(get_class($client->getConnection()));
//获取连接池
dump(get_class($client->getPool()));
//获取当前的连接资源的响应
dump(get_class($client->getResponse()));
//获取资源响应内容
dump($client->getContent());
//断开连接
$client->disconnection();
$client = $this->app->make('client.manager')->connection([
'name' => 'http',//如果connection方法中的$userPool为true,则必须要有连接名称
'driver' => 'http',
'host' => '192.168.1.12',
'port' => 8500,
'settings' => [
'timeout' => 1,
],
]);
$client = $this->app->make('client.manager')->connection([
'name' => 'http',
'driver' => 'http',
'host' => '192.168.1.12',
'port' => 8500,
'settings' => [
'timeout' => 1,
],
], false);
如果有连接名则会调用config中的相应连接名配置,如果需要覆盖默认配置,则直接定义即可,如上定义了host及port则会覆盖默认配置
- Http
- Yar