From 2bf162db0dd31cfd0eeebe47e27727e07f429ad5 Mon Sep 17 00:00:00 2001 From: Bi Zhiming Date: Tue, 21 Jan 2020 13:22:05 +0800 Subject: [PATCH] Fixed register failed because has the exactly same service. (#1310) --- CHANGELOG.md | 3 +- .../src/Listener/RegisterServiceListener.php | 22 +++++----- src/service-governance/src/ServiceManager.php | 6 ++- .../Listener/RegisterServiceListenerTest.php | 42 ++++++++++++++++++ .../tests/ServiceManagerTest.php | 43 +++++++++++++++++++ 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 src/service-governance/tests/ServiceManagerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 67f78c21e..4a079fa38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/service-governance/src/Listener/RegisterServiceListener.php b/src/service-governance/src/Listener/RegisterServiceListener.php index 9c1e57698..4cb1cd757 100644 --- a/src/service-governance/src/Listener/RegisterServiceListener.php +++ b/src/service-governance/src/Listener/RegisterServiceListener.php @@ -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; + } } } } diff --git a/src/service-governance/src/ServiceManager.php b/src/service-governance/src/ServiceManager.php index b71a869f5..949fa6243 100644 --- a/src/service-governance/src/ServiceManager.php +++ b/src/service-governance/src/ServiceManager.php @@ -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; + } } /** diff --git a/src/service-governance/tests/Listener/RegisterServiceListenerTest.php b/src/service-governance/tests/Listener/RegisterServiceListenerTest.php index 6e948d973..771e0b697 100644 --- a/src/service-governance/tests/Listener/RegisterServiceListenerTest.php +++ b/src/service-governance/tests/Listener/RegisterServiceListenerTest.php @@ -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, + ], ], ], ])); diff --git a/src/service-governance/tests/ServiceManagerTest.php b/src/service-governance/tests/ServiceManagerTest.php new file mode 100644 index 000000000..be98219c8 --- /dev/null +++ b/src/service-governance/tests/ServiceManagerTest.php @@ -0,0 +1,43 @@ +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()); + } +}