ClientManger.php
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace AukeySwrpc;
use AukeySwrpc\Client;
use AukeySwrpc\Register\Consul;
/**
* 客户端管理器
* Class ClientManger
*
* @package App\Clients
* @author pengjch 202435 23:20:30
*/
class ClientManger
{
/** @var \Hprose\Client */
private static $clients;
private function __sleep()
{
}
private function __wakeup()
{
}
private function __construct()
{
}
/**
* @param $key
* @return Client
* @throws \Exception
* @author pengjch 2024314 12:34:8
*/
public static function getInstance($key): Client
{
if (!isset(self::$clients[$key])) {
$registerUri = env('SWRPC_REGISTER_URI');
if ($registerUri) { //从注册中心获取连接地址
self::$clients[$key] = Client::createBalancer($key, new Consul($registerUri));
} else { //直连模式
$conf = explode(':', env($key));
if (!$conf || count($conf) != 2) {
throw new \Exception('.env未配置' . $key);
}
self::$clients[$key] = Client::create($key, $conf[0], $conf[1]);
}
}
return self::$clients[$key];
}
}