BaseService.php
4.0 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace AukeySwrpc;
use AukeySwrpc\Request\SyncRequest;
use AukeySwrpc\Tracer\TracerContext;
/**
* Class BaseService
*
* @package App\Clients
* @author pengjch 2024314 11:26:45
*/
class BaseService
{
protected $serviceKey;
protected $traceContext = null;
/**
* @return static
* @author 2021-03-14 11:25:18
*/
public static function factory()
{
return new static();
}
/**
* BaseService constructor.
*/
public function __construct()
{
$this->init();
}
/**
* @author pengjch 2024314 11:33:22
*/
protected function init()
{
}
/**
* @param $serviceKey
* @author pengjch 2024314 11:31:57
*/
protected function setService($serviceKey)
{
$this->serviceKey = $serviceKey;
}
/**
* 链路追踪上下文
*
* @param TracerContext|null $context
* @return $this
* @author pengjch 2024314 11:27:45
*/
public function trace(?TracerContext $context = null)
{
$this->traceContext = $context;
return $this;
}
/**
* 调用远程服务
*
* @param $method
* @param $args
* @return mixed
* @throws \Exception
* @author pengjch 2024-03-14 11:25:18
*/
protected function callRemoteService($method, $args)
{
$serviceNs = $this->getTargetServiceNamespace();
$method = $serviceNs . '_' . $method;
$client = ClientManger::getInstance($this->serviceKey);
$request = SyncRequest::create($method, $args, $this->traceContext);
return $client->send($request);
}
/**
* 调用本地服务
*
* @param $method
* @param $args
* @return string
* @author pengjch 2024-03-14 11:25:18
*/
protected function callLocalService($method, $args)
{
$serviceNs = $this->getTargetServiceNamespace();
return call_user_func_array([$serviceNs::factory(), $method], $args);
}
/**
* 获取目标调用类命名空间
*
* @return string
* @author pengjch 2024314 12:6:8
*/
protected function getTargetServiceNamespace(): string
{
$moduleName = $this->extractModuleName();
/** @var \Nwidart\Modules\Laravel\Module $module */
// $loader = require base_path('vendor').'/autoload.php';
// // 获取注册的命名空间前缀并筛选出 Composer 包的命名空间
// $prefixes = $loader->getPrefixesPsr4();
// foreach ($prefixes as $key => $prefix) {
// if($moduleName.'\\' === $key){
//
// }
// }
// $moduleNs = ltrim(str_replace([base_path(), '/'], ['', '\\'], $module->getPath()), '\\');
return $moduleName . '\\Http\\Service\\' . $this->extractServiceName();
}
/**
* 根据调用类提前模块名称
*
* @return false|string
* @author pengjch 2024314 12:0:17
*/
protected function extractModuleName()
{
$calledClass = get_called_class();
$module = str_replace(['App\Clients\\', '\\'], ['', '_'], $calledClass);
return substr($module, 0, strrpos($module, '_'));
}
/**
* 根据调用类提前service名称
*
* @return false|string
* @author pengjch 2024314 12:0:34
*/
protected function extractServiceName()
{
$calledClass = get_called_class();
return substr($calledClass, strrpos($calledClass, '\\') + 1);
}
/**
* __call
*
* @param $method
* @param $args
* @return false|mixed
* @throws \Exception
* @author 2021-03-14 11:25:18
*/
public function __call($method, $args)
{
switch (env('SERVICE_CALL_STRATEGY', 'remote')) {
case 'remote':
return $this->callRemoteService($method, $args);
case 'local':
return $this->callLocalService($method, $args);
default:
throw new \Exception('error strategy');
}
}
}