Yii1.1 配置RabbitMQ

1.什么是RabbitMQ

RabbitMQ原理和安装。可以参考这位大神的文章,这里就不细说了。
消息队列之 RabbitMQ

2.配置RabbitMQ基类

\protected\extensions\rabbitmq\RabbitMq_Base.php

setRabbit($message,$k_route);//设置列队
    }
    public static function getRabbitMq($k_route = 'operate-log' ){
        $rabbitObj = new RabbitMq_Server();
        $getRabObj = $rabbitObj->getRabbit($k_route,'q_'.$k_route);//获取列队
        return $getRabObj;
    }
}
?>
3.配置RabbitMQ服务

\protected\extensions\rabbitmq\RabbitMq_Server.php

'127.0.0.1','port'=>'5672','login'=>'guest','password'=>'guest','vhost'=>'/');   
    private $channel;
    private $e_name = 'test';
 
    public function __construct(){
    //创建Exchannel
    $this->channel = $this->connrabbit($this->conn_args);
    if(!$this->channel){
        echo 'Cannot Connect to the broker';
        exit();
    }
    }
   
    /*
     *创建RabbitMq Message
     */
    public function setRabbit($message,$k_route,$e_name=''){
    if(empty($e_name)){
        $e_name = $this->e_name;
    }
    $channel = $this->channel;
        $ex = new AMQPExchange($channel);
    $ex->setName($e_name);//创建名字
    $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型
    $ex->setFlags(AMQP_DURABLE); //持久化
    $ex->declareExchange();
    $ex->publish($message,$k_route);
    }
   
    /*
     *获取RabbitMq Message
     */
    public function getRabbit($k_route,$q_name){
    $channel = $this->channel;
    $q = new AMQPQueue($channel);
    $q->setName($q_name);
    $q->setFlags(AMQP_DURABLE);
    $q->declare();
    $q->bind($this->e_name, $k_route);
    $messages = $q->get(AMQP_AUTOACK);
    if(is_object( $messages))
        return $messages->getBody();
    return $messages;
    }
 
    /**
     *链接RabbitMQ
     */ 
    protected function connrabbit($conn_args){
    $conn = new AMQPConnection($conn_args);
    if(!$conn->connect()){
        return false;
    }
    $channel = new AMQPChannel($conn);
    return $channel;
    } 
}

你可能感兴趣的:(Yii1.1 配置RabbitMQ)