Skip to content

Commit

Permalink
增加RpcClient可以自定义配置的能力 (swoft-cloud/swoft-component#152)
Browse files Browse the repository at this point in the history
* 增加测试使用的配置

* 完善单测配置

* 完善单测

* 增加components配置

* 增加大文本返回时,client正确接收的单测

* 允许用户在tcp.client中自定义client的配置

* 测试配置,增加open_eof_split

* Update server.php

* 更改组件加载配置

* 更改组件加载配置

* Update ServiceConnection.php
  • Loading branch information
limingxinleo authored and huangzhhui committed Aug 8, 2018
1 parent eb7a9d8 commit 20f0161
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 15 deletions.
2 changes: 2 additions & 0 deletions rpc-client/.travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ install:
before_script:
- phpenv config-rm xdebug.ini
- composer update
- php test/server.php &
- sleep 10

script: composer test
10 changes: 8 additions & 2 deletions rpc-client/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
},
"require-dev": {
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^5.7",
"swoft/rpc": "^1.0",
"swoft/rpc-server": "^1.0",
"swoft/task": "^1.0",
"swoft/process": "^1.0",
"swoft/service-governance": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -23,7 +28,8 @@
},
"autoload-dev": {
"psr-4": {
"SwoftTest\\Rpc\\Client\\": "test/Cases"
"SwoftTest\\Rpc\\Client\\": "test/Cases/",
"SwoftTest\\Rpc\\Testing\\": "test/Testing/"
}
},
"repositories": [
Expand Down
13 changes: 13 additions & 0 deletions rpc-client/src/Service/ServiceConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function createConnection()

$address = $this->pool->getConnectionAddress();
$timeout = $this->pool->getTimeout();
$setting = $this->getTcpClientSetting();
$setting && $client->set($setting);

list($host, $port) = explode(':', $address);
if (!$client->connect($host, $port, $timeout)) {
$error = sprintf('Service connect fail errorCode=%s host=%s port=%s', $client->errCode, $host, $port);
Expand Down Expand Up @@ -76,4 +79,14 @@ public function recv(): string
{
return $this->connection->recv();
}

/**
* 返回TCP客户端的配置
*
* @return array
*/
public function getTcpClientSetting(): array
{
return App::getAppProperties()->get('server.tcp.client', []);
}
}
4 changes: 3 additions & 1 deletion rpc-client/src/Service/ServiceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static function loadProxyClass(string $className, string $interfaceClass)
$template .= self::getMethodsTemplate($reflectionMethods);
$template .= "}";

eval($template);
if (!class_exists($className)) {
eval($template);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions rpc-client/test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ PROVIDER_CONSUL_ADDRESS=http://127.0.0.1:82
PROVIDER_CONSUL_TAGS=1,2
PROVIDER_CONSUL_TIMEOUT=2
PROVIDER_CONSUL_INTERVAL=2

TCP_OPEN_EOF_CHECK=true
8 changes: 7 additions & 1 deletion rpc-client/test/Cases/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
*/
class AbstractTestCase extends TestCase
{

protected function tearDown()
{
parent::tearDown();
swoole_timer_after(6 * 1000, function () {
swoole_event_exit();
});
}
}
26 changes: 26 additions & 0 deletions rpc-client/test/Cases/DemoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@

namespace SwoftTest\Rpc\Client;

use Swoft\Rpc\Client\Bean\Collector\ReferenceCollector;
use Swoft\Rpc\Client\Service\ServiceProxy;
use SwoftTest\Rpc\Testing\Clients\DemoServiceClient;
use SwoftTest\Rpc\Testing\Lib\DemoServiceInterface;
use SwoftTest\Rpc\Testing\Pool\Config\DemoServicePoolConfig;

class DemoTest extends AbstractTestCase
{
public function testDemo()
{
$this->assertTrue(true);
}

public function testVersion()
{
$client = bean(DemoServiceClient::class);
$this->assertEquals('1.0.0', $client->version());
}

public function testLongMessageByCo()
{
go(function () {
$client = bean(DemoServiceClient::class);
$string = 'Hi, Agnes! ';
$str = $client->longMessage($string);
$expect = '';
for ($i = 0; $i < 50000; $i++) {
$expect .= $string;
}
$this->assertEquals($expect, $str);
});
}
}
47 changes: 47 additions & 0 deletions rpc-client/test/Testing/Breaker/ServiceBreaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace SwoftTest\Rpc\Testing\Breaker;

use Swoft\Sg\Bean\Annotation\Breaker;
use Swoft\Bean\Annotation\Value;
use Swoft\Sg\Circuit\CircuitBreaker;

/**
* the breaker of default
*
* @Breaker("breaker")
*/
class ServiceBreaker extends CircuitBreaker
{
/**
* @var string 服务名称
*/
public $serviceName = 'breakerService';

/**
* The number of successive failures
* If the arrival, the state switch to open
*
* @Value(name="${config.breaker.default.failCount}")
* @var int
*/
protected $switchToFailCount = 3;

/**
* The number of successive successes
* If the arrival, the state switch to close
*
* @Value(name="${config.breaker.default.successCount}")
* @var int
*/
protected $switchToSuccessCount = 3;

/**
* Switch close to open delay time
* The unit is milliseconds
*
* @Value(name="${config.breaker.default.delayTime}")
* @var int
*/
protected $delaySwitchTimer = 500;
}
26 changes: 26 additions & 0 deletions rpc-client/test/Testing/Clients/DemoServiceClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace SwoftTest\Rpc\Testing\Clients;

use Swoft\Bean\Annotation\Bean;
use Swoft\Rpc\Client\Bean\Annotation\Reference;
use SwoftTest\Rpc\Testing\Lib\DemoServiceInterface;

/**
* Class DemoServiceClient
* @Bean
* @method version
* @method longMessage($string)
*/
class DemoServiceClient
{
/**
* @Reference(name="service.demo", fallback="demoFallback", breaker="breaker")
* @var DemoServiceInterface
*/
protected $demoService;

public function __call($name, $arguments)
{
return $this->demoService->$name(...$arguments);
}
}
27 changes: 27 additions & 0 deletions rpc-client/test/Testing/Fallback/DemoServiceFallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace SwoftTest\Rpc\Testing\Fallback;


use Swoft\Sg\Bean\Annotation\Fallback;
use Swoft\Core\ResultInterface;
use SwoftTest\Rpc\Testing\Lib\DemoServiceInterface;

/**
* Class DemoServiceFallback
* @Fallback("demoFallback")
* @method ResultInterface deferVersion
* @method ResultInterface deferLongMessage($string)
*/
class DemoServiceFallback implements DemoServiceInterface
{
public function version()
{
return 'versionFallback';
}

public function longMessage($string)
{
return 'bigMessageFallBack';
}
}
17 changes: 17 additions & 0 deletions rpc-client/test/Testing/Lib/DemoServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace SwoftTest\Rpc\Testing\Lib;

use Swoft\Core\ResultInterface;

/**
* Interface DemoServiceInterface
* @method ResultInterface deferVersion()
* @method ResultInterface deferLongMessage($string)
*/
interface DemoServiceInterface
{
public function version();

public function longMessage($string);
}
20 changes: 20 additions & 0 deletions rpc-client/test/Testing/Pool/Config/DemoServicePoolConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace SwoftTest\Rpc\Testing\Pool\Config;

use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Value;
use Swoft\Pool\PoolProperties;

/**
* Class DemoServicePoolConfig
* @Bean
* @package SwoftTest\Rpc\Client\Testing\Pool\Config
*/
class DemoServicePoolConfig extends PoolProperties
{
protected $name = 'service.demo';

protected $uri = [
'127.0.0.1:8099'
];
}
21 changes: 21 additions & 0 deletions rpc-client/test/Testing/Pool/DemoServicePool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace SwoftTest\Rpc\Testing\Pool;

use Swoft\Bean\Annotation\Inject;
use Swoft\Bean\Annotation\Pool;
use Swoft\Rpc\Client\Pool\ServicePool as SwoftServicePool;
use SwoftTest\Rpc\Testing\Pool\Config\DemoServicePoolConfig;

/**
* Class DemoServicePool
* @Pool(name="service.demo")
* @package SwoftTest\Rpc\Testing\Pool
*/
class DemoServicePool extends SwoftServicePool
{
/**
* @Inject
* @var DemoServicePoolConfig
*/
protected $poolConfig;
}
30 changes: 30 additions & 0 deletions rpc-client/test/Testing/Services/DemoService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace SwoftTest\Rpc\Testing\Services;

use SwoftTest\Rpc\Testing\Lib\DemoServiceInterface;
use Swoft\Rpc\Server\Bean\Annotation\Service;
use Swoft\Core\ResultInterface;

/**
* Class DemoService
* @Service()
* @method ResultInterface deferVersion()
* @method ResultInterface deferLongMessage($string)
* @package App\Services
*/
class DemoService implements DemoServiceInterface
{
public function version()
{
return '1.0.0';
}

public function longMessage($string)
{
$res = '';
for ($i = 0; $i < 50000; $i++) {
$res .= $string;
}
return $res;
}
}
4 changes: 3 additions & 1 deletion rpc-client/test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
],
]);
$initApplicationContext = new \Swoft\Core\InitApplicationContext();
$initApplicationContext->init();
$initApplicationContext->init();

$server = new \Swoft\Rpc\Server\Rpc\RpcServer();
8 changes: 7 additions & 1 deletion rpc-client/test/config/properties/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version" => '1.0',
'autoInitBean' => true,
'beanScan' => [
'Swoft\\Rpc\\Client\\Testing' => BASE_PATH."/Testing"
'SwoftTest\\Rpc\\Testing' => BASE_PATH . "/Testing",
'Swoft\\Rpc\\Client' => BASE_PATH . '/../src',
],
'I18n' => [
'sourceLanguage' => '@root/resources/messages/',
Expand All @@ -15,5 +16,10 @@
'timeout' => 3000
]
],
'components' => [
'custom' => [
'Swoft\\Rpc\\Client' => BASE_PATH . '/../src',
],
],
'cache' => require dirname(__FILE__) . DS . "cache.php",
];
Loading

0 comments on commit 20f0161

Please sign in to comment.