Skip to content

Commit 8480104

Browse files
Support service register for rpc-multiplex. (#3941)
* 支持 rpc-multiplex 注册到服务中心 * 支持 rpc-multiplex 注册到服务中心,增加监控检查支持 * Support service register for `rpc-multiplex`. Co-authored-by: 李铭昕 <715557344@qq.com>
1 parent 840a738 commit 8480104

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\RpcMultiplex\Listener;
13+
14+
use Hyperf\Event\Contract\ListenerInterface;
15+
use Hyperf\RpcMultiplex\Constant;
16+
use Hyperf\RpcServer\Event\AfterPathRegister;
17+
use Hyperf\ServiceGovernance\ServiceManager;
18+
19+
class RegisterServiceListener implements ListenerInterface
20+
{
21+
/**
22+
* @var ServiceManager
23+
*/
24+
private $serviceManager;
25+
26+
public function __construct(ServiceManager $serviceManager)
27+
{
28+
$this->serviceManager = $serviceManager;
29+
}
30+
31+
public function listen(): array
32+
{
33+
return [
34+
AfterPathRegister::class,
35+
];
36+
}
37+
38+
/**
39+
* All official rpc protocols should register in here,
40+
* and the others non-official protocols should register in their own component via listener.
41+
*
42+
* @param AfterPathRegister $event
43+
*/
44+
public function process(object $event)
45+
{
46+
$annotation = $event->annotation;
47+
if (! in_array($annotation->protocol, $this->getProtocols(), true)) {
48+
return;
49+
}
50+
$metadata = $event->toArray();
51+
$annotationArray = $metadata['annotation'];
52+
unset($metadata['path'], $metadata['annotation'], $annotationArray['name']);
53+
$this->serviceManager->register($annotation->name, $event->path, array_merge($metadata, $annotationArray));
54+
}
55+
56+
protected function getProtocols(): array
57+
{
58+
return [
59+
Constant::PROTOCOL_DEFAULT,
60+
];
61+
}
62+
}

0 commit comments

Comments
 (0)