1
0

RabbitUserController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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('localhost', 5673, 'guest', 'guest');
  58. // $connection = new AMQPStreamConnection('172.30.118.225', 5673, 'guest', 'guest');
  59. $channel = $connection->channel();
  60. $channel->queue_declare('login', false, true, false, false);
  61. //交互格式形式json php yii rabbittask/new-task "{"\"text\":\"广州\""}"
  62. // $city='{"text":"广州"}';
  63. // var_dump($city);
  64. // $city=json_decode($city,true);
  65. // var_dump($city);
  66. $callback = function ($msg) {
  67. //do sth
  68. //$msg->body
  69. //连接数据库
  70. //begin 持久化
  71. $model = new RabbitLog();
  72. $model->msg = json_encode($msg,TRUE);
  73. $model->msg_body =json_encode($msg->body,TRUE);
  74. $model->channel = json_encode($msg->delivery_info['channel'],TRUE);
  75. $model->queue = "login";
  76. $model->msg_delivery_tag =json_encode($msg->delivery_info['delivery_tag'],TRUE);
  77. $model->addtime = date('y-m-d h:i:s',time());
  78. $model->describe = "this is rabbit consume message";
  79. $model->save(false);
  80. //请求接口登入
  81. //模拟接口 吧数据取出来
  82. //$msg->body //string
  83. $userInfo = ApiHandler::getInstance()->post('/mock/trueExam.do', [
  84. 'id' => 'ffff-1513343606669-10163178175-0418',//
  85. 'data'=>$msg->body//还想加参数过去
  86. ]);
  87. //
  88. //调试
  89. var_dump('data:-----'.$msg->body);
  90. var_dump('userInfo:-----'.$userInfo);
  91. $postRequestObj=json_decode($msg->body);
  92. $mobile=$postRequestObj->mobile;
  93. //从msg->body 取出
  94. $redis = \Yii::$app->redis;
  95. $key = 'get-one-login-user-id-by-phone-' . $mobile;
  96. $redis->set($key,$userInfo);
  97. //通知
  98. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  99. };
  100. $channel->basic_qos(null, 1, null);
  101. $channel->basic_consume('login', '', false, false, false, false, $callback);
  102. while (count($channel->callbacks)) {
  103. $channel->wait();
  104. }
  105. $channel->close();
  106. $connection->close();
  107. }
  108. /*
  109. * demo
  110. *
  111. * */
  112. public function actionInsert()
  113. {
  114. $model = new RabbitLog();
  115. $model->msg = 1;
  116. $model->msg_body = 1;
  117. $model->channel = 1;
  118. $model->queue = 1;
  119. $model->msg_delivery_tag = 1;
  120. $model->addtime =date('y-m-d h:i:s',time());
  121. $model->describe = "this is rabbit consume message";
  122. $model->save(false);
  123. echo 'ok consume one '. "\n" ;
  124. }
  125. public function actionSelect()
  126. {
  127. $model = new RabbitLog();
  128. $model->msg = 1;
  129. $model->msg_body = 1;
  130. $model->channel = 1;
  131. $model->queue = 1;
  132. $model->msg_delivery_tag = 1;
  133. $model->addtime =date('y-m-d h:i:s',time());
  134. $model->describe = "this is rabbit consume message";
  135. $model->save(false);
  136. echo 'ok consume one '. "\n" ;
  137. }
  138. }