mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Cancel grpc-server
's dependency on hyperf/rpc
. (#5554)
This commit is contained in:
parent
b414020eae
commit
a1d880d386
@ -1,5 +1,9 @@
|
||||
# v3.0.13 - TBD
|
||||
|
||||
## Optimized
|
||||
|
||||
- [#5544](https://github.com/hyperf/hyperf/pull/5554) Cancel `grpc-server`'s dependency on `hyperf/rpc`.
|
||||
|
||||
# v3.0.12 - 2023-03-20
|
||||
|
||||
## Added
|
||||
|
@ -19,10 +19,11 @@ use Hyperf\GrpcClient\GrpcNormalizer;
|
||||
use Hyperf\GrpcClient\GrpcPacker;
|
||||
use Hyperf\GrpcClient\GrpcTransporter;
|
||||
use Hyperf\Rpc\ProtocolManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class RegisterProtocolListener implements ListenerInterface
|
||||
{
|
||||
public function __construct(private ProtocolManager $protocolManager)
|
||||
public function __construct(private ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
@ -39,7 +40,9 @@ class RegisterProtocolListener implements ListenerInterface
|
||||
*/
|
||||
public function process(object $event): void
|
||||
{
|
||||
$this->protocolManager->register('grpc', [
|
||||
if ($this->container->has(ProtocolManager::class)) {
|
||||
$manager = $this->container->get(ProtocolManager::class);
|
||||
$manager->register('grpc', [
|
||||
'packer' => GrpcPacker::class,
|
||||
'transporter' => GrpcTransporter::class,
|
||||
'path-generator' => PathGenerator::class,
|
||||
@ -47,4 +50,5 @@ class RegisterProtocolListener implements ListenerInterface
|
||||
'normalizer' => GrpcNormalizer::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
21
src/grpc-server/publish/grpc_server.php
Normal file
21
src/grpc-server/publish/grpc_server.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
return [
|
||||
// Whether to support services by rpc-server
|
||||
// Required `hyperf/rpc` and `hyperf/rpc-server`.
|
||||
'rpc' => [
|
||||
// The port name
|
||||
'grpc' => [
|
||||
'enable' => false,
|
||||
],
|
||||
],
|
||||
];
|
@ -16,6 +16,7 @@ use FastRoute\Dispatcher;
|
||||
use Google\Protobuf\Internal\Message;
|
||||
use Google\Protobuf\Internal\Message as ProtobufMessage;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Di\MethodDefinitionCollector;
|
||||
use Hyperf\Di\ReflectionManager;
|
||||
use Hyperf\Grpc\Parser;
|
||||
@ -35,11 +36,16 @@ use RuntimeException;
|
||||
|
||||
class CoreMiddleware extends HttpCoreMiddleware
|
||||
{
|
||||
protected Protocol $protocol;
|
||||
/**
|
||||
* @var null|Protocol
|
||||
*/
|
||||
protected mixed $protocol = null;
|
||||
|
||||
public function __construct($container, string $serverName)
|
||||
{
|
||||
if ($container->get(ConfigInterface::class)->get(sprintf('grpc_server.rpc.%s.enable', $serverName), false)) {
|
||||
$this->protocol = new Protocol($container, $container->get(ProtocolManager::class), 'grpc');
|
||||
}
|
||||
|
||||
parent::__construct($container, $serverName);
|
||||
}
|
||||
@ -91,12 +97,16 @@ class CoreMiddleware extends HttpCoreMiddleware
|
||||
|
||||
protected function createDispatcher(string $serverName): Dispatcher
|
||||
{
|
||||
if ($this->protocol) {
|
||||
$factory = make(DispatcherFactory::class, [
|
||||
'pathGenerator' => $this->protocol->getPathGenerator(),
|
||||
]);
|
||||
return $factory->getDispatcher($serverName);
|
||||
}
|
||||
|
||||
return parent::createDispatcher($serverName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer the non-standard response content to a standard response object.
|
||||
*
|
||||
|
@ -15,10 +15,11 @@ use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\BootApplication;
|
||||
use Hyperf\Grpc\PathGenerator;
|
||||
use Hyperf\Rpc\ProtocolManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class RegisterProtocolListener implements ListenerInterface
|
||||
{
|
||||
public function __construct(private ProtocolManager $protocolManager)
|
||||
public function __construct(private ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
@ -35,8 +36,11 @@ class RegisterProtocolListener implements ListenerInterface
|
||||
*/
|
||||
public function process(object $event): void
|
||||
{
|
||||
$this->protocolManager->registerOrAppend('grpc', [
|
||||
if ($this->container->has(ProtocolManager::class)) {
|
||||
$manager = $this->container->get(ProtocolManager::class);
|
||||
$manager->registerOrAppend('grpc', [
|
||||
'path-generator' => PathGenerator::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,10 +14,11 @@ namespace Hyperf\GrpcServer\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\RpcServer\Event\AfterPathRegister;
|
||||
use Hyperf\ServiceGovernance\ServiceManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class RegisterServiceListener implements ListenerInterface
|
||||
{
|
||||
public function __construct(private ServiceManager $serviceManager)
|
||||
public function __construct(private ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
@ -36,14 +37,18 @@ class RegisterServiceListener implements ListenerInterface
|
||||
*/
|
||||
public function process(object $event): void
|
||||
{
|
||||
if ($this->container->has(ServiceManager::class)) {
|
||||
$annotation = $event->annotation;
|
||||
if (! in_array($annotation->protocol, ['grpc'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$manager = $this->container->get(ServiceManager::class);
|
||||
|
||||
$metadata = $event->toArray();
|
||||
$annotationArray = $metadata['annotation'];
|
||||
unset($metadata['path'], $metadata['annotation'], $annotationArray['name']);
|
||||
$this->serviceManager->register($annotation->name, $event->path, array_merge($metadata, $annotationArray));
|
||||
$manager->register($annotation->name, $event->path, array_merge($metadata, $annotationArray));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace HyperfTest\GrpcServer;
|
||||
|
||||
use Closure;
|
||||
use Hyperf\Config\Config;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Contract\NormalizerInterface;
|
||||
use Hyperf\Di\ClosureDefinitionCollector;
|
||||
@ -80,6 +81,7 @@ class CoreMiddlewareTest extends TestCase
|
||||
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(Mockery::mock(EventDispatcherInterface::class));
|
||||
$container->shouldReceive('make')->with(RPCDispatcherFactory::class)->withAnyArgs()->andReturn($dispatcher = new RPCDispatcherFactory(Mockery::mock(EventDispatcherInterface::class), new PathGenerator()));
|
||||
$container->shouldReceive('get')->with(RPCDispatcherFactory::class . '.unit')->andReturn($dispatcher);
|
||||
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config(['grpc_server' => ['rpc' => ['grpc' => ['enable' => true]]]]));
|
||||
return $container;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user