Fastcgi代理
描述
用于分发(a)的代理将请求同步到多个FastCGI服务器。
安装
composer require hollodotme/fast-cgi-proxy
用法
请求分发
代理可以通过以下方式向多个FastCGI服务器分发请求:
- 随机
- 通过罗宾
随机分布
要设置随机分发,请使用以下示例代码:
<?php declare (strict_types= 1 ); namespace YourVendor \\ YourProject ; use hollodotme \\ FastCGI \\ Proxy ; use hollodotme \\ FastCGI \\ Collections \\ Random ; use hollodotme \\ FastCGI \\ SocketConnections \\ NetworkSocket ; use hollodotme \\ FastCGI \\ SocketConnections \\ UnixDomainSocket ; $ random = Random:: fromConnections ( new NetworkSocket ( \' 127.0.0.1 \' , 9000 ), new NetworkSocket ( \' 10.100.10.42 \' , 9000 ), new UnixDomainSocket ( \' /var/run/php7.3-fpm.sock \' ) ); $ proxy = new Proxy ( $ random );
现在发送请求时,代理将随机选择一个FastCGI服务器来处理请求。
循环分布
要设置Round Robin分发,请使用以下示例代码:
<?php declare (strict_types= 1 ); namespace YourVendor \\ YourProject ; use hollodotme \\ FastCGI \\ Proxy ; use hollodotme \\ FastCGI \\ Collections \\ RoundRobin ; use hollodotme \\ FastCGI \\ SocketConnections \\ NetworkSocket ; use hollodotme \\ FastCGI \\ SocketConnections \\ UnixDomainSocket ; $ roundRobin = RoundRobin:: fromConnections ( new NetworkSocket ( \' 127.0.0.1 \' , 9000 ), new NetworkSocket ( \' 10.100.10.42 \' , 9000 ), new UnixDomainSocket ( \' /var/run/php7.3-fpm.sock \' ) ); $ proxy = new Proxy ( $ roundRobin );
代理人将以与RoundRobin实例相同的顺序将您的请求发送到下一个FastCGI服务器。在此示例中,它将发送到:
-
127.0.0.1:9001 -
10.100.10.42:9001, -
/var/run/php7.1-fpm.sock -
127.0.0.1:9001(再次从头开始) - 等等…
发送请求
Proxy类具有与基础客户端类相同的方法,用于发送(a)同步请求和检索响应(反应性地)。因此,请咨询Hollodotme/fast-CGI-CLIENT的文档以获取更多信息。
这只是可用方法的简短列表:
-
$proxy->sendRequest(ProvidesRequestData $request) : ProvidesResponseData
发送同步请求并返回响应。 (阻止) -
$proxy->sendAsyncRequest(ProvidesRequestData $request) : int
发送异步请求并返回请求ID。 (非块) -
$proxy->readResponse(int $requestId, ?int $timeoutMs = null) : ProvidesResponseData
读取并返回先前获得的请求ID的响应。
(阻止读取或读取响应的时间) -
$proxy->readResponses(?int $timeoutMs = null, int ...$requestIds) : \\Generator|ProvidesResponseData[]
根据给定请求ID的顺序读取并产生先前获得的请求ID的响应。
(阻止读取或读取所有响应的时间) -
$proxy->readReadyResponses(?int $timeoutMs = null) : \\Generator|ProvidesResponseData[]
读取并产生所有完成请求的响应。
(非块,旨在用于循环中) -
$proxy->waitForResponse(int $requestId, ?int $timeoutMs = null) : void
等待先前获得的请求ID的响应,并调用请求的响应回调。
(阻止读取或读取响应的时间) -
$proxy->waitForResponses(?int $timeoutMs = null) : void
等待以完成请求的顺序等待先前获得的请求ID的响应,并致电相应的响应回调。
(阻止读取或读取所有响应的时间) -
$proxy->hasResponse(int $requestId) : bool
返回给定请求ID是否具有响应。 (非块) -
$proxy->handleResponse(int $requestId, ?int $timeoutMs = null) : void
调用已经完成请求的相应响应回调。
(如果请求ID具有响应,必须在调用此方法之前检查响应,请参见$proxy->hasResponse(int $requestId))。 -
$proxy->hasUnhandledResponses() : bool
如果剩下没有响应的响应,则返回为true,否则为false。 -
$proxy->getRequestIdsHavingResponse() : array
返回具有响应的所有请求ID。 (非块) -
$proxy->handleResponses(?int $timeoutMs = null, int ...$requestIds) : void
根据给定请求ID的顺序调用已经完成的请求的相应响应回调。
(如果请求ID必须在调用此方法之前检查响应,请参见$proxy->hasResponse(int $requestId)或$proxy->getRequestIdsHavingResponse() : array。) -
$proxy->handleReadyResponses(?int $timeoutMs = null) : void
按完成请求的顺序调用相应的响应回调。
(非障碍物,$proxy->handleResponses($timeoutMs, int ...$proxy->getRequestIdsHavingResponse())))
集群请求
自本库的v0.2.0以来,此功能可用。
为了在众多FASTCGI服务器上处理单个请求,引入了ClusterProxy类。因此,为了将请求分配给已配置的FASTCGI服务器之一,群集代理将向所有配置的FastCGI服务器发送相同的请求,并允许您读取/处理其响应(反应性地)。
根据集群请求的概念,请求和响应总是有一对一的关系。这就是为什么ClusterProxy类不提供同步请求和基于请求ID的单个响应的读数的原因。
要设置集群代理,请使用以下示例代码:
<?php declare (strict_types= 1 ); namespace YourVendor \\ YourProject ; use hollodotme \\ FastCGI \\ ClusterProxy ; use hollodotme \\ FastCGI \\ Collections \\ Cluster ; use hollodotme \\ FastCGI \\ SocketConnections \\ NetworkSocket ; use hollodotme \\ FastCGI \\ SocketConnections \\ UnixDomainSocket ; $ cluster = Cluster:: fromConnections ( new NetworkSocket ( \' 127.0.0.1 \' , 9000 ), new NetworkSocket ( \' 10.100.10.42 \' , 9000 ), new UnixDomainSocket ( \' /var/run/php7.3-fpm.sock \' ) ); $ clusterProxy = new ClusterProxy ( $ cluster );
在集群代理类中可用以下减少的发送请求和处理响应的方法集:
-
$clusterProxy->sendAsyncRequest(ProvidesRequestData $request) : void
向集群中的所有连接发送异步请求。 (非块) -
$clusterProxy->readReadyResponses(?int $timeoutMs = null) : \\Generator|ProvidesResponseData[]
读取并产生所有完成请求的响应。
(非块,旨在用于循环中) -
$clusterProxy->waitForResponses(?int $timeoutMs = null) : void
等待以完成请求的顺序等待先前获得的请求ID的响应,并致电相应的响应回调。
(阻止读取或读取所有响应的时间) -
$clusterProxy->hasUnhandledResponses() : bool
如果剩下没有响应的响应,则返回为true,否则为false。 -
$clusterProxy->handleReadyResponses(?int $timeoutMs = null) : void
按完成请求的顺序调用相应的响应回调。
(非块,旨在与$clusterProxy->hasUnhandledResponses())结合使用。
群集状态
自本库的v0.2.0以来,此功能可用。
为了在集群中检索所有FastCGI服务器的状态,引入了方法ClusterProxy#getStatus() 。
当前,此方法仅支持PHP-FPM的状态响应实现,但是可以通过实现接口hollodotme\\FastCGI\\Interfaces\\ProvidesServerStatus来轻松扩展其他FASTCGI服务器。
<?php declare (strict_types= 1 ); namespace hollodotme \\ FastCGI \\ Interfaces ; interface ProvidesServerStatus { /** * Returns the original response object for the status request provided by hollodotme/fast-cgi-client * @see https://g**i*thub.com/hollodotme/fast-cgi-client/blob/2.x-stable/src/Responses/Response.php * * @return ProvidesResponseData */ public function getResponse () : ProvidesResponseData ; /** * Returns the connection object used for the status request * in order to identify the server that produced the status response * * @return ConfiguresSocketConnection */ public function getConnection () : ConfiguresSocketConnection ; /** * Returns any data structure representing the status information of the server * @return mixed */ public function getStatus (); /** * Returns a list of any data structure representing current processes running on the server * @return array */ public function getProcesses () : array ; }
群集状态示例
以下代码读取属于此库的Docker-Compose设置的一部分的所有3个PHP-FPM容器的状态。
请注意:如果在服务器的配置( pm.status_path for php-fpm)中未启用状态端点,则ClusterProxy#getStatus()方法将抛出RuntimeException 。
示例/cluster_status.php
<?php declare (strict_types= 1 ); namespace hollodotme \\ FastCGI \\ Examples ; use hollodotme \\ FastCGI \\ ClusterProxy ; use hollodotme \\ FastCGI \\ Collections \\ Cluster ; use hollodotme \\ FastCGI \\ Responses \\ PhpFpmStatusResponse ; use hollodotme \\ FastCGI \\ SocketConnections \\ NetworkSocket ; require_once __DIR__ . \' /../vendor/autoload.php \' ; $ cluster = Cluster:: fromConnections ( new NetworkSocket ( \' php71 \' , 9001 ), new NetworkSocket ( \' php72 \' , 9001 ), new NetworkSocket ( \' php73 \' , 9001 ) ); $ clusterProxy = new ClusterProxy ( $ cluster ); $ statusResponses = $ clusterProxy -> getStatus ( \' /status?full \' ); # If you do not want the list processes, use the following line to get the status only # $statusResponses = $clusterProxy->getStatus( \'/status\' ); /** @var PhpFpmStatusResponse $statusResponse */ foreach ( $ statusResponses as $ statusResponse ) { $ connection = $ statusResponse -> getConnection (); $ status = $ statusResponse -> getStatus (); $ processes = $ statusResponse -> getProcesses (); $ response = $ statusResponse -> getResponse (); echo \' [ SERVER: \' , $ connection -> getSocketAddress (), \" ] \\n\" ; echo \' - Pool name: \' , $ status -> getPoolName (), \"\\n\" ; echo \' - Process manager: \' , $ status -> getProcessManager (), \"\\n\" ; echo \' - Started at: \' , $ status -> getStartTime ()-> format ( \' c \' ), \"\\n\" ; echo \' - Seconds since start: \' , $ status -> getStartSince (), \"\\n\" ; echo \' - Number of accepted connections: \' , $ status -> getAcceptedConnections (), \"\\n\" ; echo \' - Current listen queue: \' , $ status -> getListenQueue (), \"\\n\" ; echo \' - Listen queue maximum: \' , $ status -> getMaxListenQueue (), \"\\n\" ; echo \' - Listen queue length: \' , $ status -> getListenQueueLength (), \"\\n\" ; echo \' - Number of idle processes: \' , $ status -> getIdleProcesses (), \"\\n\" ; echo \' - Number of active processes: \' , $ status -> getActiveProcesses (), \"\\n\" ; echo \' - Number of total processes: \' , $ status -> getTotalProcesses (), \"\\n\" ; echo \' - Number of active processes maximum: \' , $ status -> getMaxActiveProcesses (), \"\\n\" ; echo \' - Times max children reached: \' , $ status -> getMaxChildrenReached (), \"\\n\" ; echo \' - Number of slow requests: \' , $ status -> getSlowRequests (), \"\\n\" ; echo \"\\n Printing processes: \\n\\n\" ; foreach ( $ processes as $ index => $ process ) { echo \' - [ PROCESS # \' , ( $ index + 1 ), \" ] \\n\" ; echo \' * PID: \' , $ process -> getPid (), \"\\n\" ; echo \' * State: \' , $ process -> getState (), \"\\n\" ; echo \' * Started at: \' , $ process -> getStartTime ()-> format ( \' c \' ), \"\\n\" ; echo \' * Seconds since start: \' , $ process -> getStartSince (), \"\\n\" ; echo \' * Number of requests processed: \' , $ process -> getRequests (), \"\\n\" ; echo \' * Last request duration: \' , $ process -> getRequestDuration (), \"\\n\" ; echo \' * Last request method: \' , $ process -> getRequestMethod (), \"\\n\" ; echo \' * Last request URI: \' , $ process -> getRequestUri (), \"\\n\" ; echo \' * Last content length: \' , $ process -> getContentLength (), \"\\n\" ; echo \' * Last user: \' , $ process -> getUser (), \"\\n\" ; echo \' * Last script: \' , $ process -> getScript (), \"\\n\" ; echo \' * CPU usage of last request: \' , $ process -> getLastRequestCpu (), \"\\n\" ; echo \' * Memory usage of last request: \' , $ process -> getLastRequestMemory (), \"\\n\" ; echo \"\\n\\n --- \\n\\n\" ; } echo \' Processing duration: \' , $ response -> getDuration (), \" seconds \\n\\n\" ; }
该脚本会产生以下输出
[ SERVER: tcp://php71:9001 ]
- Pool name: network
- Process manager: dynamic
- Started at: 2019-06-10T14:56:45+00:00
- Seconds since start: 18094
- Number of accepted connections: 81
- Current listen queue: 0
- Listen queue maximum: 0
- Listen queue length: 128
- Number of idle processes: 1
- Number of active processes: 1
- Number of total processes: 2
- Number of active processes maximum: 2
- Times max children reached: 0
- Number of slow requests: 0
Printing processes:
- [ PROCESS #1 ]
* PID: 8
* State: Idle
* Started at: 2019-06-10T14:56:45+00:00
* Seconds since start: 18094
* Number of requests processed: 40
* Last request duration: 190
* Last request method: -
* Last request URI: -
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 2097152
---
- [ PROCESS #2 ]
* PID: 9
* State: Running
* Started at: 2019-06-10T14:56:45+00:00
* Seconds since start: 18094
* Number of requests processed: 41
* Last request duration: 190
* Last request method: GET
* Last request URI: /status?full
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 0
---
Processing duration: 0.0137939453125 seconds
[ SERVER: tcp://php72:9001 ]
- Pool name: network
- Process manager: dynamic
- Started at: 2019-06-10T14:56:46+00:00
- Seconds since start: 18093
- Number of accepted connections: 75
- Current listen queue: 0
- Listen queue maximum: 0
- Listen queue length: 128
- Number of idle processes: 1
- Number of active processes: 1
- Number of total processes: 2
- Number of active processes maximum: 2
- Times max children reached: 0
- Number of slow requests: 0
Printing processes:
- [ PROCESS #1 ]
* PID: 10
* State: Idle
* Started at: 2019-06-10T14:56:46+00:00
* Seconds since start: 18093
* Number of requests processed: 38
* Last request duration: 217
* Last request method: -
* Last request URI: -
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 2097152
---
- [ PROCESS #2 ]
* PID: 11
* State: Running
* Started at: 2019-06-10T14:56:46+00:00
* Seconds since start: 18093
* Number of requests processed: 37
* Last request duration: 177
* Last request method: GET
* Last request URI: /status?full
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 0
---
Processing duration: 0.027499914169312 seconds
[ SERVER: tcp://php73:9001 ]
- Pool name: network
- Process manager: dynamic
- Started at: 2019-06-10T14:56:45+00:00
- Seconds since start: 18094
- Number of accepted connections: 1706
- Current listen queue: 0
- Listen queue maximum: 1
- Listen queue length: 128
- Number of idle processes: 2
- Number of active processes: 1
- Number of total processes: 3
- Number of active processes maximum: 23
- Times max children reached: 0
- Number of slow requests: 0
Printing processes:
- [ PROCESS #1 ]
* PID: 331
* State: Idle
* Started at: 2019-06-10T17:00:25+00:00
* Seconds since start: 10674
* Number of requests processed: 383
* Last request duration: 185
* Last request method: -
* Last request URI: -
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 2097152
---
- [ PROCESS #2 ]
* PID: 497
* State: Running
* Started at: 2019-06-10T17:31:02+00:00
* Seconds since start: 8837
* Number of requests processed: 59
* Last request duration: 244
* Last request method: GET
* Last request URI: /status?full
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 0
---
- [ PROCESS #3 ]
* PID: 315
* State: Idle
* Started at: 2019-06-10T16:42:27+00:00
* Seconds since start: 11752
* Number of requests processed: 433
* Last request duration: 230
* Last request method: -
* Last request URI: -
* Last content length: 0
* Last user: -
* Last script: -
* CPU usage of last request: 0
* Memory usage of last request: 2097152
---
Processing duration: 0.029183149337769 seconds
贡献
欢迎捐款,并将得到充分的认可。有关详细信息,请参见贡献指南。
