溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Laravel基于reset怎么實現(xiàn)分布式事務

發(fā)布時間:2021-11-09 16:08:41 來源:億速云 閱讀:168 作者:iii 欄目:編程語言

這篇文章主要講解了“Laravel基于reset怎么實現(xiàn)分布式事務”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Laravel基于reset怎么實現(xiàn)分布式事務”吧!

                           

快速預覽

安裝laravel5.5 - laravel8之間的版本,然后安裝快速服務化的package

composer require windawake/laravel-reset-transaction dev-master

首先創(chuàng)建ResetProductController.php控制器,創(chuàng)建ResetProductModel.php模型,創(chuàng)建reset_transaction和reset_product兩張數(shù)據(jù)庫表。這些操作只需要執(zhí)行下面命令全部完成

php artisan resetTransact:create-examples

phpunit.xml增加testsuite Transaction

<?xml version="1.0" encoding="UTF-8"?><phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true">
    <testsuites>
        ......        <testsuite name="Transaction">
            <directory>./vendor/windawake/laravel-reset-transaction/tests</directory>
        </testsuite>
    </testsuites>
    ......</phpunit>

最后運行測試命令 ./vendor/bin/phpunit --testsuite=Transaction
運行結果如下所示,5個例子測試通過。

oot@DESKTOP-VQOELJ5:/web/linux/php/laravel/laravel62# ./vendor/bin/phpunit --testsuite=TransactionPHPUnit 8.5.20 by Sebastian Bergmann and contributors......                                                               5 / 5 (100%)Time: 219 ms, Memory: 22.00 MB

OK (5 tests, 5 assertions)

功能特性

  • 開箱即用,不需要重構原有項目的代碼,與mysql事務寫法一致,簡單易用。

  • 支持http協(xié)議的服務化接口,想要支持其它協(xié)議則需要重寫中間件。

  • 支持讀已提交,可重復讀,與mysql的事務隔離級別同步。

原理解析

看過《明日邊緣》電影就會知道,存檔和讀檔的操作。這個分布式事務組件仿造《明日邊緣》電影的原理,每次請求基礎服務一開始時讀檔,然后繼續(xù)后面的操作,結束時所有操作全部回滾并且存檔,最后commit把存檔全部執(zhí)行成功。整個過程是遵守兩段提交協(xié)議,先prepare,最后commit。

如何使用

vendor/windawake/laravel-reset-transaction/tests/TransactionTest.php文件為例子

<?php
namespace Tests\Feature;use Tests\TestCase;use Illuminate\Support\Facades\DB;class TransactionTest extends TestCase{
    public function testCreateWithCommit()
    {
        $num = rand(1, 10000);
        $productName = 'php ' . $num;
        $data = [
            'store_id' => 1,
            'product_name' => $productName,
        ];
        // 開啟分布式事務,其實是生成全局唯一id
        $transactId = $this->beginDistributedTransaction();
        $header = [
           在header 'transact_id' => $transactId,
        ];
        // 分布式事務內(nèi),請求都需要在request header帶上transact_id
        $response = $this->post('api/resetProduct', $data, $header);
        $product = $response->json();
        // 分布式事務提交,也是接口請求,把之前的存檔記錄全部處理
        $this->commitDistributedTransaction($transactId);

        $response = $this->get('/api/resetProduct/' . $product['pid']);
        $product = $response->json();
        $this->assertEquals($productName, $product['product_name']);
    }

    private function beginDistributedTransaction()
    {
        return session_create_id();
    }

    private function commitDistributedTransaction($transactId)
    {
        $response = $this->post('/api/resetTransaction/commit', [], ['transact_id' => $transactId]);
        return $response->getStatusCode();
    }

    private function rollbackDistributedTransaction($transactId)
    {
        $response = $this->post('/api/resetTransaction/rollback', [], ['transact_id' => $transactId]);
        return $response->getStatusCode();
    }}

感謝各位的閱讀,以上就是“Laravel基于reset怎么實現(xiàn)分布式事務”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Laravel基于reset怎么實現(xiàn)分布式事務這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI