Skip to content

Latest commit

 

History

History
177 lines (123 loc) · 13.1 KB

File metadata and controls

177 lines (123 loc) · 13.1 KB

Laravel Microservices Full Course | Event Driven Architecture

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to the Microservices Example

Summary: The app demonstrates a simple microservices setup using Laravel for two backends (admin and main), each with its own database. They communicate via RabbitMQ events, with one internal API call from main to admin. Frontends can be in Angular, React, or Vue, but the focus is on the backend. The admin handles product creation, while the main app handles likes, syncing data through events.

Key Takeaway/Example: When liking a product in the main app, it triggers a RabbitMQ event to update likes in the admin app. Creating a product in admin fires an event to replicate it in main. There's also an internal call to get a random user from admin for likes.

Ask AI: Introduction to the Microservices Example

Setting Up the Admin Laravel App with Docker

Summary: Start with a new Laravel 8 project named 'admin'. Install barryvdh/laravel-ide-helper for better IDE support. Create a Dockerfile based on PHP 7.4, installing necessary packages and Composer. Set up docker-compose.yaml with services for the app and MySQL database, mapping ports and volumes for persistence.

Key Takeaway/Example: Run docker-compose up to build and start containers. Connect to the database using localhost:33060, root/root credentials. Use docker-compose exec admin sh to enter the container for running commands like migrations.

FROM php:7.4-fpm

# Install packages and extensions...
RUN apt-get update && apt-get install -y ...
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /app
COPY . /app
RUN composer install

EXPOSE 8000
CMD ["php", "artisan", "serve", "--host=0.0.0.0"]

Ask AI: Setting Up the Admin Laravel App with Docker

Database Migrations, Factories, and Seeders

Summary: Modify default migrations: Simplify users table to just an ID, remove password_resets, disable failed_jobs timestamps. Create products table with title, image, and likes (default 0). Set up factories for users and products using Faker. Create seeders to generate 20 users and 10 products, then run them inside the Docker container.

Key Takeaway/Example: In the User model, set $timestamps = false; and remove unnecessary traits. For products, use guarded = [] for mass assignment.

// Product migration
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('image');
    $table->unsignedInteger('likes')->default(0);
    $table->timestamps();
});

Ask AI: Database Migrations, Factories, and Seeders

Creating CRUD API for Products

Summary: Build a ProductController with index, show, store, update, and destroy methods. Use apiResource in routes/api.php to map all CRUD routes. Test endpoints with HTTP requests (e.g., POST /api/products with title and image). Handle responses with appropriate HTTP status codes like 201 for created.

Key Takeaway/Example: In store: Product::create($request->only(['title', 'image'])); Return response()->json($product, Response::HTTP_CREATED);.

// routes/api.php
Route::apiResource('products', ProductController::class);

Ask AI: Creating CRUD API for Products

Setting Up the Main Laravel App

Summary: Create a new Laravel project 'main' similarly, with its own Docker setup but different ports. Modify products migration to use unsignedBigInteger for ID (no auto-increment) to sync with admin. Create Product model and controller for basic index.

Key Takeaway/Example: Products table: $table->unsignedBigInteger('id')->primary(); to match admin IDs via events.

Ask AI: Setting Up the Main Laravel App

Configuring RabbitMQ for Event Communication

Summary: Install vladimir-yuldashev/laravel-queue-rabbitmq in both apps. Add RabbitMQ config to config/queue.php and .env (using CloudAMQP free tier). Create a test job and command to fire events, handle in EventServiceProvider. Run php artisan queue:work to listen.

Key Takeaway/Example: Dispatch: TestJob::dispatch(); Handle: echo 'Event has been handled' . PHP_EOL;.

Ask AI: Configuring RabbitMQ for Event Communication

Implementing Product Creation Event

Summary: Create ProductCreated job in both apps. Dispatch from admin's store method with product data as array. In main's handle, create product with same ID, title, image.

Key Takeaway/Example: Dispatch: ProductCreated::dispatch($product->toArray()); Handle: Product::create(['id' => $data['id'], 'title' => $data['title'], 'image' => $data['image']]);.

Ask AI: Implementing Product Creation Event

Handling Product Update and Delete Events

Summary: Create ProductUpdated and ProductDeleted jobs. Dispatch from admin's update/destroy with data. In main, find product by ID and update or delete accordingly.

Key Takeaway/Example: For update: $product->update(['title' => $data['title'], 'image' => $data['image']]); For delete: Product::destroy($data['id']);.

Ask AI: Handling Product Update and Delete Events

Adding Like Functionality with Internal API Call

Summary: In main, add UserController with random method to get a random user ID from admin. In ProductController, add like method: Call admin's /user endpoint internally using Http::get, then create pivot entry in product_user table to record like, with unique constraint to prevent duplicates.

Key Takeaway/Example: Internal call: Http::get('http://host.docker.internal:8000/api/user')->json()['user_id']; Use try-catch for duplicate likes.

// Migration for product_user
Schema::create('product_user', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('product_id');
    $table->unique(['user_id', 'product_id']);
});

Ask AI: Adding Like Functionality with Internal API Call

Implementing Product Liked Event

Summary: Create ProductLiked job. Dispatch from main's like method with product_user data. In admin's handle, find product and increment likes count.

Key Takeaway/Example: Dispatch: ProductLiked::dispatch($productUser->toArray()); Handle: $product->likes++; $product->save();.

Ask AI: Implementing Product Liked Event

Configuring Separate Queues

Summary: To avoid conflicts, define separate queues in .env (e.g., QUEUE_NAME=admin_queue for admin). Dispatch jobs to specific queues using ::dispatch()->onQueue('main_queue');. Restart queue workers.

Key Takeaway/Example: Ensures events are processed by the correct app without overlap.

Ask AI: Configuring Separate Queues

Automating Queue Workers with Docker

Summary: Add a separate service in docker-compose.yaml for the queue worker, using the same Dockerfile but with command php artisan queue:work. Remove serve from Dockerfile and add as command to app service. This runs queue:work automatically on startup.

Key Takeaway/Example: docker-compose service: command: php artisan queue:work and depends_on the DB.

Ask AI: Automating Queue Workers with Docker


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: