Another jet client for Hyperf
Fund package maintenance!
huangdijia
hdj.me/sponsors
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^6.0|^7.0
- markrogoyski/math-php: ^2.0
Requires (Dev)
- grpc/grpc: ~1.30.0
- huangdijia/php-coding-standard: ^2.0
- hyperf/di: ^3.0
- hyperf/guzzle: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^10.0|^11.0
Suggests
- swoole: >=4.6.0
- v4.2.0
- v4.1.0
- dev-main / 4.0.x-dev
- v4.0.0
- 3.x-dev
- v3.7.0
- v3.6.0
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.3
- 2.x-dev
- v2.7.0
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.3
- v2.2.2
- v2.2.1
- 1.x-dev
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.0
- v1.3.0-beta.11
- v1.3.0-beta.10
- v1.3.0-beta.9
- v1.3.0-beta.8
- v1.3.0-beta.7
- v1.3.0-beta.6
- v1.3.0-beta.5
- v1.3.0-beta.4
- v1.3.0-beta.3
- v1.3.0-beta.2
- v1.3.0-beta.1
- 1.2.x-dev
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
This package is auto-updated.
Last update: 2024-11-29 12:08:50 UTC
README
Another jet client for Hyperf
Installation
Composer
composer require "friendsofhyperf/jet:^4.0"
QuickStart
Register with metadata
use FriendsOfHyperf\Jet\Metadata;
use FriendsOfHyperf\Jet\ServiceManager;
use FriendsOfHyperf\Jet\Registry\ConsulRegistry;
use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter;
$metadata = (new Metadata('CalculatorService'))
->withTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502))
// or
->setRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500']))
;
ServiceManager::register('CalculatorService', $metadata);
Register default registry
use FriendsOfHyperf\Jet\RegistryManager;
use FriendsOfHyperf\Jet\Registry\ConsulRegistry;
RegistryManager::register(RegistryManager::DEFAULT, new ConsulRegistry(['uri' => $uri, 'timeout' => 1]));
In Laravel project, Add to
boot()
inApp/Providers/AppServiceProvider.php
Call RPC method
Call by ClientFactory
use FriendsOfHyperf\Jet\ClientFactory;
$client = ClientFactory::create('CalculatorService');
var_dump($client->add(1, 20));
Call by ClientFactory Using Grpc
use FriendsOfHyperf\Jet\ClientFactory;
return ClientFactory::create(function() {
return (new Metadata('CalculatorService'))
->withPacker(new GrpcPacker())
->withPathGenerator(new GrpcPathGenerator())
// If use consul next config is necessary
->withRegistry(RegistryManager::get(RegistryManager::DEFAULT))
->withTransporterConfig([
'path' => 'calculator.CalCulator',
])
->withProtocol('grpc')
->withTimeout(10)
// If not use consul,directly use GrpcTransporter
->withTransporter(new GrpcTransporter('127.0.0.1', 9502, [
'path' => 'calculator.CalCulator',
]));
});
Call by custom client
use FriendsOfHyperf\Jet\Client;
use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter;
use FriendsOfHyperf\Jet\Registry\ConsulRegistry;
/**
* @method int add(int $a, int $b)
*/
class CalculatorService extends Client
{
public function __construct($service = 'CalculatorService')
{
$metadata = (new Metadata($service))
// Custom transporter
->withTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502))
// Custom registry
->withRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500']));
parent::__construct($metadata);
}
}
$service = new CalculatorService;
var_dump($service->add(3, 10));
Call by custom facade
use FriendsOfHyperf\Jet\Facade;
use FriendsOfHyperf\Jet\ClientFactory;
/**
* @method static int add(int $a, int $b)
*/
class Calculator extends Facade
{
protected static function getFacadeAccessor()
{
// return ClientFactory::create('CalculatorService');
return 'CalculatorService';
}
}
var_dump(Calculator::add(rand(0, 100), rand(0, 100)));
Coroutine support in Hyperf
- Aspect
<?php
namespace App\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Guzzle\ClientFactory;
class GuzzleHttpTransporterAspect extends AbstractAspect
{
public array $classes = [
'FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter::getClient',
];
protected ClientFactory $clientFactory;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$instance = $proceedingJoinPoint->getInstance();
$config = (function () { return $this->config; })->call($instance);
return $this->clientFactory->create($config);
}
}
- Config
config/autoload/aspects.php
<?php
return [
'App\Aspect\GuzzleHttpTransporterAspect',
];