zoukankan      html  css  js  c++  java
  • PHP使用MQ消息队列

    1.安装php-amqplib 服务
    在composer.json配置

    {
    "require": {
    "php-amqplib/php-amqplib": ">=2.6.1"
    }
    }
    

    2.执行composer.phar install 来安装

    3.引入mq文件

    define('EXTEND_PATH', '../vendor/autoload.php');
    use app\synchronous\model\RabbitMqModel;
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    

    4.发送到队列数据代码

    /**
    * MQ生产数据
    * @param $queueName 队列名称
    * @param $msg 发送数据
    * @name MqPushLish
    * @author 
    * @return
    */
    public function MqPublish($queueName , $msg = []){
    try{
    if(empty($queueName))
    return false;
    //获取mq配置
    $mqConfig = $this->getConfig();
    //创建连接和channel
    $connection = new AMQPStreamConnection($mqConfig['host'] , $mqConfig['port'] , $mqConfig['user'] , $mqConfig['password']);
    $channel = $connection->channel();
    $name = $queueName;
    $type = "direct";
    $passive = false;
    $durable = true;
    $auto_delete = true;
    $channel->exchange_declare($name, $type, $passive, $durable, $auto_delete);
    $message = new AMQPMessage('[{"data_id":184981,"complex_id":7821,"area":"bj","dj":0,"mj":84,"shi":"2室","ting":"2厅","wei":"1卫","chu":"1厨","cate_status":"4","thumb":"http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png","tujis":["http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png"],"create_time":1512937831,"update_time":1533907683,"house_toward":"南北","reference_totalprice":0,"reference_down_payment":""},{"data_id":184981,"complex_id":9057,"area":"sh","dj":0,"mj":84,"shi":"2室","ting":"2厅","wei":"1卫","chu":"1厨","cate_status":"4","thumb":"http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png","tujis":["http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png"],"create_time":1512937831,"update_time":1533907683,"house_toward":"南北","reference_totalprice":0,"reference_down_payment":""}]');
    $channel->basic_publish($message,'', $queueName);//发送数据到MQ
    $channel->close();
    $connection->close();
    //打印日志
    DeShangLog::log(0, $msg ,$queueName.'MQ发送队列数据正常');
    return true;
    }catch (\Exception $e){
    //打印日志
    DeShangLog::log(1, $e->getMessage() ,'MQ发送队列数据异常!!!!!!!!!!!!!!!!');
    return false;
    }
    }
    

    5.消费队列数据代码

    /**
    * MQ消费数据 监视数据
    * @param $queueName 队列名称
    * @name MqConsumer
    * @author Admin
    * @return
    */
    public function mqConsumer(){
    try{
    $queueName = $this->getx('queue' , 'complex_info_test');
    if(empty($queueName)){
    echo "not queue ";die;
    }
    //创建连接和channel
    $connection = new AMQPStreamConnection(C('config_mq.host') , C('config_mq.port') , C('config_mq.user') , C('config_mq.password'));
    $channel = $connection->channel();
    $channel->queue_declare($queueName, false, true, false, false);
    echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
    $callback = function($msg) {
    //打印日志,记录消费的数据
    DeShangLog::log(0, $msg->body ,'MQ接收户型同步数据');
    $returnData = json_decode($msg->body , true);
    //调用插入户型数据
    $this->addDoorList($returnData);
    echo " [x] Received ", $msg->body, "\n";
    };
    $channel->basic_consume($queueName, '', false, false, false, false, $callback);//消费出数据
    while(count($channel->callbacks)) {
    $channel->wait();
    }
    $channel->close();
    $connection->close();
    }catch (\Exception $e){
    //打印日志
    DeShangLog::log(1, $e->getMessage() ,'MQ发送队列数据异常!!!!!!!!!!!!!!!!');
    return false;
    }
    }
    
    6.在linux 配置守护进程
    
     命令:``` nohup php index.php /synchronous/synchronous/mqconsumer & ``` “&” 代表不间断运行
        在/etc/rc.local文件中,将 ``` nohup php index.php /synchronous/synchronous/mqconsumer
     ```这个命令加入即可。
    ps:注意路径
    

  • 相关阅读:
    Spring Boot与Redis的集成
    Spring Boot 使用YAML配置
    Spring Boot与MyBatis的集成
    springboot 初识
    微服务架构
    Spring MVC 国际化
    Spring MVC 为控制器添加通知与处理异常
    Spring MVC 数据转换和格式化
    Spring MVC 上传文件
    Spring MVC 实例:Excel视图的使用
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15452966.html
Copyright © 2011-2022 走看看