PHP+RabbitMq示例展示

注意事项:
1、accept.PHP消费者代码需要在命令行执行

RabbitMQCommand.php操作类代码

<?php
/* 
 * amqp协议操作类,可以访问rabbitMQ 
 * 需先安装php_amqp扩展 
 */ 
class RabbitMQCommand{
    public $configs=array();
    //交换机名称
    public $exchange_name='';
    //队列名称
    public $queue_name='';
    //路由名称
    public $route_key='';
    //持久化
    public $durable=True;
     /* 
     * 自动删除 
     * exchange is deleted when all queues have finished using it 
     * queue is deleted when last consumer unsubscribes 
     *  
     */  
    public $autodelete = False;  
     /*
     * 镜像 
     * 镜像队列,打开后消息会在节点之间复制,有master和slave的概念 
     */
    public $mirror = False;  

    private $_conn = Null;  
    private $_exchange = Null;  
    private $_channel = Null;  
    private $_queue = Null; 

    public function __construct($configs=array(),$exchange_name='',$queue_name='',$route_key=''){
        $this->setConfig($configs);
        $this->exchange_name=$exchange_name;
        $this->queue_name=$queue_name;
        $this->route_key=$route_key;
    }
    private function setConfig($configs){
        if(!is_array($configs)){
            throw new Exception('config is not array');
        }
        if(!($configs['host'] && $configs['port'] && $configs['username'] && $configs['password'])){
            throw new Exception('configs is empty'); 
        }
        if(empty($configs['vhost'])){
            $configs['vhost']='/';
        }
        $configs['login']=$configs['username'];
        unset($configs['username']);
        $this->configs=$configs;
    }
    /* 
     * 设置是否持久化,默认为True 
     */ 
    public function setDurable($durable){
        $this->durable=$durable;
    }
    /* 
     * 设置是否自动删除 
     */ 
    public function setAutoDelete($autodelete){
        $this->autodelete=$autodelete;
    }
    /* 
     * 设置是否镜像 
     */
    public function setMirror($mirror){
        $this->mirror=$mirror;
    }
    /* 
     * 打开amqp连接 
     */  
    private function open(){
        if(!$this->_conn){
            try {
                $this->_conn=new AMQPConnection($this->configs);
                $this->_conn->connect();
                $this->initConnection();
            } catch (AMQPConnectionException $ex) {
                throw new Exception('cannot connection rabbitmq',500);
            }
        }
    }
     /* 
     * rabbitmq连接不变 
     * 重置交换机,队列,路由等配置 
     */
    public function reset($exchange_name,$queue_name,$route_key){
        $this->exchange_name=$exchange_name;
        $this->queue_name=$queue_name;
        $this->route_key=$route_key;
        $this->initConnection();
    }
      /* 
     * 初始化rabbit连接的相关配置 
     */ 
    private function initConnection(){
        if(empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)){
            throw new Exception('rabbitmq exhange_name or queue_name or route_key is empty',500);
        }
        //连接channel
        $this->_channel=new AMQPChannel($this->_conn);
        //创建交换机
        $this->_exchange=new AMQPExchange($this->_channel);
        $this->_exchange->setName($this->exchange_name);
        $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);
        if($this->durable){
            $this->_exchange->setFlags(AMQP_DURABLE);
        }
        if($this->autodelete){
            $this->_exchange->setFlags(AMQP_AUTODELETE);
        }
        $this->_exchange->declare();
        //创建队列
        $this->_queue=new AMQPQueue($this->_channel);
        $this->_queue->setName($this->queue_name);
        if($this->durable){
            $this->_queue->setFlags(AMQP_DURABLE);
        }
        if($this->autodelete){
            $this->_queue->setFlags(AMQP_AUTODELETE);
        }
        if($this->mirror){
            $this->_queue->setArgument('x-ha-policy','all');
        }
        $this->_queue->declare();
        //将队列通过制定路由绑定到指定交换机上
        $this->_queue->bind($this->exchange_name,$this->route_key);

    }

    public function close(){

        if($this->_conn){
            $this->_conn->disconnect();
        }
    }
    public function __sleep(){
        $this->close();
        return array_keys(get_object_vars($this));
    }
    public function __destruct(){
        $this->close();
    }
    /* 
     * 生产者发送消息 
     */ 
     public function send($msg) {  
        $this->open();  
        if(is_array($msg)){  
            $msg = json_encode($msg);  
        }else{  
            $msg = trim(strval($msg));  
        }  
        return $this->_exchange->publish($msg, $this->route_key);  
    } 
     /* 
     * 消费者 
     * $fun_name = array($classobj,$function) or function name string 
     * $autoack 是否自动应答 
     *  
     * function processMessage($envelope, $queue) { 
            $msg = $envelope->getBody();  
            echo $msg."\n"; //处理消息 
            $queue->ack($envelope->getDeliveryTag());//手动应答 
        } 
     */ 
    public function run($fun_name,$autoack=True){
        $this->open();
        if(!$fun_name || !$this->_queue) return false;
        while(True){
            if($autoack) $this->_queue->consume($fun_name,AMQP_AUTOACK);
            else $this->_queue->consume($fun_name);
        }
    }


}

send.php生产者代码

<?php  
set_time_limit(0);  
include_once('RabbitMQCommand.php');  

$configs = array('host'=>'127.0.0.1','port'=>5672,'username'=>'asdf','password'=>'123456','vhost'=>'/');  
$exchange_name = 'class-e-1';  
$queue_name = 'class-q-1';  
$route_key = 'class-r-1';  
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);  
for($i=0;$i<=100;$i++){  
    $ra->send(date('Y-m-d H:i:s',time()));  
}  

accept.php消费者代码

<?php  
error_reporting(0);  
include_once('RabbitMQCommand.php');  


$configs = array('host'=>'127.0.0.1','port'=>5672,'username'=>'asdf','password'=>'123456','vhost'=>'/');  
$exchange_name = 'class-e-1';  
$queue_name = 'class-q-1';  
$route_key = 'class-r-1';  
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);  


class A{  
    function processMessage($envelope, $queue) {  
        $msg = $envelope->getBody();  
        //自增id
        $envelopeID = $envelope->getDeliveryTag();  
        $pid = posix_getpid(); 
        //将取出来的内容放到文件里面 
        file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);  
        $queue->ack($envelopeID);  
    }  
}  
$a = new A();  


$s = $ra->run(array($a,'processMessage'),false); 

消息队列的实现中,RabbitMQ以其健壮和可靠见长.公司的项目中选择了它作为消息队列的实现.关于MQ的机制和原理网上有很多文章可以看,这里就不再赘述,只讲几个比较容易混淆的问题

1,binding key和routing key
  binding key和routing key是都不过是自己设置的一组字符,只是用的地方不同,binding key是在绑定交换机和队列时候通过方法传递的字符串,routing key是在发布消息时候,顺便带上的字符串,有些人说这两个其实是一个东西,也对也不对,说对,是因为这两个可以完全一样,说不对,是因为这两个起的作用不同,一个交换机可以绑定很多队列,但是每个队列也许需要的消息类型不同,binding key就是这个绑定时候留在交换机和队列之间的提示信息,当消息发送出来后,随着消息一起发送的routing key如果和binding key一样就说明消息是这个队列要的东西,如果不一样那就不要给这个队列,交换机你找找下个队列看看要不要.明白了吧,这两个key就是暗号,对上了就是自己人,对不上那麻烦你再找找去.

2,持久化
  交换机和队列都可以在创建时候设置为持久化,重启以后会回复,但是其中的消息未不会,如果要消息也恢复,将消息发布到交换机的时候,可以指定一个标志“Delivery Mode”(投递模式),  1为非持久化,2为持久化.
  
3,流控机制
  当消息生产的速度更快,而进程的处理能力低时,消息就会堆积起来,占用内存越来越多,导致MQ崩溃,所以rabbitmq有一个流控机制,当超过限定时候就会阻止接受消息,mq流控有三种机制
  1,主动阻塞住发消息太快的连接,这个无法调整,如果被阻塞了,在abbitmqctl 控制台上会显示一个blocked的状态。
  2,内存超过限量,会阻塞连接,在vm_memory_high_watermark可调
   3,剩余磁盘在限定以下mq会 主动阻塞所有的生产者,默认为50m,在disk_free_limit可调.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用PHP AMQP库实现多个消费者监听同一个队列的示例代码: ```php <?php require_once __DIR__ . '/vendor/autoload.php'; // 引入AMQP库 use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; // 连接RabbitMQ服务器 $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); // 定义队列名称和属性 $queue_name = 'my_queue'; $queue_attrs = array('passive' => false, 'durable' => true, 'exclusive' => false, 'auto_delete' => false); // 声明队列 $channel->queue_declare($queue_name, $queue_attrs); // 定义回调函数 $callback = function ($msg) { echo "Received message: " . $msg->body . "\n"; sleep(1); // 模拟消息处理时间 }; // 同时启动三个消费者 $channel->basic_consume($queue_name, '', false, true, false, false, $callback); $channel->basic_consume($queue_name, '', false, true, false, false, $callback); $channel->basic_consume($queue_name, '', false, true, false, false, $callback); // 监听队列 while (count($channel->callbacks)) { $channel->wait(); } // 关闭连接 $channel->close(); $connection->close(); ?> ``` 在上面的示例中,我们定义了一个名为`my_queue`的队列,并且启动了三个消费者同时监听该队列。当有消息发送到队列时,RabbitMQ会将其分发给其中一个正在监听该队列的消费者,每个消费者都会收到部分消息。在本例中,我们简单地在回调函数中输出了消息内容,并且在每个消息处理之间模拟了1秒的处理时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值