rabbitmq第三篇:rabbitmq使用及参数设置

前面已经介绍过rabbitmq的安装(一)rabbitmq的php的扩展安装(二),完成这些安装后,我们就可以开始rabbitmq之旅了。


        首先写两个脚本文件,一个生产者脚本(test_publisher.php),一个消费者脚本(test_consumer.php)

        生产者脚本(test_publisher.php),用于循环往rabbitmq中发送数据,(可以选择是发送持久化数据或非持久化数据,在性能与数据安全中择决)

        消费者脚本(test_consumer.php),用作后台运行,用于从rabbitmq中取数据。


        掌握了这两个文件,就可以使用rabbitmq存取数据了,至于存什么样的数据、取出数据后该如何用,这个就是你自身项目的要求来定了。


        关于rabbitmq配制参数,一般默认就够用了,当然你也可以根据自身要求进行修改。本篇不详细介绍,到google或官网看吧。


OK,上代码了

nohup命令的用途:使程序在后台(进程)不挂断地运行,像守护进程一样。使用 jobs 查看所有任务

生产者脚本(test_publisher.php)(运行命令:php test_publisher.php 1  /  nohup php test_publisher.php 1 &)

[php]  view plain  copy
  1. <?php  
  2.   
  3. error_reporting(E_ERROR);  
  4. ini_set('display_errors','Off');  
  5.   
  6. declare(ticks = 1);  
  7.   
  8. $app = 'daemon';  
  9.   
  10. // Bootstrap the application  
  11. require realpath(dirname(__FILE__)) .'/../../../daemon/bootstrap.php';  
  12.   
  13. if(empty($_SERVER['argv'][1])){  
  14.     echo 'use action start or stop'.PHP_EOL;  
  15.     exit;  
  16. }  
  17.   
  18.   
  19. $log = $_SERVER['argv'][1];  
  20.   
  21. $config = array(  
  22.         'host' => '127.0.0.1',  
  23.         'port' => '5672',  
  24.         'login' => 'guest',  
  25.         'password' => 'guest',  
  26.         'vhost'=>'/'  
  27. );  
  28.   
  29. $j=0;  
  30. while ( true )  
  31. {  
  32.         if($j==0)  
  33.         {  
  34.     // 非持久化  
  35.         //$rabbit = new Publisher($config, "bleach_analy_ex_{$log}", "test_{$log}", "bleach_analy_rk_{$log}");  
  36.               
  37.     // 持久化  
  38.     $rabbit = new Publisher($config"bleach_analy_ex_{$log}""test_{$log}""bleach_analy_rk_{$log}""direct", true);  
  39.         }  
  40.         $j++;  
  41.   
  42.         for($i=0;$i<1600;$i++)  
  43.         {  
  44.                 $t = time();  
  45.   
  46.                 $distict = rand(1, 2);  
  47.   
  48.                 $push = true;  
  49.                 if($push==true)  
  50.                 {  
  51.                         $message = "1|3|{$distict}|";  
  52.   
  53.                         $rabbit->send($message"bleach_analy_rk_$log");  
  54.                 }  
  55.         }  
  56.   
  57.         if($j>=50)  
  58.         {  
  59.                 $rabbit->__destruct();  
  60.                 $j = 0;  
  61.   
  62.                 break;  
  63.         }  
  64.         sleep(1);  
  65. }  
  66.   
  67. echo "finish".PHP_EOL;  
  68.   
  69. ?>  

消费者脚本(test_consumer.php) (运行命令:php test_consumer.php 1 /  nohup php test_consumer.php 1 &)

[php]  view plain  copy
  1. <?php  
  2.   
  3. error_reporting(E_ERROR);  
  4. ini_set('display_errors','Off');  
  5.   
  6. declare(ticks = 1);  
  7.   
  8. $app = 'daemon';  
  9.   
  10. // Bootstrap the application  
  11. require realpath(dirname(__FILE__)) .'/../../../daemon/bootstrap.php';  
  12.   
  13. if(empty($_SERVER['argv'][1])){  
  14.     echo 'use action start or stop'.PHP_EOL;  
  15.     exit;  
  16. }  
  17.   
  18.   
  19. $log = $_SERVER['argv'][1];  
  20.   
  21. $config = array(  
  22.         'host' => '127.0.0.1',  
  23.         'port' => '5672',  
  24.         'login' => 'guest',  
  25.         'password' => 'guest',  
  26.         'vhost'=>'/'  
  27. );  
  28.   
  29. $rabbit = new Consumer($config"ex_{$log}""rk_{$log}""test_{$log}","direct");  
  30.   
  31. while ( true )  
  32. {  
  33.         try  
  34.         {  
  35.                 $msg = $rabbit->get( true );  
  36.   
  37.                 if(!empty($msg)) echo $msg.PHP_EOL;  
  38.   
  39.                 $handle=fopen("msg.log""a");  
  40.                 fwrite($handle$msg.PHP_EOL);  
  41.                 fclose($handle);  
  42.         }  
  43.         catch (Exception $e)  
  44.         {  
  45.                 $msg = false;  
  46.         }  
  47. }  
  48.   
  49. echo "end",PHP_EOL;  
  50.   
  51. ?>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值