RabbitUserController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace console\controllers;
  8. use components\ApiHandler;
  9. use common\models\RabbitLog;
  10. use components\RabbitBase;
  11. use components\RabbitMqBase;
  12. use components\RabbitMqServer;
  13. use yii\console\Controller;
  14. use components\PhpClient;
  15. use PhpAmqpLib\Message\AMQPMessage;
  16. use PhpAmqpLib\Connection\AMQPStreamConnection;
  17. use yii\helpers\ArrayHelper;
  18. /**
  19. * This command echoes the first argument that you have entered.
  20. *
  21. * This command is provided as an example for you to learn how to create console commands.
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @since 2.0
  25. */
  26. class RabbitUserController extends Controller
  27. {
  28. public function actionGo()
  29. {
  30. // $RabbitMqServer = new RabbitMqServer();
  31. // $responses = $RabbitMqServer->setRabbit('{json:1}','','yang');//在这里传入发送给服务端脚本的内容
  32. $RabbitMqServer = new RabbitMqBase();
  33. RabbitMqBase::setRabbitMq('{json:2}','','yang');
  34. }
  35. public function actionLogin()
  36. {
  37. //从redis中取数结果
  38. $mobile='13236390680';
  39. $redis = \Yii::$app->redis;
  40. $key = 'get-one-login-user-id-by-phone-' . $mobile;
  41. $result = $redis->get($key); ;
  42. $redis->set($key,$result);
  43. var_dump($result);
  44. }
  45. public function actionGet()
  46. {
  47. $res = RabbitMqBase::getRabbitMq('','');
  48. var_dump($res);
  49. }
  50. public function actionShow()
  51. {
  52. $RabbitMqServer = new RabbitBase();
  53. $RabbitMqServer->call('nihao');
  54. }
  55. public function actionWorker()
  56. {
  57. $connection = new AMQPStreamConnection('172.30.118.225', 5673, 'guest', 'guest');
  58. $channel = $connection->channel();
  59. $channel->queue_declare('login', false, true, false, false);
  60. //交互格式形式json php yii rabbittask/new-task "{"\"text\":\"广州\""}"
  61. // $city='{"text":"广州"}';
  62. // var_dump($city);
  63. // $city=json_decode($city,true);
  64. // var_dump($city);
  65. $callback = function ($msg) {
  66. //do sth
  67. //$msg->body
  68. //连接数据库
  69. //begin 持久化
  70. $model = new RabbitLog();
  71. $model->msg = json_encode($msg,TRUE);
  72. $model->msg_body =json_encode($msg->body,TRUE);
  73. $model->channel = json_encode($msg->delivery_info['channel'],TRUE);
  74. $model->queue = "login";
  75. $model->msg_delivery_tag =json_encode($msg->delivery_info['delivery_tag'],TRUE);
  76. $model->addtime = date('y-m-d h:i:s',time());
  77. $model->describe = "this is rabbit consume message";
  78. $model->save(false);
  79. //请求接口登入
  80. //模拟接口 吧数据取出来
  81. //$msg->body //string
  82. $userInfo = ApiHandler::getInstance()->post('/mock/trueExam.do', [
  83. 'id' => 'ffff-1513343606669-10163178175-0418',//
  84. 'data'=>$msg->body//还想加参数过去
  85. ]);
  86. //
  87. //调试
  88. var_dump('data:-----'.$msg->body);
  89. var_dump('userInfo:-----'.$userInfo);
  90. $postRequestObj=json_decode($msg->body);
  91. $mobile=$postRequestObj->mobile;
  92. //从msg->body 取出
  93. $redis = \Yii::$app->redis;
  94. $key = 'get-one-login-user-id-by-phone-' . $mobile;
  95. $redis->set($key,$userInfo);
  96. //通知
  97. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  98. };
  99. $channel->basic_qos(null, 1, null);
  100. $channel->basic_consume('login', '', false, false, false, false, $callback);
  101. while (count($channel->callbacks)) {
  102. $channel->wait();
  103. }
  104. $channel->close();
  105. $connection->close();
  106. }
  107. /*
  108. * demo
  109. *
  110. * */
  111. public function actionInsert()
  112. {
  113. $model = new RabbitLog();
  114. $model->msg = 1;
  115. $model->msg_body = 1;
  116. $model->channel = 1;
  117. $model->queue = 1;
  118. $model->msg_delivery_tag = 1;
  119. $model->addtime =date('y-m-d h:i:s',time());
  120. $model->describe = "this is rabbit consume message";
  121. $model->save(false);
  122. echo 'ok consume one '. "\n" ;
  123. }
  124. public function actionSelect()
  125. {
  126. $model = new RabbitLog();
  127. $model->msg = 1;
  128. $model->msg_body = 1;
  129. $model->channel = 1;
  130. $model->queue = 1;
  131. $model->msg_delivery_tag = 1;
  132. $model->addtime =date('y-m-d h:i:s',time());
  133. $model->describe = "this is rabbit consume message";
  134. $model->save(false);
  135. echo 'ok consume one '. "\n" ;
  136. }
  137. }