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
|
# 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
|
# v3.0.12 - 2023-03-20
|
||||||
|
|
||||||
## Added
|
## Added
|
||||||
|
@ -19,10 +19,11 @@ use Hyperf\GrpcClient\GrpcNormalizer;
|
|||||||
use Hyperf\GrpcClient\GrpcPacker;
|
use Hyperf\GrpcClient\GrpcPacker;
|
||||||
use Hyperf\GrpcClient\GrpcTransporter;
|
use Hyperf\GrpcClient\GrpcTransporter;
|
||||||
use Hyperf\Rpc\ProtocolManager;
|
use Hyperf\Rpc\ProtocolManager;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
class RegisterProtocolListener implements ListenerInterface
|
class RegisterProtocolListener implements ListenerInterface
|
||||||
{
|
{
|
||||||
public function __construct(private ProtocolManager $protocolManager)
|
public function __construct(private ContainerInterface $container)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,12 +40,15 @@ class RegisterProtocolListener implements ListenerInterface
|
|||||||
*/
|
*/
|
||||||
public function process(object $event): void
|
public function process(object $event): void
|
||||||
{
|
{
|
||||||
$this->protocolManager->register('grpc', [
|
if ($this->container->has(ProtocolManager::class)) {
|
||||||
'packer' => GrpcPacker::class,
|
$manager = $this->container->get(ProtocolManager::class);
|
||||||
'transporter' => GrpcTransporter::class,
|
$manager->register('grpc', [
|
||||||
'path-generator' => PathGenerator::class,
|
'packer' => GrpcPacker::class,
|
||||||
'data-formatter' => DataFormatter::class,
|
'transporter' => GrpcTransporter::class,
|
||||||
'normalizer' => GrpcNormalizer::class,
|
'path-generator' => PathGenerator::class,
|
||||||
]);
|
'data-formatter' => DataFormatter::class,
|
||||||
|
'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;
|
||||||
use Google\Protobuf\Internal\Message as ProtobufMessage;
|
use Google\Protobuf\Internal\Message as ProtobufMessage;
|
||||||
use Hyperf\Context\Context;
|
use Hyperf\Context\Context;
|
||||||
|
use Hyperf\Contract\ConfigInterface;
|
||||||
use Hyperf\Di\MethodDefinitionCollector;
|
use Hyperf\Di\MethodDefinitionCollector;
|
||||||
use Hyperf\Di\ReflectionManager;
|
use Hyperf\Di\ReflectionManager;
|
||||||
use Hyperf\Grpc\Parser;
|
use Hyperf\Grpc\Parser;
|
||||||
@ -35,11 +36,16 @@ use RuntimeException;
|
|||||||
|
|
||||||
class CoreMiddleware extends HttpCoreMiddleware
|
class CoreMiddleware extends HttpCoreMiddleware
|
||||||
{
|
{
|
||||||
protected Protocol $protocol;
|
/**
|
||||||
|
* @var null|Protocol
|
||||||
|
*/
|
||||||
|
protected mixed $protocol = null;
|
||||||
|
|
||||||
public function __construct($container, string $serverName)
|
public function __construct($container, string $serverName)
|
||||||
{
|
{
|
||||||
$this->protocol = new Protocol($container, $container->get(ProtocolManager::class), 'grpc');
|
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);
|
parent::__construct($container, $serverName);
|
||||||
}
|
}
|
||||||
@ -91,10 +97,14 @@ class CoreMiddleware extends HttpCoreMiddleware
|
|||||||
|
|
||||||
protected function createDispatcher(string $serverName): Dispatcher
|
protected function createDispatcher(string $serverName): Dispatcher
|
||||||
{
|
{
|
||||||
$factory = make(DispatcherFactory::class, [
|
if ($this->protocol) {
|
||||||
'pathGenerator' => $this->protocol->getPathGenerator(),
|
$factory = make(DispatcherFactory::class, [
|
||||||
]);
|
'pathGenerator' => $this->protocol->getPathGenerator(),
|
||||||
return $factory->getDispatcher($serverName);
|
]);
|
||||||
|
return $factory->getDispatcher($serverName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::createDispatcher($serverName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,10 +15,11 @@ use Hyperf\Event\Contract\ListenerInterface;
|
|||||||
use Hyperf\Framework\Event\BootApplication;
|
use Hyperf\Framework\Event\BootApplication;
|
||||||
use Hyperf\Grpc\PathGenerator;
|
use Hyperf\Grpc\PathGenerator;
|
||||||
use Hyperf\Rpc\ProtocolManager;
|
use Hyperf\Rpc\ProtocolManager;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
class RegisterProtocolListener implements ListenerInterface
|
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
|
public function process(object $event): void
|
||||||
{
|
{
|
||||||
$this->protocolManager->registerOrAppend('grpc', [
|
if ($this->container->has(ProtocolManager::class)) {
|
||||||
'path-generator' => PathGenerator::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\Event\Contract\ListenerInterface;
|
||||||
use Hyperf\RpcServer\Event\AfterPathRegister;
|
use Hyperf\RpcServer\Event\AfterPathRegister;
|
||||||
use Hyperf\ServiceGovernance\ServiceManager;
|
use Hyperf\ServiceGovernance\ServiceManager;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
class RegisterServiceListener implements ListenerInterface
|
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
|
public function process(object $event): void
|
||||||
{
|
{
|
||||||
$annotation = $event->annotation;
|
if ($this->container->has(ServiceManager::class)) {
|
||||||
if (! in_array($annotation->protocol, ['grpc'])) {
|
$annotation = $event->annotation;
|
||||||
return;
|
if (! in_array($annotation->protocol, ['grpc'])) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$metadata = $event->toArray();
|
$manager = $this->container->get(ServiceManager::class);
|
||||||
$annotationArray = $metadata['annotation'];
|
|
||||||
unset($metadata['path'], $metadata['annotation'], $annotationArray['name']);
|
$metadata = $event->toArray();
|
||||||
$this->serviceManager->register($annotation->name, $event->path, array_merge($metadata, $annotationArray));
|
$annotationArray = $metadata['annotation'];
|
||||||
|
unset($metadata['path'], $metadata['annotation'], $annotationArray['name']);
|
||||||
|
$manager->register($annotation->name, $event->path, array_merge($metadata, $annotationArray));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ namespace HyperfTest\GrpcServer;
|
|||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Hyperf\Config\Config;
|
use Hyperf\Config\Config;
|
||||||
|
use Hyperf\Contract\ConfigInterface;
|
||||||
use Hyperf\Contract\ContainerInterface;
|
use Hyperf\Contract\ContainerInterface;
|
||||||
use Hyperf\Contract\NormalizerInterface;
|
use Hyperf\Contract\NormalizerInterface;
|
||||||
use Hyperf\Di\ClosureDefinitionCollector;
|
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('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('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(RPCDispatcherFactory::class . '.unit')->andReturn($dispatcher);
|
||||||
|
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config(['grpc_server' => ['rpc' => ['grpc' => ['enable' => true]]]]));
|
||||||
return $container;
|
return $container;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user