Rabbitmq RPC-amq.gen过多

时间:2018-07-16 10:13:24

标签: c# php rabbitmq rpc

我在RPC模式下使用Rabbitmq时遇到问题。我们有一个PHP服务器和一个C#客户端。 但是有太多的自动生成队列。这杀死了我的服务器。 你有什么主意吗 ?有办法快速杀死他们吗? 谢谢, 最好的问候,

更新13h58

这是我的RPC服务器(PHP):

$connection = new AMQPStreamConnection('localhost', 5672, '*', '*');
$channel = $connection->channel();
$channel->queue_declare('rpc_queue', false, false, false, false);

$callback = function($req) {

    $request = json_decode($req->body);

    $someclass = new SomeClass();
    $response = $someclass->someFunction($request);

    $msg = new AMQPMessage(json_encode($response), array('correlation_id' => $req->get('correlation_id')));
    $req->delivery_info['channel']->basic_publish($msg, '', $req->get('reply_to'));
    $req->delivery_info['channel']->basic_ack($req->delivery_info['delivery_tag']);

};

$channel->basic_qos(null, 1, null);
$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);

while(count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();

这是我的客户(C#):

    class RPCClient {

    private IConnection connection;
    private IModel channel;
    private string replyQueueName;
    private QueueingBasicConsumer consumer;

    public RPCClient(){
        try{
            var factory = new ConnectionFactory() { HostName = Conf.hostName, UserName = Conf.userName, Password = Conf.password, RequestedHeartbeat = Conf.heartbeat, AutomaticRecoveryEnabled = true, TopologyRecoveryEnabled = true };
            connection = factory.CreateConnection();
            channel = connection.CreateModel();
            replyQueueName = channel.QueueDeclare().QueueName;
            consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume(queue: replyQueueName, noAck: true, consumer: consumer);
        } catch (Exception e){}
    }

    public string Call(string message) {

        var corrId = Guid.NewGuid().ToString();
        var props = channel.CreateBasicProperties();
        props.ReplyTo = replyQueueName;
        props.CorrelationId = corrId;

        var messageBytes = Encoding.UTF8.GetBytes(message);
        channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: props, body: messageBytes);

        while (true){
            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

            if (ea.BasicProperties.CorrelationId == corrId){
                return Encoding.UTF8.GetString(ea.Body);
            }
        }
    }

    public void Close(){
        connection.Close();
    }
}

0 个答案:

没有答案