andrew-svirin / lumen-microservices
此软件包尚未发布版本,且相关信息较少。
README
Laravel Lumen 中的微服务同步请求
支持 Laravel 6.0+ 和 Laravel Lumen 6.0+
为了配置,请将 config/microsystem.php
复制到项目目录,并在文件内设置微服务。
Lumen 6.0+ 的安装
在 bootstrap/app.php
中注册服务提供者
$app->register(\AndrewSvirin\Microsystem\MicrosystemServiceProvider::class);
在 bootstrap/app.php
中注册设置文件 config/microsystem.php
$app->configure('microsystem');
在 bootstrap/app.php
中可选使用外观
$app->withFacades(true, [ AndrewSvirin\Microsystem\Facades\Microsystem::class => 'Microsystem', ]);
使用方法
例如,我们需要与 2 个远程微服务进行交互,其中我们需要获取用户的访问令牌。此示例在测试文件 MicrosystemTest.php
中。
$accessToken = AndrewSvirin\Microsystem\Facades\Microsystem:: call(\AndrewSvirin\Microsystem\Data\Requests\Demo1\Request1::create([ 'password' => 'password', ])) // Get password hash. ->next(\AndrewSvirin\Microsystem\Data\Requests\Demo2\Request1::create([ 'password_hash' => function (\AndrewSvirin\Microsystem\Services\ChainResponseCollection $responses) { return $responses->get(0)->getData()['password_hash']; }, 'email' => 'email', ])) // Get user login data using previous responses. ->next(\AndrewSvirin\Microsystem\Data\Requests\Demo1\Request2::create([ 'id' => function (\AndrewSvirin\Microsystem\Services\ChainResponseCollection $responses) { return $responses->get(1)->getData()['id']; }, 'name' => function (\AndrewSvirin\Microsystem\Services\ChainResponseCollection $responses) { return $responses->get(1)->getData()['name']; }, ])) // Get user access token using previous responses. ->getData();
可以使用过程封装链式逻辑,请参阅示例 AndrewSvirin\Microsystem\Tests\Data\Procedures\Procedure1。
服务配置
return [ /* |-------------------------------------------------------------------------- | Microservices definitions. |-------------------------------------------------------------------------- */ 'services' => [ /* |-------------------------------------------------------------------------- | Define Demo 1 microservice configuration. |-------------------------------------------------------------------------- */ 'demo-1' => [ 'format' => 'json', 'server' => [ 'scheme' => 'http', 'host' => 'demo-1.loc', 'port' => 80, 'vhost' => '/api/', ], 'client' => [ 'http_errors' => false, 'decode_content' => false, 'verify' => false, 'cookies' => false, ], ], /* |-------------------------------------------------------------------------- | Define Demo 2 microservice configuration. |-------------------------------------------------------------------------- */ 'demo-2' => [ 'format' => 'json', 'server' => [ 'scheme' => 'http', 'host' => 'demo-2.loc', 'port' => 8080, 'vhost' => '/api/', ], 'client' => [ 'http_errors' => false, 'decode_content' => false, 'verify' => false, 'cookies' => false, ], ], ], ];