搜尋
首頁後端開發php教程在 PHP 中實作事件驅動架構:深入研究事件溯源與 CQRS

Implementing Event-Driven Architectures in PHP: A Deep Dive into Event Sourcing and CQRS

事件驅動架構(EDA)專注於解耦系統,並透過回應事件使系統更加靈活、可擴展和可維護。在 PHP 中,EDA 中經常使用的兩個重要模式是事件溯源命令查詢職責分離(CQRS)。這是使用 PHP 實作它們的逐步指南,以及實作範例。

概念概述

1. 事件溯源

  • 不只是將應用程式的最終狀態保存在資料庫中,對應用程式狀態的每個變更(事件)都會被儲存。
  • 範例:如果您有訂單系統,您不只儲存最新的訂單狀態,而是儲存訂單上的每個操作,例如「訂單已建立」、「商品已新增」、「訂單已付款”等

2.CQRS

  • CQRS 將讀取(查詢)和寫入(指令)操作分開。這兩個模型可能會單獨發展,寫入模型專注於業務邏輯和驗證,讀取模型專注於資料表示。
  • 範例:對於像電子商務網站這樣的複雜系統,下訂單(寫入)的邏輯可以與獲取訂單詳細資訊(讀取)分開。

架構流程

  1. 指令

    • 指令是請求狀態變更的動作(例如,「PlaceOrder」、「AddItemToOrder」)。
    • 指令由執行業務邏輯並發出事件的 指令處理程序 處理。
  2. 活動

    • 處理指令後,會引發一個事件(例如「OrderPlaced」、「ItemAdded」),表示發生了重要的事情。
    • 事件是不可變的,它們會觸發系統其他部分的操作,例如更新讀取模型或通知外部系統。
  3. 讀取模型:

    • 讀取模型透過對事件做出反應來保持最新。它針對讀取操作進行了最佳化,並且可能具有與寫入模型不同的架構。

逐步範例:訂單系統

第 1 步:設定項目

建立目錄結構:

event-driven-php/
    ├── src/
    │   ├── Commands/
    │   ├── Events/
    │   ├── Handlers/
    │   ├── Models/
    │   └── ReadModels/
    ├── tests/
    └── vendor/

安裝依賴項(例如 symfony/event-dispatcher):

composer require symfony/event-dispatcher

第 2 步:定義指令

指令代表改變狀態的動作。範例:PlaceOrderCommand.php。

// src/Commands/PlaceOrderCommand.php
class PlaceOrderCommand
{
    public string $orderId;
    public string $customerId;

    public function __construct(string $orderId, string $customerId)
    {
        $this->orderId = $orderId;
        $this->customerId = $customerId;
    }
}

第 3 步:建立事件

事件描述了系統中發生的事情。範例:OrderPlacedEvent.php。

// src/Events/OrderPlacedEvent.php
class OrderPlacedEvent
{
    public string $orderId;
    public string $customerId;

    public function __construct(string $orderId, string $customerId)
    {
        $this->orderId = $orderId;
        $this->customerId = $customerId;
    }
}

第 4 步:命令處理程序

命令處理程序執行實際的業務邏輯並引發事件。範例:PlaceOrderHandler.php。

// src/Handlers/PlaceOrderHandler.php
use Symfony\Component\EventDispatcher\EventDispatcher;

class PlaceOrderHandler
{
    private EventDispatcher $eventDispatcher;

    public function __construct(EventDispatcher $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    public function handle(PlaceOrderCommand $command)
    {
        // Business logic (e.g., check stock, validate order)

        // Emit the event
        $event = new OrderPlacedEvent($command->orderId, $command->customerId);
        $this->eventDispatcher->dispatch($event, 'order.placed');
    }
}

第 5 步:事件處理程序(投影資料以讀取模型)

事件處理程序偵聽特定事件並更新讀取模型。範例:OrderProjection.php。

// src/ReadModels/OrderProjection.php
class OrderProjection
{
    private array $orders = [];

    public function onOrderPlaced(OrderPlacedEvent $event)
    {
        // Save or update read model with necessary data
        $this->orders[$event->orderId] = [
            'orderId' => $event->orderId,
            'customerId' => $event->customerId,
            'status' => 'placed'
        ];
    }

    public function getOrder(string $orderId)
    {
        return $this->orders[$orderId] ?? null;
    }
}

第 6 步:將其放在一起

use Symfony\Component\EventDispatcher\EventDispatcher;

// Bootstrapping the system
$dispatcher = new EventDispatcher();
$orderProjection = new OrderProjection();

// Register event listeners
$dispatcher->addListener('order.placed', [$orderProjection, 'onOrderPlaced']);

// Create the command and command handler
$command = new PlaceOrderCommand('123', 'cust_001');
$handler = new PlaceOrderHandler($dispatcher);

// Handle the command (Place the order)
$handler->handle($command);

// Query the read model for the order
$order = $orderProjection->getOrder('123');
print_r($order);

輸出:

Array
(
    [orderId] => 123
    [customerId] => cust_001
    [status] => placed
)

步驟7:事件儲存(可選)

對於完整的事件來源,您還需要實作事件儲存將事件儲存到資料庫。

class EventStore
{
    private array $storedEvents = [];

    public function append(Event $event)
    {
        $this->storedEvents[] = $event;
    }

    public function getEventsForAggregate(string $aggregateId): array
    {
        return array_filter($this->storedEvents, function($event) use ($aggregateId) {
            return $event->aggregateId === $aggregateId;
        });
    }
}

逐部分細分

  1. 指令建立:代表使用者更改某些內容的意圖。
  2. 指令處理:處理指令和引發事件的業務邏輯。
  3. 事件發射:成功處理指令後引發的事件。
  4. 事件處理:將事件資料投影到讀取模型中以最佳化查詢。
  5. CQRS分離:命令模型專注於領域邏輯,而查詢模型針對快速查找進行了最佳化。
  6. 事件儲存:可選地,保留事件以在需要時重播狀態。

結論

此範例示範了 CQRSEvent Sourcing 在 PHP 中的簡單應用。透過這些模式,您可以建立可擴展且可維護的系統,同時提供強大的可審核性和靈活的讀取/寫入處理。該架構可以隨著額外的投影、更複雜的事件處理以及訊息佇列或第三方通知等外部整合而成長。

以上是在 PHP 中實作事件驅動架構:深入研究事件溯源與 CQRS的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)