This commit is contained in:
xl
2022-05-04 03:32:10 +08:00
parent ad346d01d1
commit 4b50c3fc18
2 changed files with 114 additions and 117 deletions
+114 -114
View File
@@ -21,138 +21,138 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
{ {
/** /**
* @var Pool * @var Pool
*/ */
public Pool $pool; public Pool $pool;
/** /**
* @var RpcManager * @var RpcManager
*/ */
#[Inject(RpcManager::class)] #[Inject(RpcManager::class)]
public RpcManager $manager; public RpcManager $manager;
/** /**
* @var RpcClientInterface * @var RpcClientInterface
*/ */
#[Inject(RpcClientInterface::class)] #[Inject(RpcClientInterface::class)]
public RpcClientInterface $client; public RpcClientInterface $client;
protected string $name = ''; protected string $name = '';
/** /**
* @param string $method * @param string $method
* @param mixed $data * @param mixed $data
* @param string $version * @param string $version
* @throws Exception * @throws Exception
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function notify(string $method, mixed $data, string $version = '2.0'): void public function notify(string $method, mixed $data, string $version = '2.0'): void
{ {
$this->client->withConfig($this->get_consul($this->name))->sendRequest( $this->client->withConfig($this->get_consul($this->name))->sendRequest(
$this->requestBody([ $this->requestBody([
'jsonrpc' => $version, 'jsonrpc' => $version,
'service' => $this->name, 'service' => str_starts_with($this->name, '/') ? $this->name : '/' . $this->name,
'method' => $method, 'method' => $method,
'params' => $data, 'params' => $data,
]) ])
); );
} }
/** /**
* @param array $data * @param array $data
* @return ServerRequestInterface * @return ServerRequestInterface
*/ */
private function requestBody(array $data): ServerRequestInterface private function requestBody(array $data): ServerRequestInterface
{ {
$server = Kiri::getDi()->get(ServerRequest::class); $server = Kiri::getDi()->get(ServerRequest::class);
return $server->withBody(new Stream(json_encode($data))); return $server->withBody(new Stream(json_encode($data)));
} }
/** /**
* @param string $method * @param string $method
* @param mixed $data * @param mixed $data
* @param string $version * @param string $version
* @param string $id * @param string $id
* @return mixed * @return mixed
* @throws Exception * @throws Exception
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function get(string $method, mixed $data, string $version = '2.0', string $id = ''): ResponseInterface public function get(string $method, mixed $data, string $version = '2.0', string $id = ''): ResponseInterface
{ {
if (empty($id)) $id = Number::create(time()); if (empty($id)) $id = Number::create(time());
return $this->client->withConfig($this->get_consul($this->name))->sendRequest( return $this->client->withConfig($this->get_consul($this->name))->sendRequest(
$this->requestBody([ $this->requestBody([
'jsonrpc' => $version, 'jsonrpc' => $version,
'service' => $this->name, 'service' => str_starts_with($this->name, '/') ? $this->name : '/' . $this->name,
'method' => $method, 'method' => $method,
'params' => $data, 'params' => $data,
'id' => $id 'id' => $id
]) ])
); );
} }
/** /**
* @param array $data * @param array $data
* @return mixed * @return mixed
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function batch(array $data): mixed public function batch(array $data): mixed
{ {
return $this->client->withConfig($this->get_consul($this->name))->sendRequest( return $this->client->withConfig($this->get_consul($this->name))->sendRequest(
$this->requestBody($data) $this->requestBody($data)
); );
} }
/** /**
* @param $service * @param $service
* @return array * @return array
* @throws RpcServiceException|\ReflectionException * @throws RpcServiceException|\ReflectionException
* @throws Exception * @throws Exception
*/ */
private function get_consul($service): array private function get_consul($service): array
{ {
if (empty($service)) { if (empty($service)) {
throw new RpcServiceException('You need set rpc service name if used.'); throw new RpcServiceException('You need set rpc service name if used.');
} }
$sf = $this->manager->getServices($service); $sf = $this->manager->getServices($service);
if (empty($sf) || !is_array($sf)) { if (empty($sf) || !is_array($sf)) {
throw new RpcServiceException('You need set rpc service name if used.'); throw new RpcServiceException('You need set rpc service name if used.');
} }
return $this->_loadRand($sf); return $this->_loadRand($sf);
} }
/** /**
* @param $services * @param $services
* @return array * @return array
*/ */
private function _loadRand($services): array private function _loadRand($services): array
{ {
$array = []; $array = [];
foreach ($services as $value) { foreach ($services as $value) {
$value['Weight'] = $value['Weights']['Passing']; $value['Weight'] = $value['Weights']['Passing'];
$array[] = $value; $array[] = $value;
} }
if (count($array) < 2) { if (count($array) < 2) {
$luck = $array[0]; $luck = $array[0];
} else { } else {
$luck = Luckdraw::luck($array, 'Weight'); $luck = Luckdraw::luck($array, 'Weight');
} }
return [ return [
'Address' => $luck['TaggedAddresses']['wan_ipv4']['Address'], 'Address' => $luck['TaggedAddresses']['wan_ipv4']['Address'],
'Port' => $luck['TaggedAddresses']['wan_ipv4']['Port'] 'Port' => $luck['TaggedAddresses']['wan_ipv4']['Port']
]; ];
} }
} }
-3
View File
@@ -226,9 +226,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
private function dispatch($data): array private function dispatch($data): array
{ {
try { try {
if (!str_starts_with($data['service'], '/')) {
$data['service'] = '/' . $data['service'];
}
$handler = $this->collector->find($data['service'], 'GET'); $handler = $this->collector->find($data['service'], 'GET');
if (is_integer($handler) || is_null($handler)) { if (is_integer($handler) || is_null($handler)) {
throw new Exception('Handler not found', -32601); throw new Exception('Handler not found', -32601);