fast cgi proxy

2025-12-07 0 588

Fastcgi代理

描述

用于分发(a)的代理将请求同步到多个FastCGI服务器。

安装

composer require hollodotme/fast-cgi-proxy

用法

请求分发

代理可以通过以下方式向多个FastCGI服务器分发请求:

  1. 随机
  2. 通过罗宾

随机分布

要设置随机分发,请使用以下示例代码:

 <?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服务器。在此示例中,它将发送到:

  1. 127.0.0.1:9001
  2. 10.100.10.42:9001
  3. /var/run/php7.1-fpm.sock
  4. 127.0.0.1:9001 (再次从头开始)
  5. 等等…

发送请求

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

贡献

欢迎捐款,并将得到充分的认可。有关详细信息,请参见贡献指南。

下载源码

通过命令行克隆项目:

git clone https://github.com/hollodotme/fast-cgi-proxy.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 fast cgi proxy https://www.zuozi.net/31830.html

SeoTags
上一篇: SeoTags
phpGPX
下一篇: phpGPX
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务