【发布时间】:2014-03-27 13:31:45
【问题描述】:
我目前正在使用 Amazon SQS 将消息排队等待处理。我有基准,我只能以大约 400 条消息/秒的速度将 lib 放入队列中。我正在使用发送消息的上限到 10。
我的第二个问题是远程使用 SQS(即:我有一台服务器正在创建不在亚马逊或 EC2 实例旁边的消息)。
我的目标是增加这个瓶颈,因为我想至少做 10K 的请求。
这是因为网络延迟而失败的成就吗?或者是否有更好的 SQS 解决方案或代码调整来实现这一点。
SQS 的库也是 PHP。
编辑:添加代码
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;
}
}
}
【问题讨论】:
-
也许两者兼而有之。你的代码是什么样的?您正在使用适用于 PHP 的 AWS 开发工具包吗?
-
@JeremyLindblom 是的 "aws/aws-sdk-php": "2.4.10"
-
您是否为每条消息创建一个新队列?
-
@JeremyLindblom - 不,正如您从 getQueue 中看到的那样 - 外部代码确保没有其他实例被调用(它基本上是一个单例)
-
我看到您的 SQS 类是单例的,但
_putToClient方法同时调用了createQueue和sendMessage。这看起来很奇怪。
标签: php amazon-web-services amazon-sqs