第一次听说端口复用是在mixphp最新版本中发现的,mixphp启动监听9501端口,现在作者说可以多开几个进程来执行mixphp,我心里想了下再启动不是会端口冲突嘛,但是却没有问题,于是下载mixphp的源码解读,原来是启动http服务器使用new Co\Http\Server('0.0.0.0', 80, false, true); swoole这个http构造方法第4个参数是是否开启端口复用,这样就能多个进程来监听相同端口。底层负载调度由linux自动处理。切记需要linux3.10以上内核才支持。我们来做个试验。
【一】.基于swoole的端口复用
(1).创建t1.php:监听80端口
go(function () {
$server = new Co\Http\Server('0.0.0.0', 80, false, true);
$server->handle('/', function ($request, $response) {
$response->end("<h1>T1 is work!</h1>");
echo 'replay from T1' . PHP_EOL;
});
$server->start();
});(2).创建t2.php:监听80端口
go(function () {
$server = new Co\Http\Server('0.0.0.0', 80, false, true);
$server->handle('/', function ($request, $response) {
$response->end("<h1>T2 is work!</h1>");
echo 'replay from T2' . PHP_EOL;
});
$server->start();
});(3).通过ab.exe并发请求:
./ab.exe -n 1000 -c 1000 http://127.0.0.1/
(4).查看服务器php脚本输出:
replay from T2 replay from T1 replay from T2 replay from T1 replay from T1 replay from T2 replay from T1 replay from T2 replay from T1
(5).关闭t1脚本继续访问80端口:
replay from T2 replay from T2 replay from T2
通过上面的验证我们可以知道端口复用能够提高多核利用率,提高socket的处理能力,同时也实现了高可用,一个进程意外退出,其他继续工作,不需要重新建立socket句柄。另外端口重用解决了端口占用不能及时释放的问题,不释放我们可以直接重用。
【二】.基于原生socket的端口复用
(1).创建w1.php:监听8484端口
// 设置IP
$host = '0.0.0.0';
// 设置端口
$port = 8484;
// 创建socket
$listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// 设置端口复用
socket_set_option($listen_socket, SOL_SOCKET, SO_REUSEPORT, 1);
// 绑定端口
socket_bind($listen_socket, $host, $port);
// 监听socket
socket_listen($listen_socket);
// 事件轮询
while (true)
{
// 客户端
$client = socket_accept($listen_socket);
// 向客户端发送消息
$msg = "This is w1\r\n";
// 记录响应来自哪个进程
echo 'replay from W1' . PHP_EOL;
//发送消息
socket_write($client, $msg, strlen($msg));
//关闭客户端
socket_close($client);
}
socket_close($listen_socket);(2).创建w2.php:监听8484端口
// 设置IP
$host = '0.0.0.0';
// 设置端口
$port = 8484;
// 创建socket
$listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// 设置端口复用
socket_set_option($listen_socket, SOL_SOCKET, SO_REUSEPORT, 1);
// 绑定端口
socket_bind($listen_socket, $host, $port);
// 监听socket
socket_listen($listen_socket);
// 事件轮询
while (true)
{
// 客户端
$client = socket_accept($listen_socket);
// 向客户端发送消息
$msg = "This is w2\r\n";
// 记录响应来自哪个进程
echo 'replay from W2' . PHP_EOL;
//发送消息
socket_write($client, $msg, strlen($msg));
//关闭客户端
socket_close($client);
}
socket_close($listen_socket);(3).并发后两个脚本的输出:
replay from W1 replay from W1 replay from W1 replay from W1 replay from W1 replay from W1 replay from W1 replay from W1 replay from W1 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2 replay from W2
Linux底层会自动负载均衡。
注意事项:
linux内核必须大于等于3.10以上
windows下的php原生socket不支持端口复用。
windows下的linux子系统wsl1暂时对内核实现不完整,测试不出负载调度效果。
开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...
先在centos安装openssl,然后开始://生成私钥openssl genrsa -out rsa_private_key.pem 1024//生成公钥openssl rsa -in rsa_private_key.pem&...
面试中PHP面试官会问调用一个不存在的方法,如何知道是哪个文件哪行调用的?假设方法是getWorkLoad()回答1:开启PHP错误输出,PHP会输出Fatal error: Call to undefined function getWorkLoad() in D:\wwwroot\thinkpa...
重构框架的时候想要考虑支持下cli模式,于是参考了thinkphp的底层。/** * 获取应用根目录 * @return string */ public static function getRootP...
(1).在PHP中可以查看的环境变量包括: (1.1).电脑环境变量 (2.1).服务器环境变量(2).getenv()函数获取一个环境变量的值.参数1是环境变量的key,参数2值为true的时候仅从你的电脑环境变量中查找,参数2值为false会从两种变量中全部查询//获取我电脑登录的用户名,输出A...
<?php //php7+ define('CONFIG', [ 'MYSQL' => '127.0.0.1',  ...