使用TcpListenerInterface约束该服务为tcp基础协议
class HttpServer implements ProcessInterface, TcpListenerInterface
{
/** @var App|null */
protected $app;
/** @inheritDoc */
public function onWorkerStart(Worker $worker)
{
require_once base_path() . '/support/bootstrap.php';
$this->app = new App($worker, Container::instance(), Log::channel('default'), app_path(), public_path());
Http::requestClass(config('app.request_class', config('server.request_class', Request::class)));
}
/**
* @param TcpConnection $connection
* @param $data
* @return void
*/
public function onMessage(TcpConnection $connection, $data){
call_user_func([$this->app, 'onMessage'], $connection, $data);
}
/** @inheritDoc */
public function onWorkerStop(Worker $worker){}
/** @inheritDoc */
public function onWorkerReload(Worker $worker){}
/** @inheritDoc */
public function onWorkerExit(Worker $worker, $status, $pid){}
}
而不是使用配置
return [
'listen' => 'http://0.0.0.0:8787',
'transport' => 'tcp',
'context' => [],
'name' => 'webman',
'count' => cpu_count() * 2,
'user' => '',
'group' => '',
'reusePort' => false,
'event_loop' => '',
'stop_timeout' => 2,
'pid_file' => runtime_path() . '/webman.pid',
'status_file' => runtime_path() . '/webman.status',
'stdout_file' => runtime_path() . '/logs/stdout.log',
'log_file' => runtime_path() . '/logs/workerman.log',
'max_package_size' => 10 * 1024 * 1024
];
foreach ($config['services'] ?? [] as $server) {
if (!class_exists($server['handler'])) {
echo "process error: class {$server['handler']} not exists\r\n";
continue;
}
$listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
if (isset($server['listen'])) {
echo "listen: {$server['listen']}\n";
}
$instance = Container::make($server['handler'], $server['constructor'] ?? []);
worker_bind($listen, $instance);
$listen->listen();
}
将start.php中master相关的内容移至helper.php的master_init()
function master_init(array $config){
Worker::$pidFile = $config['pid_file'] ?? runtime_path() . '/webman.pid';
Worker::$stdoutFile = $config['stdout_file'] ?? runtime_path() . '/logs/stdout.log';
Worker::$logFile = $config['log_file'] ?? runtime_path() . '/logs/workerman.log';
Worker::$eventLoopClass = $config['event_loop'] ?? '';
TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10 * 1024 * 1024;
if (property_exists(Worker::class, 'statusFile')) {
Worker::$statusFile = $config['status_file'] ?? '';
}
if (property_exists(Worker::class, 'stopTimeout')) {
Worker::$stopTimeout = $config['stop_timeout'] ?? 2;
}
Worker::$onMasterReload = function () {
if (function_exists('opcache_get_status') and function_exists('opcache_invalidate')) {
if ($status = opcache_get_status()) {
if (isset($status['scripts']) && $scripts = $status['scripts']) {
foreach (array_keys($scripts) as $file) {
opcache_invalidate($file, true);
}
}
}
}
};
}
app.php
return [
'debug' => true,
'error_reporting' => E_ALL,
'default_timezone' => 'Asia/Shanghai',
'request_class' => Request::class,
'public_path' => base_path() . DIRECTORY_SEPARATOR . 'public',
'runtime_path' => base_path(false) . DIRECTORY_SEPARATOR . 'runtime',
'controller_suffix' => '',
'event_loop' => '',
'stop_timeout' => 2,
'pid_file' => runtime_path() . '/webman.pid',
'status_file' => runtime_path() . '/webman.status',
'stdout_file' => runtime_path() . '/logs/stdout.log',
'log_file' => runtime_path() . '/logs/workerman.log',
'max_package_size' => 10 * 1024 * 1024
];
webman是否可以提供测试用例,以便我测试我的PR代码是否合规,也方便其他开发者PR的自审;比如 https://www.dtm.pub/other/develop.html
webman是否可以提供issue模板或者是开源参与的流程,用于区分是bug还是提问亦或是Request for Comments,这样其他开发者可以更好的参与进来,也可以在PR的时候关联对应的issue;
webman是否可以在官网描述中提供并强调一下框架的设计哲学或者说设计理念,以便开源贡献者或使用者能够更深的理解框架的内涵,统一思路;
1、你的想法和我一样。目前在开发中的1.4版本可以用process.php配置多个httpserver,也在考虑将server.php合并到process.php。但是pid_file stdout_file这部分作为服务器配置放在应用配置app.php感觉也不太合适,单独放在server.php又感觉啰嗦。可能还是要有个server.php,里面只配置pid_file stdout_file等这些,不再配置http服务。1.4版本将master内容封装到support/App.php 类中了,类似你说的master_init()方法。
worker_start()有一段关于配置services的处理,这部分于微服务没有关系,是用来实现在一个进程内实现多个端口的监听。这部分用的人不多,后面可能会去掉。
2、目前没有测试用例。没有issue模版,issue大部分是使用咨询,没必要用模版。如果有小的优化改动直接发pr即可。
3、webman理念主页有介绍,总结起来就是内核尽量简单,其它靠组件、插件扩展。
okok,worker_start那个services的部分,我想表达的意思就是我觉得services这部分没有太多作用,大部分功能都与自定义进程重叠
大佬们辛苦了,这是要走“微服务”路线吗?(太菜了看不懂全凭猜)
还是web框架啊,只是说概念上把重复的东西抽象成一起,避免冗余的设计,和微服务可能没太大关系,只是提了一嘴