Fixed register failed because has the exactly same service. (#1310)

This commit is contained in:
Bi Zhiming 2020-01-21 13:22:05 +08:00 committed by 李铭昕
parent dccaf4212e
commit 2bf162db0d
5 changed files with 104 additions and 12 deletions

View File

@ -7,8 +7,9 @@
## Fixed
- [#1308](https://github.com/hyperf/hyperf/pull/1308) Fixed some missing traslation of validation, like gt, gte, ipv4, ipv6, lt, lte, mimetypes, not_regex, starts_with, uuid.
- [#1291](https://github.com/hyperf/hyperf/pull/1291) Fixed `$_SERVER` has lower keys for super-globals.
- [#1308](https://github.com/hyperf/hyperf/pull/1308) Fixed some missing traslation of validation, like gt, gte, ipv4, ipv6, lt, lte, mimetypes, not_regex, starts_with, uuid.
- [#1310](https://github.com/hyperf/hyperf/pull/1310) Fixed register failed because has the exactly same service.
# v1.1.16 - 2020-01-16

View File

@ -82,16 +82,18 @@ class RegisterServiceListener implements ListenerInterface
try {
$services = $this->serviceManager->all();
$servers = $this->getServers();
foreach ($services as $serviceName => $paths) {
foreach ($paths as $path => $service) {
if (! isset($service['publishTo'], $service['server'])) {
continue;
}
[$address, $port] = $servers[$service['server']];
switch ($service['publishTo']) {
case 'consul':
$this->publishToConsul($address, (int) $port, $service, $serviceName, $path);
break;
foreach ($services as $serviceName => $serviceProtocols) {
foreach ($serviceProtocols as $paths) {
foreach ($paths as $path => $service) {
if (! isset($service['publishTo'], $service['server'])) {
continue;
}
[$address, $port] = $servers[$service['server']];
switch ($service['publishTo']) {
case 'consul':
$this->publishToConsul($address, (int) $port, $service, $serviceName, $path);
break;
}
}
}
}

View File

@ -24,7 +24,11 @@ class ServiceManager
*/
public function register(string $name, string $path, array $metadata): void
{
$this->services[$name][$path] = $metadata;
if (isset($metadata['protocol'])) {
$this->services[$name][$path][$metadata['protocol']] = $metadata;
} else {
$this->services[$name][$path]['default'] = $metadata;
}
}
/**

View File

@ -63,6 +63,43 @@ class RegisterServiceListenerTest extends TestCase
$this->assertArrayHasKey('HTTP', $serviceDefinition['Check']);
}
public function testRegisterForTheSameServiceWithoutTheSameProtocol()
{
$container = $this->createContainer();
$serviceDefinition = [];
$listener = new RegisterServiceListener($container);
$mockAgent = $container->get(ConsulAgent::class);
$mockAgent->shouldReceive('registerService')
->twice()
->with(Mockery::on(function ($args) use (&$serviceDefinition) {
$serviceDefinition[] = $args;
return true;
}))
->andReturn(new ConsulResponse(new Response(200, ['content-type' => 'application/json'])));
$serviceManager = $container->get(ServiceManager::class);
$serviceManager->register('Foo\\FooService', 'Foo/FooService/foo', [
'publishTo' => 'consul',
'server' => 'jsonrpc-http',
'protocol' => 'jsonrpc-http',
]);
$serviceManager->register('Foo\\FooService', 'Foo/FooService/foo', [
'publishTo' => 'consul',
'server' => 'jsonrpc',
'protocol' => 'jsonrpc',
]);
$listener->process((object) []);
$this->assertEquals('Foo\\FooService', $serviceDefinition[0]['Name']);
$this->assertEquals(['Protocol' => 'jsonrpc-http'], $serviceDefinition[0]['Meta']);
$this->assertArrayHasKey('Check', $serviceDefinition[0]);
$this->assertArrayHasKey('HTTP', $serviceDefinition[0]['Check']);
$this->assertEquals('Foo\\FooService', $serviceDefinition[1]['Name']);
$this->assertEquals(['Protocol' => 'jsonrpc'], $serviceDefinition[1]['Meta']);
$this->assertArrayHasKey('Check', $serviceDefinition[1]);
$this->assertArrayHasKey('TCP', $serviceDefinition[1]['Check']);
}
private function createContainer()
{
$container = Mockery::mock(ContainerInterface::class);
@ -85,6 +122,11 @@ class RegisterServiceListenerTest extends TestCase
'host' => '0.0.0.0',
'port' => 9501,
],
[
'name' => 'jsonrpc',
'host' => '0.0.0.0',
'port' => 9502,
],
],
],
]));

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\ServiceGovernance;
use Hyperf\ServiceGovernance\ServiceManager;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ServiceManagerTest extends TestCase
{
public function testRegister()
{
$manager = new ServiceManager();
$manager->register('demo', 'index/demo', [
'protocol' => 'jsonrpc',
]);
$manager->register('demo', 'index/demo', [
'protocol' => 'jsonrpc-http',
]);
$this->assertEquals([
'demo' => [
'index/demo' => [
'jsonrpc' => ['protocol' => 'jsonrpc'],
'jsonrpc-http' => ['protocol' => 'jsonrpc-http'],
],
],
], $manager->all());
}
}