lumen搭建
lumen搭建项目
1. composer create-project --prefer-dist laravel/lumen project_ar_zhihu_api
2. cd project_ar_zhihu_api
3. composer require illuminate/redis
4. php -S localhost:8000 -t public
添加dingo
安装扩展
"dingo/api": "^2.1",
composer update
修改配置 app.php
$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
$app['Dingo\Api\Exception\Handler']->setErrorFormat([
'error' => [
'message' => ':message',
'errors' => ':errors',
'code' => ':code',
'status_code' => ':status_code',
'debug' => ':debug'
]
]);
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($app) {
//引入路由文件
require __DIR__.'/../routes/web.php';
require __DIR__.'/../routes/api/v1.php';
});
修改.env文件
APP_NAME=SINWJIM
APP_ENV=local
APP_KEY=base64:Fi0d5YkFGMgEffaQInE1D0fj1miHjSvM9LLa5Pr2y8o=
APP_DEBUG=true
APP_URL=http://127.0.0.1:1215
####Dingo API配置
####接口的名称,用于生成api文档,其他地方不使用
####的唯一作用是在使用API Blueprint命令生成文档的时候会使用,这样可以省去手动书写名字
API_NAME=SINWJIM
####或者是设置子域名,两者选一个
API_DOMAIN=127.0.0.1
####在请求header中需要用到他
####subtype 是项目或工程的简称,全部小写
API_SUBTYPE=sinwjim
####接口的版本,填写后是默认访问的版本
API_VERSION=v1
####标准数
####有三个不同的树:x、prs和vnd,你使用的标准树取决于你所开发的项目
####未注册的树(x)主要用于本地或私有环境
####个人树(prs)主要用于非商业销售的项目
####供应商树(vnd)主要用于公开的以及商业销售的项目
####是一种概念上的东西,类似与git的分支,如果正常开发就按照 x,prs,vnd 的描述来填写即可。
API_STANDARDS_TREE=vnd
####条件请求默认为开启状态,这有利于客户端的缓存机制在可能的情况下缓存 API 请求。
####带条件的请求
####由于缓存API请求的时候会使用客户端的缓存功能,所以默认是开启了带条件的请求,如果要关闭这项
API_CONDITIONAL_REQUEST=true
####是否开启调试,开启后访问api会看到
API_DEBUG=true
####返回的类型,一般都是json
API_DEFAULT_FORMAT=json
####严格模式要求客户端发送Accept头代替.env文件中的version,意味着不能通过浏览器访问api。
API_STRICT=true
配置swoole支持
安装swoole扩展
swooletw/laravel-swoole": "v2.6.5.4
composer update
修改配置
在bootstrap下增加2个文件swoole_http.php 和swoole_websocket.php
swoole_http.php
<?php
return [
/*
|--------------------------------------------------------------------------
| HTTP server configurations.
|--------------------------------------------------------------------------
|
| @see https://www.swoole.co.uk/docs/modules/swoole-server/configuration
|
*/
'server' => [
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
'port' => env('SWOOLE_HTTP_PORT', '1215'),
'public_path' => base_path('public'),
// Determine if to use swoole to respond request for static files
'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
'access_log' => env('SWOOLE_HTTP_ACCESS_LOG', false),
// You must add --enable-openssl while compiling Swoole
// Put `SWOOLE_SOCK_TCP | SWOOLE_SSL` if you want to enable SSL
'socket_type' => SWOOLE_SOCK_TCP,
'process_type' => SWOOLE_PROCESS,
'options' => [
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
// Normally this value should be 1~4 times larger according to your cpu cores.
//通过此参数来调节Reactor线程的数量,以充分利用多核
'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num()),
'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num()),
//启动异步非阻塞 如task_worker_num => 64,表示启动64个进程来接收异步任务
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num()),
// The data to receive can't be larger than buffer_output_size.
'package_max_length' => 20 * 1024 * 1024,
// The data to send can't be larger than buffer_output_size.
'buffer_output_size' => 10 * 1024 * 1024,
// Max buffer size for socket connections
'socket_buffer_size' => 128 * 1024 * 1024,
// Worker will restart after processing this number of requests
//此参数表示worker进程在处理完n次请求后结束运行。manager会重新创建一个worker进程。此选项用来防止worker进程内存溢出。
'max_request' => 3000,
// Enable coroutine send
'send_yield' => true,
// You must add --enable-openssl while compiling Swoole
'ssl_cert_file' => null,
'ssl_key_file' => null,
],
],
/*
|--------------------------------------------------------------------------
| Enable to turn on websocket server.
|--------------------------------------------------------------------------
*/
'websocket' => [
'enabled' => env('SWOOLE_HTTP_WEBSOCKET', false),
],
/*
|--------------------------------------------------------------------------
| Hot reload configuration
|--------------------------------------------------------------------------
*/
'hot_reload' => [
'enabled' => env('SWOOLE_HOT_RELOAD_ENABLE', false),
'recursively' => env('SWOOLE_HOT_RELOAD_RECURSIVELY', true),
'directory' => env('SWOOLE_HOT_RELOAD_DIRECTORY', base_path()),
'log' => env('SWOOLE_HOT_RELOAD_LOG', true),
'filter' => env('SWOOLE_HOT_RELOAD_FILTER', '.php'),
],
/*
|--------------------------------------------------------------------------
| Console output will be transferred to response content if enabled.
|--------------------------------------------------------------------------
*/
'ob_output' => env('SWOOLE_OB_OUTPUT', true),
/*
|--------------------------------------------------------------------------
| Pre-resolved instances here will be resolved when sandbox created.
|--------------------------------------------------------------------------
*/
'pre_resolved' => [
'view', 'files', 'session', 'session.store', 'routes',
'db', 'db.factory', 'cache', 'cache.store', 'config', 'cookie',
'encrypter', 'hash', 'router', 'translator', 'url', 'log',
],
/*
|--------------------------------------------------------------------------
| Instances here will be cleared on every request.
|--------------------------------------------------------------------------
*/
'instances' => [
//
],
/*
|--------------------------------------------------------------------------
| Providers here will be registered on every request.
|--------------------------------------------------------------------------
*/
'providers' => [
Illuminate\Pagination\PaginationServiceProvider::class,
],
/*
|--------------------------------------------------------------------------
| Resetters for sandbox app.
|--------------------------------------------------------------------------
*/
'resetters' => [
SwooleTW\Http\Server\Resetters\ResetConfig::class,
SwooleTW\Http\Server\Resetters\ResetSession::class,
SwooleTW\Http\Server\Resetters\ResetCookie::class,
SwooleTW\Http\Server\Resetters\ClearInstances::class,
SwooleTW\Http\Server\Resetters\BindRequest::class,
SwooleTW\Http\Server\Resetters\RebindKernelContainer::class,
SwooleTW\Http\Server\Resetters\RebindRouterContainer::class,
SwooleTW\Http\Server\Resetters\RebindViewContainer::class,
SwooleTW\Http\Server\Resetters\ResetProviders::class,
],
/*
|--------------------------------------------------------------------------
| Define your swoole tables here.
|
| @see https://www.swoole.co.uk/docs/modules/swoole-table
|--------------------------------------------------------------------------
*/
'tables' => [
// 'table_name' => [
// 'size' => 1024,
// 'columns' => [
// ['name' => 'column_name', 'type' => Table::TYPE_STRING, 'size' => 1024],
// ]
// ],
],
];
swoole_websocket.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Websocket handler for onOpen and onClose callback
| Replace this handler if you want to customize your websocket handler
|--------------------------------------------------------------------------
*/
'handler' => SwooleTW\Http\Websocket\SocketIO\WebsocketHandler::class,
/*
|--------------------------------------------------------------------------
| Default frame parser
| Replace it if you want to customize your websocket payload
|--------------------------------------------------------------------------
*/
'parser' => SwooleTW\Http\Websocket\SocketIO\SocketIOParser::class,
/*
|--------------------------------------------------------------------------
| Websocket route file path
|--------------------------------------------------------------------------
*/
'route_file' => base_path('routes/websocket.php'),
/*
|--------------------------------------------------------------------------
| Default middleware for on connect request
|--------------------------------------------------------------------------
*/
'middleware' => [
// SwooleTW\Http\Websocket\Middleware\DecryptCookies::class,
// SwooleTW\Http\Websocket\Middleware\StartSession::class,
// SwooleTW\Http\Websocket\Middleware\Authenticate::class,
],
/*
|--------------------------------------------------------------------------
| Default websocket driver
|--------------------------------------------------------------------------
*/
'default' => 'table',
/*
|--------------------------------------------------------------------------
| Websocket client's heartbeat interval (ms)
|--------------------------------------------------------------------------
*/
'ping_interval' => 25000,
/*
|--------------------------------------------------------------------------
| Websocket client's heartbeat interval timeout (ms)
|--------------------------------------------------------------------------
*/
'ping_timeout' => 60000,
/*
|--------------------------------------------------------------------------
| Room drivers mapping
|--------------------------------------------------------------------------
*/
'drivers' => [
'table' => SwooleTW\Http\Websocket\Rooms\TableRoom::class,
'redis' => SwooleTW\Http\Websocket\Rooms\RedisRoom::class,
],
/*
|--------------------------------------------------------------------------
| Room drivers settings
|--------------------------------------------------------------------------
*/
'settings' => [
'table' => [
'room_rows' => 4096,
'room_size' => 2048,
'client_rows' => 8192,
'client_size' => 2048,
],
'redis' => [
'server' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'persistent' => true,
],
'options' => [
//
],
'prefix' => 'swoole:',
],
],
];
修改env
#####swoole 配置
#####worker进程数
#####同步阻塞服务器,worker_num配置为100或者更高,具体要看每次请求处理的耗时和操作系统负载状况
SWOOLE_HTTP_WORKER_NUM=10
#####reactor线程数
#####默认设置为CPU核数
SWOOLE_HTTP_REACTOR_NUM=2
#####守护进程化
#####转入后台作为守护进程运行
SWOOLE_HTTP_DAEMONIZE=1
命令
php artisan swoole:http start //开启http服务
php artisan swoole:http stop //停止
php artisan swoole:http restart //重新启动
php artisan swoole:http reload //重新加载,每次改完代码都需运行此命令
php artisan swoole:http infos //运行信息
测试
curl http://127.0.0.1:1215
更新完成代码需要
php artisan swoole:http reload
swoole nginx配置
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name meu.kukuvideo.com;
root /opt/onemena/wwwroot/mobibookapp.com/project_adjust_api/public;
index index.php;
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
# any php files must not be accessed
#location ~* \.php$ {
# return 404;
#}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# IF https
# proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$suffix;
}
}
```
## 配置log
#### 安装log
"rap2hpoutre/laravel-log-viewer": "^1.1",
composer update
#### 修改 env 文件
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
网友评论