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=