Download the PHP package amranidev/micro-bus without Composer
On this page you can find all versions of the php package amranidev/micro-bus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download amranidev/micro-bus
More information about amranidev/micro-bus
Files in amranidev/micro-bus
Package micro-bus
Short Description Build your laravel/lumen microservice application with AWS SNS/SQS
License MIT
Informations about the package micro-bus
Build your laravel microservices with micro-bus
What is micro-bus?
MicroBus is a laravel/lumen package for building microservices with event-driven architecture (Pub-Sub) using Amazon web services (SNS/SQS).
What is event-driven microservices?
-
Edmunds: Event-Driven, Serverless, and Cost-Effective Enterprise Message Bus
-
GOTO 2017 • The Many Meanings of Event-Driven Architecture • Martin Fowler
-
GOTO 2018 • Pragmatic Event-Driven Microservices • Allard Buijze
- Building Scalable Applications and Microservices: Adding Messaging to Your Toolbox
Installation.
Laravel.
-
Install the package,
composer require amranidev/micro-bus
. -
Publish the subscriber config file,
php artisan vendor:publish --tag=subscriber
. -
Publish the publisher config file,
php artisan vendor:publish --tag=publisher
. -
Add the subscriber and the publisher environment variables.
- In the
.env
file add.
- Add the Queue connection configuration in
config/queue.php
.
- In the
Congratulations you have successfully installed micro-bus :rocket:
Lumen.
-
Install the package,
composer require amranidev/micro-bus
. -
Add the subscriber and the publisher environment variables.
- In the
.env
file add.
- In the
-
Create
config
folder in the root directory. -
Create
subscriber.php
in the config folder. -
Create
publisher.php
in the config folder. - Create
queue.php
in the config folder.
Copy the same queue.php
from laravel/laravel and add the subscriber configuration into connections
.
- Configure
subscriber
,publisher
,queue
and register the ServiceProvider inbootstrap/app.php
.
Congratulations you have successfully installed micro-bus in lumen :rocket:
Usage.
Note that you need to define your Topics in SNS, create the queues in SQS and tie SNS topics to the queues, AWS configuration is not included in this documentation.
After installing the package in both laravel nodes, now, let's make them talk to each other. Basically, they can be both subscribers or publishers at the same time, or, you can make one as subscriber and the other as publisher, it is entirely up to you.
Let's say we have a laravel app called A and a lumen microservice called B.
When a user is created in the A, B needs to add the user's email to the mailing list,
In this case A is the publisher and B is the subscriber.
Note that the SNS configuration needs to be added to A and the SQS needs to be added in B, see installation steps above.
Setup Laravel app (Microservice A).
First of all, let's add the topic that we've created in AWS to A,
we need to specify an event name user_created
and the AWS TopicArn in config/publisher.php
After we configured the SNS in A, we need to add the functionality, when a user is created, we need to publish a message to SNS, to do so:
Alternatively you can publish messages with:
-
Service container:
- Artisan command:
php artisan bus:publish <data> <eventOrTopicname>
Set up Lumen (Microservice B).
Now, let's create the subscriber class which will be listening for A in B.
php artisan make:subscriber UserCreated
This command will scaffold a job class for you in app/Subscribers/
.
As you can see, the job created has $payload
property, it
is simply the piece of data that A will be publishing, in our case will be the $user
.
The Handle method is the one responsible for executing the job coming form SQS,
The last thing we need to do in B is to tie this class to the TopicArn in config/subscriber.php
.
And run the queue:work
command, php artisan queue:work <connection-name>
,
Contributing.
Thank you for considering contributing to this project! The contribution guide can be found in Contribution guide.
Alternative (Google Cloud Platform).
Testing.
- Pull the localstack/localstack docker image.
docker pull localstack/localstack
Create a docker-compose.yml
file.
-
Run
docker-compose run
to start localstack, if you're using mac runTMPDIR=/private$TMPDIR docker-compose up
. - Finally run
phpunit
.