Developing Microservices by using Lumen & RabbitMQ — Part 1

What is RabbitMQ?

RabbitMQ Fundamentals:

Consumer:

Message:

Exchange:

Queue:

Types of exchanges in RabbitMQ:

Fanout

Direct

Topic

Headers

What we are going to do?

Let’s start!

  • Create a lumen project and name it E-Store.
composer create-project — prefer-dist laravel/lumen estore
  • After installation is completed, in the bootstrap/app.php file uncomment the line
$app→register(App\Providers\EventServiceProvider::class);
  • Then create two migrations for Order and User Model
  • run the below command:
php artisan make:migration create_users_table
  • Then put the below code in the migration file
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
  • Run the below command:
php artisan make:migration create_orders_table
  • Then put the below code in the migration file
$table->id();
$table->unsignedInteger('user_id')->default(1);
$table->string('product_name')->default('item 1');
$table->decimal('quantity')->default(1);
$table->decimal('total_price')->default(1);
$table->timestamps();
  • The migration for failed jobs also requires so run following command
php artisan queue:failed-table
  • And finally, run:
php artisan migrate
  • populates the tables with some dummy data or you could create a seeder for this task.
  • Add the below code in the $listen array
\App\Events\OrderCreated::class => [
\App\Listeners\OrderCreatedListener::class,
],
  • Create a class named OrderCreated (extends Event class) in the app/Events folder and put the below code in it
class OrderCreated extends Event
{
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
}
  • Then we should create a listener class for our event. In the app/Listeners create one class and name it OrderCreatedListener and put the below code in that class.
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;
class OrderCreatedListener implements ShouldQueue
{

public function __construct()
{

}

public function handle($event)
{
Log::info('Order created on estore:' . json_encode($event));
}
}
namespace App\Console\Commands;
use App\Order;
use Illuminate\Console\Command;
class CreateOrder extends Command
{
protected $signature = 'order:create';

protected $description = 'Create an order';

public function __construct()
{
parent::__construct();
}

public function handle()
{
$order = Order::with('user')->inRandomOrder()->first();
event(new \App\Events\OrderCreated($order));
return 0;
}
}
protected $commands = [
CreateOrder::class
];
  • Now in the bootstrapp/app.php file, add the below line
$app→configure('queue');
php artisan queue:work
  • Then run the below code in another terminal:
php artisan order:create 

--

--

--

Senior Backend Developer at Ucraft

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Top five benefits of using low-code app builder platforms

CSS Weekly Issue 10

Development Environments = Production

Welcome CSS Weekly

How to set up an SFTP server on Rocky Linux 8

Smick: a smart brick for easy IoT

Certified Kubernetes Administrator (CKA), Application Developer (CKAD) and Security Specialist…

Golang Weekly Issue 29

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Behzad Babaei

Behzad Babaei

Senior Backend Developer at Ucraft

More from Medium

How to Use and Make Foriegn Keys in PostgreSQL

How We Migrate Legacy Authentication OAuth 1.0 Using JWT And Redis

How to make your database highly available (HA)

Optimistic vs. Pessimistic Database Locking