提交服务端基础框架

This commit is contained in:
wanghao
2025-03-12 12:21:57 +08:00
parent 023758147a
commit 1ee65ad34e
1471 changed files with 284825 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker;
use think\App;
use think\Error;
use think\exception\HttpException;
use Workerman\Protocols\Http as WorkerHttp;
/**
* Worker应用对象
*/
class Application extends App
{
/**
* 处理Worker请求
* @access public
* @param \Workerman\Connection\TcpConnection $connection
* @param void
*/
public function worker($connection)
{
try {
ob_start();
// 重置应用的开始时间和内存占用
$this->beginTime = microtime(true);
$this->beginMem = memory_get_usage();
// 销毁当前请求对象实例
$this->delete('think\Request');
$pathinfo = ltrim(strpos($_SERVER['REQUEST_URI'], '?') ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'], '/');
$this->request
->setPathinfo($pathinfo)
->withInput($GLOBALS['HTTP_RAW_REQUEST_DATA']);
if ($this->config->get('session.auto_start')) {
WorkerHttp::sessionStart();
}
// 更新请求对象实例
$this->route->setRequest($this->request);
$response = $this->run();
$response->send();
$content = ob_get_clean();
// Trace调试注入
if ($this->env->get('app_trace', $this->config->get('app_trace'))) {
$this->debug->inject($response, $content);
}
$this->httpResponseCode($response->getCode());
foreach ($response->getHeader() as $name => $val) {
// 发送头部信息
WorkerHttp::header($name . (!is_null($val) ? ':' . $val : ''));
}
$connection->send($content);
} catch (HttpException $e) {
$this->exception($connection, $e);
} catch (\Exception $e) {
$this->exception($connection, $e);
} catch (\Throwable $e) {
$this->exception($connection, $e);
}
}
protected function httpResponseCode($code = 200)
{
WorkerHttp::header('HTTP/1.1', true, $code);
}
protected function exception($connection, $e)
{
if ($e instanceof \Exception) {
$handler = Error::getExceptionHandler();
$handler->report($e);
$resp = $handler->render($e);
$content = $resp->getContent();
$code = $resp->getCode();
$this->httpResponseCode($code);
$connection->send($content);
} else {
$this->httpResponseCode(500);
$connection->send($e->getMessage());
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker;
use think\Cookie as BaseCookie;
use Workerman\Protocols\Http as WorkerHttp;
/**
* Workerman Cookie类
*/
class Cookie extends BaseCookie
{
/**
* Cookie初始化
* @access public
* @param array $config
* @return void
*/
public function init(array $config = [])
{
$this->config = array_merge($this->config, array_change_key_case($config));
}
/**
* Cookie 设置保存
*
* @access public
* @param string $name cookie名称
* @param mixed $value cookie值
* @param array $option 可选参数
* @return void
*/
protected function setCookie($name, $value, $expire, $option = [])
{
WorkerHttp::setCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'], $option['httponly']);
}
}

View File

@@ -0,0 +1,97 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker;
use GatewayWorker\Lib\Gateway;
use Workerman\Worker;
/**
* Worker 命令行服务类
*/
class Events
{
/**
* onWorkerStart 事件回调
* 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
*
* @access public
* @param \Workerman\Worker $businessWorker
* @return void
*/
public static function onWorkerStart(Worker $businessWorker)
{
$app = new Application;
$app->initialize();
}
/**
* onConnect 事件回调
* 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
*
* @access public
* @param int $client_id
* @return void
*/
public static function onConnect($client_id)
{
Gateway::sendToCurrentClient("Your client_id is $client_id");
}
/**
* onWebSocketConnect 事件回调
* 当客户端连接上gateway完成websocket握手时触发
*
* @param integer $client_id 断开连接的客户端client_id
* @param mixed $data
* @return void
*/
public static function onWebSocketConnect($client_id, $data)
{
var_export($data);
}
/**
* onMessage 事件回调
* 当客户端发来数据(Gateway进程收到数据)后触发
*
* @access public
* @param int $client_id
* @param mixed $data
* @return void
*/
public static function onMessage($client_id, $data)
{
Gateway::sendToAll($data);
}
/**
* onClose 事件回调 当用户断开连接时触发的方法
*
* @param integer $client_id 断开连接的客户端client_id
* @return void
*/
public static function onClose($client_id)
{
GateWay::sendToAll("client[$client_id] logout\n");
}
/**
* onWorkerStop 事件回调
* 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
*
* @param \Workerman\Worker $businessWorker
* @return void
*/
public static function onWorkerStop(Worker $businessWorker)
{
echo "WorkerStop\n";
}
}

View File

@@ -0,0 +1,284 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use think\Facade;
use think\Loader;
use Workerman\Lib\Timer;
use Workerman\Protocols\Http as WorkerHttp;
use Workerman\Protocols\Http as HttpProtocols;
use Workerman\Worker;
/**
* Worker http server 命令行服务类
*/
class Http extends Server
{
protected $app;
protected $appPath;
protected $root;
protected $monitor;
protected $lastMtime;
/** @var array Mime mapping. */
protected static $mimeTypeMap = [];
/**
* 架构函数
* @access public
* @param string $host 监听地址
* @param int $port 监听端口
* @param array $context 参数
*/
public function __construct($host, $port, $context = [])
{
$this->worker = new Worker('http://' . $host . ':' . $port, $context);
// 设置回调
foreach ($this->event as $event) {
if (method_exists($this, $event)) {
$this->worker->$event = [$this, $event];
}
}
}
public function setRoot($root)
{
$this->root = $root;
}
public function setAppPath($path)
{
$this->appPath = $path;
}
public function setStaticOption($name, $value)
{
Worker::${$name} = $value;
}
public function setMonitor($interval = 2, $path = [])
{
$this->monitor['interval'] = $interval;
$this->monitor['path'] = (array) $path;
}
/**
* 设置参数
* @access public
* @param array $option 参数
* @return void
*/
public function option(array $option)
{
// 设置参数
if (!empty($option)) {
foreach ($option as $key => $val) {
$this->worker->$key = $val;
}
}
}
/**
* onWorkerStart 事件回调
* @access public
* @param \Workerman\Worker $worker
* @return void
* @throws \Exception
*/
public function onWorkerStart($worker)
{
$this->initMimeTypeMap();
$this->app = new Application($this->appPath);
$this->lastMtime = time();
$this->app->workerman = $worker;
// 指定日志类驱动
Loader::addClassMap([
'think\\log\\driver\\File' => __DIR__ . '/log/File.php',
]);
Facade::bind([
'think\facade\Cookie' => Cookie::class,
'think\facade\Session' => Session::class,
facade\Application::class => Application::class,
facade\Http::class => Http::class,
]);
// 应用初始化
$this->app->initialize();
$this->app->bindTo([
'cookie' => Cookie::class,
'session' => Session::class,
]);
if (0 == $worker->id && $this->monitor) {
$paths = $this->monitor['path'] ?: [$this->app->getAppPath(), $this->app->getConfigPath()];
$timer = $this->monitor['interval'] ?: 2;
Timer::add($timer, function () use ($paths) {
foreach ($paths as $path) {
$dir = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir);
foreach ($iterator as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) != 'php') {
continue;
}
if ($this->lastMtime < $file->getMTime()) {
echo '[update]' . $file . "\n";
posix_kill(posix_getppid(), SIGUSR1);
$this->lastMtime = $file->getMTime();
return;
}
}
}
});
}
}
/**
* onMessage 事件回调
* @access public
* @param \Workerman\Connection\TcpConnection $connection
* @param mixed $data
* @return void
*/
public function onMessage($connection, $data)
{
$uri = parse_url($_SERVER['REQUEST_URI']);
$path = isset($uri['path']) ? $uri['path'] : '/';
$file = $this->root . $path;
if (!is_file($file)) {
$this->app->worker($connection, $data);
} else {
$this->sendFile($connection, $file);
}
}
protected function sendFile($connection, $file)
{
$info = stat($file);
$modifiyTime = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' ' . date_default_timezone_get() : '';
if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
// Http 304.
if ($modifiyTime === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
// 304
WorkerHttp::header('HTTP/1.1 304 Not Modified');
// Send nothing but http headers..
return $connection->close('');
}
}
$mimeType = $this->getMimeType($file);
WorkerHttp::header('HTTP/1.1 200 OK');
WorkerHttp::header('Connection: keep-alive');
if ($mimeType) {
WorkerHttp::header('Content-Type: ' . $mimeType);
} else {
WorkerHttp::header('Content-Type: application/octet-stream');
$fileinfo = pathinfo($file);
$filename = isset($fileinfo['filename']) ? $fileinfo['filename'] : '';
WorkerHttp::header('Content-Disposition: attachment; filename="' . $filename . '"');
}
if ($modifiyTime) {
WorkerHttp::header('Last-Modified: ' . $modifiyTime);
}
WorkerHttp::header('Content-Length: ' . filesize($file));
ob_start();
readfile($file);
$content = ob_get_clean();
return $connection->send($content);
}
/**
* 获取文件类型信息
* @access public
* @param $filename
* @return string
*/
public function getMimeType($filename)
{
$file_info = pathinfo($filename);
$extension = isset($file_info['extension']) ? $file_info['extension'] : '';
if (isset(self::$mimeTypeMap[$extension])) {
$mime = self::$mimeTypeMap[$extension];
} else {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filename);
}
return $mime;
}
/**
* 启动
* @access public
* @return void
*/
public function start()
{
Worker::runAll();
}
/**
* 停止
* @access public
* @return void
*/
public function stop()
{
Worker::stopAll();
}
/**
* Init mime map.
*
* @return void
* @throws \Exception
*/
public function initMimeTypeMap()
{
$mime_file = HttpProtocols::getMimeTypesFile();
if (!is_file($mime_file)) {
Worker::log("$mime_file mime.type file not fond");
return;
}
$items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!is_array($items)) {
Worker::log("get $mime_file mime.type content fail");
return;
}
foreach ($items as $content) {
if (preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match)) {
$mime_type = $match[1];
$workerman_file_extension_var = $match[2];
$workerman_file_extension_array = explode(' ', substr($workerman_file_extension_var, 0, -1));
foreach ($workerman_file_extension_array as $workerman_file_extension) {
self::$mimeTypeMap[$workerman_file_extension] = $mime_type;
}
}
}
}
}

View File

@@ -0,0 +1,75 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker;
use Workerman\Worker;
/**
* Worker控制器扩展类
*/
abstract class Server
{
protected $worker;
protected $socket = '';
protected $protocol = 'http';
protected $host = '0.0.0.0';
protected $port = '2346';
protected $option = [];
protected $context = [];
protected $event = ['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerReload', 'onWebSocketConnect'];
/**
* 架构函数
* @access public
*/
public function __construct()
{
// 实例化 Websocket 服务
$this->worker = new Worker($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port, $this->context);
// 设置参数
if (!empty($this->option)) {
foreach ($this->option as $key => $val) {
$this->worker->$key = $val;
}
}
// 设置回调
foreach ($this->event as $event) {
if (method_exists($this, $event)) {
$this->worker->$event = [$this, $event];
}
}
// 初始化
$this->init();
}
protected function init()
{
}
public function start()
{
Worker::runAll();
}
public function __set($name, $value)
{
$this->worker->$name = $value;
}
public function __call($method, $args)
{
call_user_func_array([$this->worker, $method], $args);
}
}

View File

@@ -0,0 +1,157 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker;
use think\Session as BaseSession;
use Workerman\Protocols\Http as WorkerHttp;
/**
* Workerman Cookie类
*/
class Session extends BaseSession
{
/**
* session初始化
* @access public
* @param array $config
* @return void
* @throws \think\Exception
*/
public function init(array $config = [])
{
$config = $config ?: $this->config;
$isDoStart = false;
if (isset($config['use_trans_sid'])) {
ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
}
// 启动session
if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
ini_set('session.auto_start', 0);
$isDoStart = true;
}
if (isset($config['prefix'])) {
$this->prefix = $config['prefix'];
}
if (isset($config['use_lock'])) {
$this->lock = $config['use_lock'];
}
if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
session_id($_REQUEST[$config['var_session_id']]);
} elseif (isset($config['id']) && !empty($config['id'])) {
session_id($config['id']);
} else {
session_id(WorkerHttp::sessionId());
}
if (isset($config['name'])) {
WorkerHttp::sessionName($config['name']);
}
if (isset($config['path'])) {
WorkerHttp::sessionSavePath($config['path']);
}
if (isset($config['domain'])) {
ini_set('session.cookie_domain', $config['domain']);
}
if (isset($config['expire'])) {
ini_set('session.gc_maxlifetime', $config['expire']);
ini_set('session.cookie_lifetime', $config['expire']);
}
if (isset($config['secure'])) {
ini_set('session.cookie_secure', $config['secure']);
}
if (isset($config['httponly'])) {
ini_set('session.cookie_httponly', $config['httponly']);
}
if (isset($config['use_cookies'])) {
ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
}
if (isset($config['cache_limiter'])) {
session_cache_limiter($config['cache_limiter']);
}
if (isset($config['cache_expire'])) {
session_cache_expire($config['cache_expire']);
}
if (!empty($config['type'])) {
// 读取session驱动
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
// 检查驱动类
if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
throw new ClassNotFoundException('error session handler:' . $class, $class);
}
}
if ($isDoStart) {
$this->start();
} else {
$this->init = false;
}
return $this;
}
/**
* session自动启动或者初始化
* @access public
* @return void
*/
public function boot()
{
if (is_null($this->init)) {
$this->init();
}
if (false === $this->init) {
if (PHP_SESSION_ACTIVE != session_status()) {
$this->start();
}
$this->init = true;
}
}
/**
* 启动session
* @access public
* @return void
*/
public function start()
{
WorkerHttp::sessionStart();
$this->init = true;
}
/**
* 暂停session
* @access public
* @return void
*/
public function pause()
{
// 暂停session
WorkerHttp::sessionWriteClose();
$this->init = false;
}
}

View File

@@ -0,0 +1,16 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
\think\Console::addDefaultCommands([
'worker:gateway' => '\\think\\worker\\command\\GatewayWorker',
'worker:server' => '\\think\\worker\\command\\Server',
'worker' => '\\think\\worker\\command\\Worker',
]);

View File

@@ -0,0 +1,201 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker\command;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use Workerman\Worker;
/**
* Worker 命令行类
*/
class GatewayWorker extends Command
{
public function configure()
{
$this->setName('worker:gateway')
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
->setDescription('GatewayWorker Server for ThinkPHP');
}
public function execute(Input $input, Output $output)
{
$action = $input->getArgument('action');
if (DIRECTORY_SEPARATOR !== '\\') {
if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
$output->writeln("Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .");
exit(1);
}
global $argv;
array_shift($argv);
array_shift($argv);
array_unshift($argv, 'think', $action);
} else {
$output->writeln("GatewayWorker Not Support On Windows.");
exit(1);
}
if ('start' == $action) {
$output->writeln('Starting GatewayWorker server...');
}
$option = Config::pull('gateway_worker');
if ($input->hasOption('host')) {
$host = $input->getOption('host');
} else {
$host = !empty($option['host']) ? $option['host'] : '0.0.0.0';
}
if ($input->hasOption('port')) {
$port = $input->getOption('port');
} else {
$port = !empty($option['port']) ? $option['port'] : '2347';
}
$this->start($host, $port, $option);
}
/**
* 启动
* @access public
* @param string $host 监听地址
* @param integer $port 监听端口
* @param array $option 参数
* @return void
*/
public function start($host, $port, $option = [])
{
$registerAddress = !empty($option['registerAddress']) ? $option['registerAddress'] : '127.0.0.1:1236';
if (!empty($option['register_deploy'])) {
// 分布式部署的时候其它服务器可以关闭register服务
// 注意需要设置不同的lanIp
$this->register($registerAddress);
}
// 启动businessWorker
if (!empty($option['businessWorker_deploy'])) {
$this->businessWorker($registerAddress, isset($option['businessWorker']) ? $option['businessWorker'] : []);
}
// 启动gateway
if (!empty($option['gateway_deploy'])) {
$this->gateway($registerAddress, $host, $port, $option);
}
Worker::runAll();
}
/**
* 启动register
* @access public
* @param string $registerAddress
* @return void
*/
public function register($registerAddress)
{
// 初始化register
new Register('text://' . $registerAddress);
}
/**
* 启动businessWorker
* @access public
* @param string $registerAddress registerAddress
* @param array $option 参数
* @return void
*/
public function businessWorker($registerAddress, $option = [])
{
// 初始化 bussinessWorker 进程
$worker = new BusinessWorker();
$this->option($worker, $option);
$worker->registerAddress = $registerAddress;
}
/**
* 启动gateway
* @access public
* @param string $registerAddress registerAddress
* @param string $host 服务地址
* @param integer $port 监听端口
* @param array $option 参数
* @return void
*/
public function gateway($registerAddress, $host, $port, $option = [])
{
// 初始化 gateway 进程
if (!empty($option['socket'])) {
$socket = $option['socket'];
unset($option['socket']);
} else {
$protocol = !empty($option['protocol']) ? $option['protocol'] : 'websocket';
$socket = $protocol . '://' . $host . ':' . $port;
unset($option['host'], $option['port'], $option['protocol']);
}
$gateway = new Gateway($socket, isset($option['context']) ? $option['context'] : []);
// 以下设置参数都可以在配置文件中重新定义覆盖
$gateway->name = 'Gateway';
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 2000;
$gateway->pingInterval = 30;
$gateway->pingNotResponseLimit = 0;
$gateway->pingData = '{"type":"ping"}';
$gateway->registerAddress = $registerAddress;
// 全局静态属性设置
foreach ($option as $name => $val) {
if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
Worker::${$name} = $val;
unset($option[$name]);
}
}
$this->option($gateway, $option);
}
/**
* 设置参数
* @access protected
* @param Worker $worker Worker对象
* @param array $option 参数
* @return void
*/
protected function option($worker, array $option = [])
{
// 设置参数
if (!empty($option)) {
foreach ($option as $key => $val) {
$worker->$key = $val;
}
}
}
}

View File

@@ -0,0 +1,162 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use think\facade\Env;
use think\worker\Server as WorkerServer;
use Workerman\Worker;
/**
* Worker Server 命令行类
*/
class Server extends Command
{
protected $config = [];
public function configure()
{
$this->setName('worker:server')
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
->setDescription('Workerman Server for ThinkPHP');
}
public function execute(Input $input, Output $output)
{
$action = $input->getArgument('action');
if (DIRECTORY_SEPARATOR !== '\\') {
if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
$output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .</error>");
return false;
}
global $argv;
array_shift($argv);
array_shift($argv);
array_unshift($argv, 'think', $action);
} elseif ('start' != $action) {
$output->writeln("<error>Not Support action:{$action} on Windows.</error>");
return false;
}
$this->config = Config::pull('worker_server');
if ('start' == $action) {
$output->writeln('Starting Workerman server...');
}
// 自定义服务器入口类
if (!empty($this->config['worker_class'])) {
$class = (array) $this->config['worker_class'];
foreach ($class as $server) {
$this->startServer($server);
}
// Run worker
Worker::runAll();
return;
}
if (!empty($this->config['socket'])) {
$socket = $this->config['socket'];
list($host, $port) = explode(':', $socket);
} else {
$host = $this->getHost();
$port = $this->getPort();
$protocol = !empty($this->config['protocol']) ? $this->config['protocol'] : 'websocket';
$socket = $protocol . '://' . $host . ':' . $port;
unset($this->config['host'], $this->config['port'], $this->config['protocol']);
}
if (isset($this->config['context'])) {
$context = $this->config['context'];
unset($this->config['context']);
} else {
$context = [];
}
$worker = new Worker($socket, $context);
if (empty($this->config['pidFile'])) {
$this->config['pidFile'] = Env::get('runtime_path') . 'worker.pid';
}
// 避免pid混乱
$this->config['pidFile'] .= '_' . $port;
// 开启守护进程模式
if ($this->input->hasOption('daemon')) {
Worker::$daemonize = true;
}
if (!empty($this->config['ssl'])) {
$this->config['transport'] = 'ssl';
unset($this->config['ssl']);
}
// 设置服务器参数
foreach ($this->config as $name => $val) {
if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
Worker::${$name} = $val;
} else {
$worker->$name = $val;
}
}
// Run worker
Worker::runAll();
}
protected function startServer($class)
{
if (class_exists($class)) {
$worker = new $class;
if (!$worker instanceof WorkerServer) {
$this->output->writeln("<error>Worker Server Class Must extends \\think\\worker\\Server</error>");
}
} else {
$this->output->writeln("<error>Worker Server Class Not Exists : {$class}</error>");
}
}
protected function getHost()
{
if ($this->input->hasOption('host')) {
$host = $this->input->getOption('host');
} else {
$host = !empty($this->config['host']) ? $this->config['host'] : '0.0.0.0';
}
return $host;
}
protected function getPort()
{
if ($this->input->hasOption('port')) {
$port = $this->input->getOption('port');
} else {
$port = !empty($this->config['port']) ? $this->config['port'] : 2345;
}
return $port;
}
}

View File

@@ -0,0 +1,154 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use think\facade\Env;
use think\worker\Http as HttpServer;
/**
* Worker 命令行类
*/
class Worker extends Command
{
protected $config = [];
public function configure()
{
$this->setName('worker')
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
->setDescription('Workerman HTTP Server for ThinkPHP');
}
public function execute(Input $input, Output $output)
{
$action = $input->getArgument('action');
if (DIRECTORY_SEPARATOR !== '\\') {
if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
$output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .</error>");
return false;
}
global $argv;
array_shift($argv);
array_shift($argv);
array_unshift($argv, 'think', $action);
} elseif ('start' != $action) {
$output->writeln("<error>Not Support action:{$action} on Windows.</error>");
return false;
}
if ('start' == $action) {
$output->writeln('Starting Workerman http server...');
}
$this->config = Config::pull('worker');
if (isset($this->config['context'])) {
$context = $this->config['context'];
unset($this->config['context']);
} else {
$context = [];
}
$host = $this->getHost();
$port = $this->getPort();
$worker = new HttpServer($host, $port, $context);
if (empty($this->config['pidFile'])) {
$this->config['pidFile'] = Env::get('runtime_path') . 'worker.pid';
}
// 避免pid混乱
$this->config['pidFile'] .= '_' . $port;
// 设置应用目录
$worker->setAppPath($this->config['app_path']);
unset($this->config['app_path']);
// 开启守护进程模式
if ($this->input->hasOption('daemon')) {
$worker->setStaticOption('daemonize', true);
}
// 开启HTTPS访问
if (!empty($this->config['ssl'])) {
$this->config['transport'] = 'ssl';
unset($this->config['ssl']);
}
// 设置网站目录
if (empty($this->config['root'])) {
$this->config['root'] = Env::get('root_path') . 'public';
}
$worker->setRoot($this->config['root']);
unset($this->config['root']);
// 设置文件监控
if (DIRECTORY_SEPARATOR !== '\\' && (Env::get('app_debug') || !empty($this->config['file_monitor']))) {
$interval = isset($this->config['file_monitor_interval']) ? $this->config['file_monitor_interval'] : 2;
$paths = isset($this->config['file_monitor_path']) ? $this->config['file_monitor_path'] : [];
$worker->setMonitor($interval, $paths);
unset($this->config['file_monitor'], $this->config['file_monitor_interval'], $this->config['file_monitor_path']);
}
// 全局静态属性设置
foreach ($this->config as $name => $val) {
if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
$worker->setStaticOption($name, $val);
unset($this->config[$name]);
}
}
// 设置服务器参数
$worker->option($this->config);
if (DIRECTORY_SEPARATOR == '\\') {
$output->writeln('You can exit with <info>`CTRL-C`</info>');
}
$worker->start();
}
protected function getHost($default = '0.0.0.0')
{
if ($this->input->hasOption('host')) {
$host = $this->input->getOption('host');
} else {
$host = !empty($this->config['host']) ? $this->config['host'] : $default;
}
return $host;
}
protected function getPort($default = '2346')
{
if ($this->input->hasOption('port')) {
$port = $this->input->getOption('port');
} else {
$port = !empty($this->config['port']) ? $this->config['port'] : $default;
}
return $port;
}
}

View File

@@ -0,0 +1,45 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:gateway 指令有效
// +----------------------------------------------------------------------
return [
// 扩展自身需要的配置
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host' => '0.0.0.0', // 监听地址
'port' => 2348, // 监听端口
'socket' => '', // 完整监听地址
'context' => [], // socket 上下文选项
'register_deploy' => true, // 是否需要部署register
'businessWorker_deploy' => true, // 是否需要部署businessWorker
'gateway_deploy' => true, // 是否需要部署gateway
// Register配置
'registerAddress' => '127.0.0.1:1236',
// Gateway配置
'name' => 'thinkphp',
'count' => 1,
'lanIp' => '127.0.0.1',
'startPort' => 2000,
'daemonize' => false,
'pingInterval' => 30,
'pingNotResponseLimit' => 0,
'pingData' => '{"type":"ping"}',
// BusinsessWorker配置
'businessWorker' => [
'name' => 'BusinessWorker',
'count' => 1,
'eventHandler' => '\think\worker\Events',
],
];

View File

@@ -0,0 +1,56 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Env;
// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:server 指令有效
// +----------------------------------------------------------------------
return [
// 扩展自身需要的配置
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host' => '0.0.0.0', // 监听地址
'port' => 2345, // 监听端口
'socket' => '', // 完整监听地址
'context' => [], // socket 上下文选项
'worker_class' => '', // 自定义Workerman服务类名 支持数组定义多个服务
// 支持workerman的所有配置参数
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => Env::get('runtime_path') . 'worker.pid',
// 支持事件回调
// onWorkerStart
'onWorkerStart' => function ($worker) {
},
// onWorkerReload
'onWorkerReload' => function ($worker) {
},
// onConnect
'onConnect' => function ($connection) {
},
// onMessage
'onMessage' => function ($connection, $data) {
$connection->send('receive success');
},
// onClose
'onClose' => function ($connection) {
},
// onError
'onError' => function ($connection, $code, $msg) {
echo "error [ $code ] $msg\n";
},
];

View File

@@ -0,0 +1,31 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Env;
// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker 指令有效
// +----------------------------------------------------------------------
return [
// 扩展自身需要的配置
'host' => '0.0.0.0', // 监听地址
'port' => 2346, // 监听端口
'root' => '', // WEB 根目录 默认会定位public目录
'app_path' => '', // 应用目录 守护进程模式必须设置(绝对路径)
'file_monitor' => false, // 是否开启PHP文件更改监控调试模式下自动开启
'file_monitor_interval' => 2, // 文件监控检测时间间隔(秒)
'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录
// 支持workerman的所有配置参数
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => Env::get('runtime_path') . 'worker.pid',
];

View File

@@ -0,0 +1,24 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker\facade;
use think\Facade;
/**
* @see \think\worker\Application
* @mixin \think\worker\Application
* @method void initialize() static 初始化应用
* @method void worker(\Workerman\Connection\TcpConnection $connection) static 处理Worker请求
*/
class Application extends Facade
{
}

View File

@@ -0,0 +1,25 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\worker\facade;
use think\Facade;
/**
* @see \think\worker\Worker
* @mixin \think\worker\Worker
* @method void option(array $option) static 参数设置
* @method void start() static 启动服务
* @method void stop() static 停止服务
*/
class Worker extends Facade
{
}

View File

@@ -0,0 +1,276 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\log\driver;
use think\App;
/**
* 本地化调试输出到文件
*/
class File
{
protected $config = [
'time_format' => ' c ',
'single' => false,
'file_size' => 2097152,
'path' => '',
'apart_level' => [],
'max_files' => 0,
'json' => false,
];
protected $app;
// 实例化并传入参数
public function __construct(App $app, $config = [])
{
$this->app = $app;
if (is_array($config)) {
$this->config = array_merge($this->config, $config);
}
if (empty($this->config['path'])) {
$this->config['path'] = $this->app->getRuntimePath() . 'log' . DIRECTORY_SEPARATOR;
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
$this->config['path'] .= DIRECTORY_SEPARATOR;
}
}
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @param bool $append 是否追加请求信息
* @return bool
*/
public function save(array $log = [], $append = false)
{
$destination = $this->getMasterLogFile();
$path = dirname($destination);
!is_dir($path) && mkdir($path, 0755, true);
$info = [];
foreach ($log as $type => $val) {
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg;
}
if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) {
// 独立记录的日志级别
$filename = $this->getApartLevelFile($path, $type);
$this->write($info[$type], $filename, true, $append);
unset($info[$type]);
}
}
if ($info) {
return $this->write($info, $destination, false, $append);
}
return true;
}
/**
* 日志写入
* @access protected
* @param array $message 日志信息
* @param string $destination 日志文件
* @param bool $apart 是否独立文件写入
* @param bool $append 是否追加请求信息
* @return bool
*/
protected function write($message, $destination, $apart = false, $append = false)
{
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
$this->checkLogSize($destination);
// 日志信息封装
$info['timestamp'] = date($this->config['time_format']);
foreach ($message as $type => $msg) {
$info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
}
if (PHP_SAPI == 'cli') {
$message = $this->parseCliLog($info);
} else {
// 添加调试日志
$this->getDebugLog($info, $append, $apart);
$message = $this->parseLog($info);
}
return error_log($message, 3, $destination);
}
/**
* 获取主日志文件名
* @access public
* @return string
*/
protected function getMasterLogFile()
{
if ($this->config['single']) {
$name = is_string($this->config['single']) ? $this->config['single'] : 'worker';
$destination = $this->config['path'] . $name . '.log';
} else {
if ($this->config['max_files']) {
$filename = date('Ymd') . '_worker.log';
$files = glob($this->config['path'] . '*.log');
try {
if (count($files) > $this->config['max_files']) {
unlink($files[0]);
}
} catch (\Exception $e) {
}
} else {
$filename = date('Ym') . DIRECTORY_SEPARATOR . date('d') . '_worker.log';
}
$destination = $this->config['path'] . $filename;
}
return $destination;
}
/**
* 获取独立日志文件名
* @access public
* @param string $path 日志目录
* @param string $type 日志类型
* @return string
*/
protected function getApartLevelFile($path, $type)
{
if ($this->config['single']) {
$name = is_string($this->config['single']) ? $this->config['single'] : 'worker';
$name .= '_' . $type;
} elseif ($this->config['max_files']) {
$name = date('Ymd') . '_' . $type . '_worker';
} else {
$name = date('d') . '_' . $type . '_worker';
}
return $path . DIRECTORY_SEPARATOR . $name . '.log';
}
/**
* 检查日志文件大小并自动生成备份文件
* @access protected
* @param string $destination 日志文件
* @return void
*/
protected function checkLogSize($destination)
{
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
try {
rename($destination, dirname($destination) . DIRECTORY_SEPARATOR . time() . '-' . basename($destination));
} catch (\Exception $e) {
}
}
}
/**
* CLI日志解析
* @access protected
* @param array $info 日志信息
* @return string
*/
protected function parseCliLog($info)
{
if ($this->config['json']) {
$message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
} else {
$now = $info['timestamp'];
unset($info['timestamp']);
$message = implode("\r\n", $info);
$message = "[{$now}]" . $message . "\r\n";
}
return $message;
}
/**
* 解析日志
* @access protected
* @param array $info 日志信息
* @return string
*/
protected function parseLog($info)
{
$requestInfo = [
'ip' => $this->app['request']->ip(),
'method' => $this->app['request']->method(),
'host' => $this->app['request']->host(),
'uri' => $this->app['request']->url(),
];
if ($this->config['json']) {
$info = $requestInfo + $info;
return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
}
array_unshift($info, "---------------------------------------------------------------\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");
unset($info['timestamp']);
return implode("\r\n", $info) . "\r\n";
}
protected function getDebugLog(&$info, $append, $apart)
{
if ($this->app->isDebug() && $append) {
if ($this->config['json']) {
// 获取基本信息
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
$info = [
'runtime' => number_format($runtime, 6) . 's',
'reqs' => $reqs . 'req/s',
'memory' => $memory_use . 'kb',
'file' => count(get_included_files()),
] + $info;
} elseif (!$apart) {
// 增加额外的调试信息
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
$time_str = '[运行时间:' . number_format($runtime, 6) . 's] [吞吐率:' . $reqs . 'req/s]';
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
array_unshift($info, $time_str . $memory_str . $file_load);
}
}
}
}