当前位置:首页 > PHP > 正文内容

php端口复用,php socket端口复用

高老师6年前 (2019-11-04)PHP15624

第一次听说端口复用是在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暂时对内核实现不完整,测试不出负载调度效果。

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/111.html

分享给朋友:

“php端口复用,php socket端口复用” 的相关文章

PHP生成微信分享需要的signature签名

PHP生成微信分享需要的signature签名

因为一个TP项目中客户需要全部网页分享支持自定义图片和描述信息,于是自己封装了下 //share()微信分享链接 //参数1 appid //参数2 appsert //参数3 nonceStr随机码 //参数4 timestamp时间戳 public&nb...

PHP浮点数乘以整数还是浮点型

PHP浮点数乘以整数还是浮点型

<?PHP     $a=0.5*100;     if(is_int($a)){   echo'int'; } else{ echo'not int';   }    ...

php抽奖概率算法

php抽奖概率算法

<?php /*  *算法学习自百度.只是学习和记录  */ header("Content-type:text/html;charset=utf-8"); //1.设置奖项,id是奖项id,name是中奖名称,v是中奖概率 $arr =&n...

php 将数组键值转为变量

php 将数组键值转为变量

<?php $data=array('a'=>1,'b'=>2,'c'=>3,'d'=>4); extract($data); var_dump($a,$b,$c,$d); ?>在人人商城中捡到的...

 php max input vars,max input vars限制,max input vars 设置

php max input vars,max input vars限制,max input vars 设置

在一个正式项目中操作人员提交239个产品信息进行保存,但是系统却提示没有提交239个产品,于是开启错误信息,显示如下:Warning: Unknown: Input variables exceeded 1000. To incr...

PHP二维数组排序,PHP多维数组排序, array_multisort()

PHP二维数组排序,PHP多维数组排序, array_multisort()

使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...