用webman重构一下
安装
webman 要求PHP >= 7.2
composer安装
1、去掉composer代理composer config -g --unset repos.packagist
2、创建项目
composer create-project workerman/webman
3、运行
进入webman目录
debug方式运行(用于开发调试)
php start.php start
daemon方式运行(用于正式环境)
php start.php start -d
windows用户用 双击windows.bat 或者运行 php windows.php
启动
4、访问
浏览器访问 http://ip地址:8787
webman配置https证书
配置数据库
数据库配置文件位置为 config/database.php
。
<?php
return [
// 默认数据库
'default' => 'mysql',
// 各种数据库配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'webman',
'username' => 'webman',
'password' => 'webman',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'webm_',
'strict' => false, //是否严格模式
'engine' => null,
],
],
];
Migration数据库迁移工具 Phinx
composer require robmorgan/phinx
在你项目目录中创建目录 db/migrations
,并给予充足权限。这个目录将是你迁移脚本放置的地方,并且应该被设置为可写权限。
安装后,Phinx 现在可以在你的项目中执行初始化
vendor/bin/phinx init
检查是否创建成功
# !!! 这个测试通过,并没有测试 数据库设链接是否正确。
[root@iZvixj0oze6411Z webman]# vendor/bin/phinx test
### 默认的环境是开发环境,这个测试数据库的链接是否正确
[root@iZvixj0oze6411Z webman]# vendor/bin/phinx test -e development
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
validating environment development
success!
创建目录
[root@iZvixj0oze6411Z webman]# php vendor/bin/phinx create UserMigration
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
using migration paths
- /www/wwwroot/webman/db/migrations
using seed paths
- /www/wwwroot/webman/db/seeds
using migration base class Phinx\Migration\AbstractMigration
using default template
created db/migrations/20221124030111_user_migration.php
public function change()
{
$table = $this->table('webm_user');
// 添加几个字段
$table
->addColumn('name', 'string', ["length" => "30", "default" => "", "comment" => '用户名'])
->addColumn('phone', 'string', ["length" => "30"])
->addColumn('sex', 'integer', ["length" => "4"])
->addIndex(array('name'), array('unique' => true, 'name' => 'idx_users_name'))
->addIndex(array('phone'), array('unique' => false, 'name' => 'idx_users_phone'))
->create();
}
运行
[root@iZvixj0oze6411Z webman]# php vendor/bin/phinx migrate
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
using migration paths
- /www/wwwroot/webman/db/migrations
using seed paths
- /www/wwwroot/webman/db/seeds
warning no environment specified, defaulting to: development
using adapter mysql
using database webman
ordering by creation time
== 20221124024949 MyNewMigration: migrating
== 20221124024949 MyNewMigration: migrated 0.0085s
== 20221124030111 UserMigration: migrating
== 20221124030111 UserMigration: migrated 0.0540s
All Done. Took 0.0783s
添加一个字段
[root@iZvixj0oze6411Z webman]# php vendor/bin/phinx create User02Migration
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
using migration paths
- /www/wwwroot/webman/db/migrations
using seed paths
- /www/wwwroot/webman/db/seeds
using migration base class Phinx\Migration\AbstractMigration
using default template
created db/migrations/20221124032327_user_02_migration.php
public function change()
{
$table = $this->table('webm_user');
$table
->addColumn('created_at', 'timestamp')
->addColumn('updated_at', 'timestamp')
->addColumn('deleted_at', 'timestamp')
->save();
}
使用建议
迁移文件一旦代码合并后不允许再次修改,出现问题必须新建修改或者删除操作文件进行处理。
数据库
安装
composer require -W psr/container ^1.1.1 illuminate/database illuminate/pagination illuminate/events symfony/var-dumper
user 模型关联
<?php
namespace app\model;
use support\Model;
class User extends Model
{
protected $with = [
'user_token'
];
/**
* 与模型关联的表名
*
* @var string
*/
protected $table = 'user';
/**
* 重定义主键,默认是id
*
* @var string
*/
protected $primaryKey = 'id';
/**
* 指示是否自动维护时间戳
*
* @var bool
*/
public $timestamps = true;
public function user_token()
{
return $this->hasOne(UserToken::class, 'user_id', 'id');
}
}
验证器
路由
<?php
use Webman\Route;
Route::group('/admin', function () {
Route::any('/login/index', [app\admin\controller\LoginController::class, 'index']);
});
Route::group('/admin', function () {
Route::any('/user/index', [app\admin\controller\UserController::class, 'index']);
Route::any('/user/store', [app\admin\controller\UserController::class, 'store']);
Route::any('/user/update', [app\admin\controller\UserController::class, 'update']);
Route::any('/user/{id}', [app\admin\controller\UserController::class, 'show']);
Route::any('/user/delete/{id}', [app\admin\controller\UserController::class, 'delete']);
})->middleware([
app\middleware\AuthCheckTest::class,
]);
控制器
<?php
namespace app\admin\controller;
use app\model\User;
use support\Request;
use support\Response;
class UserController
{
public function index(Request $request)
{
$session = $request->session();
$user = $session->get('user');
var_dump('登录用户:' . $user->id);
$per_page = 2;
$users = User::paginate($per_page, '*', 'page', $request->input('page'));
return json(['code' => 0, 'msg' => '获取成功', 'data' => $users]);
}
public function store(Request $request)
{
// 验证请求
$user = new User();
$user->name = $request->get('name') . date('Y-m-d H:i:s');
$user->phone = $request->get('phone');
$user->sex = $request->get('sex');
$user->save();
return json(['code' => 0, 'msg' => '添加成功']);
}
public function update(Request $request)
{
$user = User::find($request->get('id'));
$user->name = $request->get('name') . '修改了' . date('Y-m-d H:i:s');
$user->save();
return json(['code' => 0, 'msg' => '保存成功']);
}
public function show($id)
{
$model = User::find($id);
return json(['code' => 0, 'msg' => '获取成功', 'data' => $model]);
}
public function delete($id)
{
$model = User::find($id);
$model->delete();
return json(['code' => 0, 'msg' => '删除成功']);
}
}
token验证
[root@iZvixj0oze6411Z webman]# php vendor/bin/phinx create UserTokenMigration
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
using migration paths
- /www/wwwroot/webman/db/migrations
using seed paths
- /www/wwwroot/webman/db/seeds
using migration base class Phinx\Migration\AbstractMigration
using default template
created db/migrations/20221124064101_user_token_migration.php
public function change()
{
$table = $this->table('webm_user_token');
// 添加几个字段
$table
->addColumn('user_id', 'integer', ["default" => 0, "comment" => '用户id'])
->addColumn('token', 'string', ["length" => "32"])
->addIndex(array('user_id'), array('unique' => false, 'name' => 'idx_user_id'))
->addIndex(array('token'), array('unique' => false, 'name' => 'idx_token'))
->create();
}
[root@iZvixj0oze6411Z webman]# php vendor/bin/phinx migrate
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
using migration paths
- /www/wwwroot/webman/db/migrations
using seed paths
- /www/wwwroot/webman/db/seeds
warning no environment specified, defaulting to: development
using adapter mysql
using database webman
ordering by creation time
== 20221124064101 UserTokenMigration: migrating
== 20221124064101 UserTokenMigration: migrated 0.0589s
user_token model
<?php
namespace app\model;
use support\Model;
class UserToken extends Model
{
/**
* 与模型关联的表名
*
* @var string
*/
protected $table = 'user_token';
/**
* 重定义主键,默认是id
*
* @var string
*/
protected $primaryKey = 'id';
public $timestamps = false;
}
登录
<?php
namespace app\admin\controller;
use app\model\User;
use app\model\UserToken;
use support\Request;
use support\Response;
class LoginController
{
public function index(Request $request)
{
$model = User::find(9);
$user_token_model = new UserToken();
$user_token_model->token = md5($model->id);
$user_token_model->user_id = $model->id;
$user_token_model->save();
return json(['code' => 0, 'msg' => '登录成功', 'token' => md5($model->id)]);
}
}
登录中间件
<?php
namespace app\middleware;
use app\model\User;
use app\model\UserToken;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AuthCheckTest implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
$token = $request->get('token');
var_dump($token);
if (empty($token)) {
return json(['code' => 0, 'msg' => '请先登录1']);
}
$user_token_model = UserToken::where('token', $token)->first();
if (empty($user_token_model)) {
return json(['code' => 0, 'msg' => '请先登录2']);
}
$model = User::find($user_token_model->user_id);
if (empty($model)) {
return json(['code' => 0, 'msg' => '非法操作']);
}
$session = $request->session();
$session->set('user', $model);
// 请求继续向洋葱芯穿越
return $handler($request);
}
}
访问
ip:8787/admin/login/index
ip:8787/admin/user/index?page=3&token=45c48cce2e2d7fbdea1afc51c7c6ad26
ip:8787/admin/user/store?name=webman4&phone=10010&sex=1
ip:8787/admin/user/update?id=4&name=tony
ip:8787/admin/user/1
ip:8787/admin/user/delete/1
ab压测
安装
yum -y install httpd-tools
ab -V
现在我们假设有1000个请求(-n),并发量为100(-c)
ab -n 1000 -c 100 http://ip:8787/admin/user/index?page=3&token=45c48cce2e2d7fbdea1afc51c7c6ad26
阿里云1核2G压测结果
本作品采用《CC 协议》,转载必须注明作者和本文链接
压测的传输速度太低了,看起来瓶颈在服务器带宽上。