Php 服务器之间的SQS Amazon节流请求
我目前正在使用AmazonSQS对消息进行排队处理。我有一个基准测试,我只能以大约400条消息/秒的速度将它放入队列中。我正在使用发送消息到10的上限 我的第二个问题是,SQS被远程使用(即:我有一台服务器正在创建消息,但它不在amazon或EC2实例旁边) 我的目标是增加这个瓶颈,因为我希望至少完成10K请求 这是由于网络延迟导致的失败吗?或者有更好的SQS解决方案或代码调整来实现这一点 SQS的库也是PHP 编辑:添加代码Php 服务器之间的SQS Amazon节流请求,php,amazon-web-services,amazon-sqs,Php,Amazon Web Services,Amazon Sqs,我目前正在使用AmazonSQS对消息进行排队处理。我有一个基准测试,我只能以大约400条消息/秒的速度将它放入队列中。我正在使用发送消息到10的上限 我的第二个问题是,SQS被远程使用(即:我有一台服务器正在创建消息,但它不在amazon或EC2实例旁边) 我的目标是增加这个瓶颈,因为我希望至少完成10K请求 这是由于网络延迟导致的失败吗?或者有更好的SQS解决方案或代码调整来实现这一点 SQS的库也是PHP 编辑:添加代码 use Aws\Sqs\SqsClient; class Sqs i
use Aws\Sqs\SqsClient;
class Sqs implements QueueInterface
{
private static $_instance = null;
private $_client = null;
protected function __construct($setting)
{
if ($this->_client === null) {
try {
$this->_client = SqsClient::factory($setting);
} catch (Exception $e) {
$this->_client = null;
}
}
}
public static function getQueue($setting)
{
if (self::$_instance === null) {
self::$_instance = new Sqs($setting);
}
return self::$_instance;
}
public function put(Data\DataRepository $data, $queueName)
{
$attributes=array();
if (!$this->_client) return false;
return self::_putToClient($this->_client, $data->getData(), $queueName, $attributes);
}
/**
* Put data into the queue using a defined client.
*
* @param mixed $client The client to use.
* @param mixed $data The data to insert.
* @param string $queueName The name of the queue in which to insert.
* @param mixed $attributes Some attributes for the client (QueueAttribute::DELAY_SECONDS)
*
* @return string The job id in the queue or false if a problem happened.
*/
private static function _putToClient($client, $data, $queueName, $attributes=array())
{
try {
$result = $client->createQueue(array('QueueName' => $queueName, 'Attributes' => $attributes));
$queue_url = $result->get('QueueUrl');
$response = $client->sendMessage(array('QueueUrl' => $queue_url, 'MessageBody' => $data));
return $response->getPath('MessageId');
} catch (Exception $e) {
return false;
}
}
}
网络延迟可能会影响您,但您可能还可以做其他一些事情来获得更高的吞吐量 您的代码应该可以正常工作。然而,它肯定不是最优化的<代码>\u putToClient()始终进行两次API调用。只需调用一次
SqsClient::createQueue()
;每次你发信息的时候都打那个电话似乎很奇怪。如果只执行一次,并存储/缓存QueueUrl,则可以在那里消除一些延迟
你也应该看看这本书。这将允许您一次发送10条以上的消息。您可能还想阅读,看看是否有什么方法可以加快SDK的使用速度
我还将发帖子到,看看SQS工程师是否可以指出任何特定于SQS的最佳实践。也许两者都可以。你的代码看起来像什么?您正在使用AWS SDK for PHP吗?@JeremyLindblom yup“AWS/AWS SDK PHP”:“2.4.10”您正在为每条消息创建一个新队列吗?@JeremyLindblom-不,正如您在getQueue中看到的那样-外部代码确保没有调用其他实例(它基本上是一个单例)我看到您的SQS类是一个单例,但是
\u putToClient
方法同时调用createQueue
和sendMessage
。这似乎真的很奇怪。除了“升级你的PHP”之外,似乎还有很多设置可以关闭,以便消息可以更快地传递。谢谢你的意见