1.建表
-
新建商品表
CREATE TABLE `zlsn_concurrency_goods` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `goods_code` varchar(10) NOT NULL COMMENT '商品编码', `num` int(10) NOT NULL COMMENT '商品剩余数量', `created_at` datetime DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='模拟高并发-商品表'; -
新建订单表
CREATE TABLE `zlsn_concurrency_user_order` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_code` varchar(10) NOT NULL COMMENT '用户编码', `order_code` varchar(10) NOT NULL COMMENT '订单编码', `created_at` datetime DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=114 DEFAULT CHARSET=utf8mb4 COMMENT='模拟高并发-用户订单表';
2.编写模型类和控制器
-
模型类
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * 测试高并发商品表 * Class TestGoods * @package App\Models */ class TestGoods extends Model { protected $table = "concurrency_goods"; }<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * 测试高并发用户订单表 * Class UserOrder * @package App\Models */ class TestUserOrder extends Model { protected $table = "concurrency_user_order"; } -
控制器
<?php namespace App\Http\Controllers\Test; use App\Models\TestGoods; use App\Models\TestUserOrder; /** * 测试高并发接口 * Class ConcurrencyController * @package App\Http\Controllers\Test */ class ConcurrencyController { /** * Notes:秒杀功能 * Author:zlsn <269765627@qq.com> * DateTime: 2022/7/9 10:32 */ public function seckill() { $detail = TestGoods::where('id', 1)->select('id', 'num')->get()->toArray(); //模拟存放用户id $ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $userId = "测试用户" . $ids[rand(0, 9)]; if (!empty($detail) && $detail[0]['num'] > 0) { //商品id和商品数量 $id = $detail[0]['id']; $number = $detail[0]['num']; //商品数量减一 $number -= 1; $testGoodModel = new TestGoods(); $param[

本文详细介绍了如何使用PHP构建商品表和订单表,通过JMeter模拟高并发场景,并探讨了多种解决超卖和并发问题的方法,如悲观锁、乐观锁和Redis实现。最后,总结了其他测试工具的使用和常见技术手段应对高并发挑战。
最低0.47元/天 解锁文章
4892

被折叠的 条评论
为什么被折叠?



