php swoole+amqp+rabbitmq整合多进程启动channel被locked问题

唐小明生 发布于 2016/01/13 10:36
阅读 1K+
收藏 4

运行环境:swoole 1.7.21 稳定版 ,mac os x 10.10 , php 5.5, rabbitmq3.5, amqp 1.6

<?php
error_reporting(E_ALL^E_NOTICE);
date_default_timezone_set('Asia/Shanghai');

class send
{

    private $redis;
    private $serv;
    private $mq_conn=null;
    private $mq_channel=null;
    private $mq_exchange=null;
    private $mq_queue=null;
    private $mq_exchange_name = "swoole_exchanger_name";
    private $mq_router_name="swoole_router_name";
    private $mq_queue_name="swoole_queue_name";
    private $mq=array();




    public function __construct() {


        $this->serv = new swoole_http_server(
            "0.0.0.0",
            "9501"
        );

        $this->serv->set(array(
            'worker_num' => 8,   //工作进程数量
            'max_conn' => 1000,
            'max_request' => 100000,
            'debug_mode'=> 1,
            'task_worker_num'=>8
            //'daemonize' => true, //是否作为守护进程
        ));


        $this->serv->on('Start', array($this, 'onStart'));
        $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->serv->on('connect', array($this, 'onConnect'));
        $this->serv->on('request' , array( $this , 'onRequest'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));
        $this->serv->on('Task', array($this, 'onTask'));
        $this->serv->on('Finish', array($this, 'onFinish'));
        $this->serv->start();
    }

    public function onStart( $serv ) {
        echo "Start\n";
    }

    public function onRequest($request, $response){

        $this->serv->task("");
        $response->end("hello swoole rabbitmq");
    }
    public function onConnect($serv, $fd){

        echo "Connected\n";
    }

    public function onWorkerStart( $serv , $worker_id) {

        $serv->mq['conn'] = new AMQPConnection();
        $serv->mq['conn'] ->setHost('127.0.0.1');
        $serv->mq['conn'] ->setLogin('guest');
        $serv->mq['conn'] ->setPassword('guest');
        $serv->mq['conn'] ->connect();
        $serv->mq['channel'] = new AMQPChannel($serv->mq['conn']);

        $serv->mq['exchange'] = new AMQPExchange($serv->mq['channel']);
        $serv->mq['exchange']->setType(AMQP_EX_TYPE_DIRECT);
        $serv->mq['exchange']->setName($this->mq_exchange_name);
        $serv->mq['exchange']->declareExchange();

        $serv->mq['queue'] = new AMQPQueue($serv->mq['channel']);
        $serv->mq['queue']->setFlags(AMQP_EXCLUSIVE);
        $serv->mq['queue']->setName($this->mq_queue_name);
        $serv->mq['queue']->declareQueue();

        $serv->mq['queue']->bind($this->mq_exchange_name, $this->mq_queue_name);
    }


    public function onReceive(swoole_server $serv, $fd, $from_id, $data){
        $serv->task("");
    }

    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }




    public function onTask($serv,$task_id,$from_id, $data) {
        //准备在这里把消息发送到rabbitmq队列里
    }

    public function onFinish($serv,$task_id, $data) {

    }

}

new send();

在php cli模式下运行  报以下错,上述问题需要解决多进程连接rabbitmq问题,rabbitmq通道被locked

#[root@winfan] php send.php

Fatal error: Uncaught exception 'AMQPQueueException' with message 'Server channel error: 405, message: RESOURCE_LOCKED - cannot obtain exclusive access to locked queue 'swoole_queue_name' in vhost '/'' in /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php:88

Stack trace:

#0 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(88): AMQPQueue->declareQueue()

#1 [internal function]: send->onWorkerStart(Object(swoole_http_server), 1)

#2 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(48): swoole_http_server->start()

#3 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(120): send->__construct()

#4 {main}




加载中
1
唐小明生
唐小明生

引用来自“太阳黑子”的评论

请问如何解决的

己解决了,请看如下连接

https://github.com/winfan/swoole_integration_rabbitmq

太阳黑子
太阳黑子
多谢分享经验
0
太阳黑子
太阳黑子
请问如何解决的
返回顶部
顶部