Package Data | |
---|---|
Maintainer Username: | butschster |
Maintainer Contact: | butschster@gmail.com (Pavel Buchnev) |
Package Create Date: | 2020-10-18 |
Package Last Update: | 2021-01-21 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-17 03:08:29 |
Package Statistics | |
---|---|
Total Downloads: | 765 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 5 |
Total Watchers: | 2 |
Total Forks: | 2 |
Total Open Issues: | 0 |
This Package helps you to use Lumen framework as a microservice through AMQP without extra knowledge. Just install package, create Exchange Point class and start exchanging information between services through RabbitMQ.
For serialization this package uses JMS\Serializer.
P.S. You can use this service for testing https://www.cloudamqp.com. They have free plan.
From the command line run
composer require butschster/lumen-microservice
$app->register(Butschster\Exchanger\Providers\ExchangeServiceProvider::class);
config
directory to your Lumen app and register them.$app->configure('amqp');
$app->configure('microservice');
MICROSERVICE_NAME=...
MICROSERVICE_VERSION=1.0.0
RABBITMQ_HOST=...
RABBITMQ_USERNAME=...
RABBITMQ_PASSWORD=...
RABBITMQ_VHOST=...
This point will use for receiving request from other services and for sending responses.
namespace App\Exchange\Point;
use Butschster\Exchanger\Contracts\Exchange\Point as PointContract;
class Point implements PointContract
{
public function getName(): string
{
return 'com.test';
}
/**
* // subject: com.test.action.test
* @subject action.test
*/
public function testSubject(IncomingRequest $request, LoggerInterface $logger)
{
$logger->info(
sprintf('info [%s]:', $request->getSubject()),
[$payload]
);
// Response
$payload = new ...;
$request->sendResponse($payload);
}
/**
* // subject: com.test.action.test1
* @subject action.test1
*/
public function anotherTestSubject(IncomingRequest $request, LoggerInterface $logger)
{
$payload = new ...;
$request->sendResponse($payload);
}
}
Then register this point
use App\Exchange\Point;
use Illuminate\Support\ServiceProvider;
use Butschster\Exchanger\Contracts\Exchange\Point as PointContract;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(PointContract::class, Point::class);
}
}
use Butschster\Exchanger\Console\Commands\RunMicroservice;
class Kernel extends ConsoleKernel
{
protected $commands = [
RunMicroservice::class
];
}
This single command will run your microservice and start listening to commands registered in your exchange point.
php artisan service:run
Supervisor is a process monitor for the Linux operating system, and will automatically restart your horizon process if it fails. To install Supervisor on Ubuntu, you may use the following command:
sudo apt-get install supervisor
Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let's create a microservice.conf file that starts and monitors a process:
[program:microservice]
process_name=%(program_name)s
command=php /var/www/app.com/artisan service:run
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/app.com/storage/logs/microservice.log
stopwaitsecs=3600
Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start microservice
For requesting information from another service you should use ExchangeManager
use Butschster\Exchanger\Contracts\ExchangeManager;
class UserController {
public function getUser(ExchangeManager $manager, int $userId)
{
$request = new GetUserByIdPayload();
$request->userId = $userId;
$user = $manager->request('com.test.action.test', $request);
...
}
}
If you want to send event to all services you should use ExchangeManager
use Illuminate\Http\Request;
use Butschster\Exchanger\Contracts\ExchangeManager;
class UserController {
public function update(ExchangeManager $manager, Request $request, User $user)
{
$payload = new UserPayload();
$data = $request->validate(...);
$user->update($data);
$payload->id = $user->id;
$payload->username = $user->username;
...
$manager->broadcast('com.user.event.updated', $payload);
}
}