Merge pull request #325 from wenbinye/feature-add-service-govenance-test

check consul service once
This commit is contained in:
黄朝晖 2019-08-03 13:43:03 +08:00 committed by GitHub
commit 5c4c1d9ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 111 additions and 1 deletions

View File

@ -1,5 +1,9 @@
# v1.0.10 - TBD
## Fixed
- [#325](https://github.com/hyperf-cloud/hyperf/pull/325) Fixed consul service check the same service registration status more than one times.
# v1.0.9 - 2019-08-03
## Added

View File

@ -184,6 +184,7 @@
"HyperfTest\\Pool\\": "src/pool/tests/",
"HyperfTest\\Process\\": "src/process/tests/",
"HyperfTest\\Redis\\": "src/redis/tests/",
"HyperfTest\\ServiceGovernance\\": "src/service-governance/tests/",
"HyperfTest\\Task\\": "src/task/tests/",
"HyperfTest\\Utils\\": "src/utils/tests/",
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/"

View File

@ -23,6 +23,7 @@
<directory suffix="Test.php">./src/event/tests</directory>
<directory suffix="Test.php">./src/guzzle/tests</directory>
<directory suffix="Test.php">./src/http-server/tests</directory>
<directory suffix="Test.php">./src/service-governance/tests</directory>
<directory suffix="Test.php">./src/logger/tests</directory>
<directory suffix="Test.php">./src/model-cache/tests</directory>
<directory suffix="Test.php">./src/paginator/tests</directory>

View File

@ -33,6 +33,7 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\ServiceGovernance\\": "tests/"
}
},
"config": {

View File

@ -55,6 +55,11 @@ class RegisterServiceListener implements ListenerInterface
'component' => 'service-governance',
];
/**
* @var array
*/
private $registeredServices;
public function __construct(ContainerInterface $container)
{
$this->consulAgent = $container->get(ConsulAgent::class);
@ -75,6 +80,7 @@ class RegisterServiceListener implements ListenerInterface
*/
public function process(object $event)
{
$this->registeredServices = [];
$continue = true;
while ($continue) {
try {
@ -142,6 +148,7 @@ class RegisterServiceListener implements ListenerInterface
}
$response = $this->consulAgent->registerService($requestBody);
if ($response->getStatusCode() === 200) {
$this->registeredServices[$serviceName][$service['protocol']][$address][$port] = true;
$this->logger->info(sprintf('Service %s[%s]:%s register to the consul successfully.', $serviceName, $path, $nextId), $this->defaultLoggerContext);
} else {
$this->logger->warning(sprintf('Service %s register to the consul failed.', $serviceName), $this->defaultLoggerContext);
@ -183,6 +190,9 @@ class RegisterServiceListener implements ListenerInterface
private function isRegistered(string $name, string $address, int $port, string $protocol): bool
{
if (isset($this->registeredServices[$name][$protocol][$address][$port])) {
return true;
}
$response = $this->consulAgent->services();
if ($response->getStatusCode() !== 200) {
$this->logger->warning(sprintf('Service %s register to the consul failed.', $name), $this->defaultLoggerContext);
@ -202,6 +212,7 @@ class RegisterServiceListener implements ListenerInterface
$service['Meta']['Protocol'],
]);
if ($currentTag === $tag) {
$this->registeredServices[$name][$protocol][$address][$port] = true;
return true;
}
}

View File

@ -13,7 +13,6 @@ declare(strict_types=1);
namespace Hyperf\ServiceGovernance\Register;
use Hyperf\Consul\Agent;
use Hyperf\Consul\Client;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Guzzle\ClientFactory;
use Psr\Container\ContainerInterface;

View File

@ -0,0 +1,93 @@
<?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-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\ServiceGovernance\Listener;
use GuzzleHttp\Psr7\Response;
use Hyperf\Config\Config;
use Hyperf\Consul\ConsulResponse;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Logger\Logger;
use Hyperf\ServiceGovernance\Listener\RegisterServiceListener;
use Hyperf\ServiceGovernance\Register\ConsulAgent;
use Hyperf\ServiceGovernance\ServiceManager;
use Mockery;
use Monolog\Handler\StreamHandler;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
/**
* @internal
* @coversNothing
*/
class RegisterServiceListenerTest extends TestCase
{
public function testRegisterOnceForTheSameService()
{
$container = $this->createContainer();
$serviceDefinition = null;
$listener = new RegisterServiceListener($container);
$mockAgent = $container->get(ConsulAgent::class);
$mockAgent->shouldReceive('registerService')
->once()
->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/bar', [
'publishTo' => 'consul',
'server' => 'jsonrpc-http',
'protocol' => 'jsonrpc-http',
]);
$listener->process((object) []);
$this->assertEquals('Foo\\FooService', $serviceDefinition['Name']);
$this->assertEquals(['Protocol' => 'jsonrpc-http'], $serviceDefinition['Meta']);
$this->assertArrayHasKey('Check', $serviceDefinition);
$this->assertArrayHasKey('HTTP', $serviceDefinition['Check']);
}
private function createContainer()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('get')->with(ConsulAgent::class)
->andReturn($mockAgent = Mockery::mock(ConsulAgent::class));
$mockAgent->shouldReceive('services')
->andReturn(new ConsulResponse(new Response(200, ['content-type' => 'application/json'], '{}')));
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)
->andReturn(new Logger('App', [
new StreamHandler('/dev/null'),
]));
$container->shouldReceive('get')->with(ServiceManager::class)
->andReturn(new ServiceManager());
$container->shouldReceive('get')->with(ConfigInterface::class)
->andReturn(new Config([
'server' => [
'servers' => [
[
'name' => 'jsonrpc-http',
'host' => '0.0.0.0',
'port' => 9501,
],
],
],
]));
return $container;
}
}