<?php namespace app\index\controller; use Firebase\JWT\JWT; use think\facade\Request; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; class Index { public function getToken() { $key = "huang"; //这里是自定义的一个随机字串,应该写在config文件中的,解密时也会用,相当 于加密中常用的 盐 salt $token = [ "iss" => "", //签发者 可以为空 "aud" => "", //面象的用户,可以为空 "iat" => time(), //签发时间 "nbf" => time(), //在什么时候jwt开始生效 (这里表示生成100秒后才生效) "exp" => time() + 7200, //token 过期时间 "uid" => 123,//记录的userid的信息,这里是自已添加上去的,如果有其它信息,可以再添加数组的键值对 'name' => '陈龙镇' ]; $jwt = JWT::encode($token, $key, "HS256"); //根据参数生成了 token return json([ "token" => $jwt ]); } /** * @return \think\response\Json */ public function check() { $jwt = Request::header('token'); $key = "huang"; //上一个方法中的 $key 本应该配置在 config文件中的 $info = JWT::decode($jwt, $key, ["HS256"]); //解密jwt return json($info); } public function test1() { $conf = [ 'host' => '127.0.0.1', 'port' => 5672, 'user' => 'guest', 'pwd' => 'guest', 'vhost' => '/', ]; $exchangeName = 'kd_sms_send_ex'; //交换机名 $queueName = 'kd_sms_send_q'; //队列名称 $routingKey = 'sms_send'; //路由关键字(也可以省略) $conn = new AMQPStreamConnection( //建立生产者与mq之间的连接 $conf['host'], $conf['port'], $conf['user'], $conf['pwd'], $conf['vhost'] ); $channel = $conn->channel(); //在已连接基础上建立生产者与mq之间的通道 $channel->basic_qos(null, 10, null); $channel->exchange_declare($exchangeName, 'direct', false, true, false); //声明初始化交换机 $channel->queue_declare($queueName, false, true, false, false); //声明初始化一条队列 $channel->queue_bind($queueName, $exchangeName, $routingKey); //将队列与某个交换机进行绑定,并使用路由关键字 $msgBody = json_encode(["name" => "iGoo", "age" => 22]); $msg = new AMQPMessage($msgBody, ['content_type' => 'text/plain', 'delivery_mode' => 2]); //生成消息 $r = $channel->basic_publish($msg, $exchangeName, $routingKey); //推送消息到某个交换机 $channel->close(); $conn->close(); } /** * @throws \Exception */ public function test2() { $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('hello', false, false, false, false); $msg = new AMQPMessage('Hello World!'); $channel->basic_publish($msg, '', 'hello'); echo " [x] Sent 'Hello World!'\n"; $channel->close(); $connection->close(); } /** * @throws \ErrorException */ public function test22() { $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('hello', false, false, false, false); echo " [*] Waiting for messages. To exit press CTRL+C\n"; $callback = function ($msg) { echo ' [x] Received ', $msg->body, "\n"; }; $channel->basic_consume('hello', '', false, true, false, false, $callback); while ($channel->is_consuming()) { $channel->wait(); } $channel->close(); $connection->close(); } /** * @throws \Exception */ public function test3() { do { fwrite(STDOUT, '请输入发送的内容:'); $data = trim(fgets(STDIN)); if (empty($data)) { $data = "Hello World!"; } $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('task_queue', false, true, false, false); $msg = new AMQPMessage( $data, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT) ); $channel->basic_publish($msg, '', 'task_queue'); echo ' [x] Sent ', $data, "\n"; $channel->close(); $connection->close(); } while ($data); } /** * @throws \ErrorException */ public function test33() { $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('task_queue', false, true, false, false); echo " [*] Waiting for messages. To exit press CTRL+C\n"; $callback = function ($msg) { print_r($msg->delivery_info['delivery_tag']); echo ' [x] Received ', $msg->body, "\n"; sleep(substr_count($msg->body, '.')); echo " [x] Done\n"; $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); }; $channel->basic_qos(null, 1, null); $channel->basic_consume('task_queue', '', false, false, false, false, $callback); while ($channel->is_consuming()) { $channel->wait(); } $channel->close(); $connection->close(); } /** * 使用交换机 * @throws \Exception */ public function test4(){ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('logs', 'fanout', false, false, false); fwrite(STDOUT, '请输入发送的内容:'); $data = trim(fgets(STDIN)); if (empty($data)) { $data = "Hello World!"; } if (empty($data)) { $data = "info: Hello World!"; } $msg = new AMQPMessage($data); $channel->basic_publish($msg, 'logs'); echo ' [x] Sent ', $data, "\n"; $channel->close(); $connection->close(); } /** * @throws \ErrorException */ public function test44(){ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); // 声明对应的交换机 $channel->exchange_declare('logs', 'fanout', false, false, false); //广播类型的交换机 // 创建一个非持久化的队列并获取自动生成的队列名称 list($queue_name, ,) = $channel->queue_declare("", false, false, true, false); // 绑定队列到交换机 $channel->queue_bind($queue_name, 'logs'); echo ' [*] Waiting for logs. To exit press CTRL+C', "\n"; $callback = function($msg){ echo ' [x] ', $msg->body, "\n"; }; $channel->basic_consume($queue_name, '', false, true, false, false, $callback); while(count($channel->callbacks)) { $channel->wait(); } $channel->close(); $connection->close(); } /** * 使用交换机和路由key * @throws \Exception */ public function test5(){ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); // 定义为direct模式路由 $channel->exchange_declare('direct_logs', 'direct', false, false, false); // 从命令行接收参数作为日志等级,默认为info fwrite(STDOUT, '请输入发送的内容:'); $severity = trim(fgets(STDIN)); if(empty($data)) $data = "Hello World!"; $msg = new AMQPMessage($data); // 发送消息到对应的路由,并携带一个routing key $channel->basic_publish($msg, 'direct_logs', $severity); echo " [x] Sent ",$severity,':',$data," \n"; $channel->close(); $connection->close(); } /** * @throws \ErrorException */ public function test55(){ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); // 定义为direct交换机并命名为direct_logs $channel->exchange_declare('direct_logs', 'direct', false, false, false); // 创建临时的非持久化的队列 list($queue_name, ,) = $channel->queue_declare("", false, false, true, false); fwrite(STDOUT, '请输入发送的内容:'); $severity = trim(fgets(STDIN)); $severities = explode(',',$severity); if(empty($severities )) { file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n"); exit(1); } foreach($severities as $severity) { // 绑定多个binding key $channel->queue_bind($queue_name, 'direct_logs', $severity); } echo ' [*] Waiting for logs. To exit press CTRL+C', "\n"; $callback = function($msg){ echo ' [x] ',$msg->delivery_info['routing_key'], ':', $msg->body, "\n"; }; $channel->basic_consume($queue_name, '', false, true, false, false, $callback); while(count($channel->callbacks)) { $channel->wait(); } $channel->close(); $connection->close(); } /** * rpc 服务端 * @throws \ErrorException */ public function rpc_1(){ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('rpc_queue', false, false, false, false); function fib($n) { if ($n == 0) return 0; if ($n == 1) return 1; return fib($n-1) + fib($n-2); } echo " [x] Awaiting RPC requests\n"; $callback = function($req) { $n = intval($req->body); echo " [.] fib(", $n, ")\n"; $msg = new AMQPMessage( (string) fib($n), array('correlation_id' => $req->get('correlation_id')) ); $req->delivery_info['channel']->basic_publish($msg, '', $req->get('reply_to')); $req->delivery_info['channel']->basic_ack($req->delivery_info['delivery_tag']); }; $channel->basic_qos(null, 1, null); $channel->basic_consume('rpc_queue', '', false, false, false, false, $callback); while(count($channel->callbacks)) { $channel->wait(); } $channel->close(); $connection->close(); } public function rpc_11(){ do { fwrite(STDOUT, '请输入发送的内容:'); $data = trim(fgets(STDIN)); if (empty($data)) { $data = 10; } $fibonacci_rpc = new FibonacciRpcClient(); $response = $fibonacci_rpc->call($data); echo " [.] Got ", $response, "\n"; } while (true); } } /** * rpc 服务客户端 * Class FibonacciRpcClient * @package app\index\controller */ class FibonacciRpcClient { private $connection; private $channel; private $callback_queue; private $response; private $corr_id; public function __construct() { $this->connection = new AMQPStreamConnection( 'localhost', 5672, 'guest', 'guest'); $this->channel = $this->connection->channel(); list($this->callback_queue, ,) = $this->channel->queue_declare("", false, false, true, false); $this->channel->basic_consume($this->callback_queue, '', false, false, false, false, array($this, 'on_response')); } public function on_response($rep) { if($rep->get('correlation_id') == $this->corr_id) { $this->response = $rep->body; } } /** * @throws \Exception */ public function __destruct() { if ($this->channel !== null) $this->channel->close(); if ($this->connection !== null) $this->connection->close(); } /** * @param $n * @return int * @throws \ErrorException */ public function call($n) { $this->response = null; $this->corr_id = uniqid(); $msg = new AMQPMessage((string) $n, array('correlation_id' => $this->corr_id, 'reply_to' => $this->callback_queue)); $this->channel->basic_publish($msg, '', 'rpc_queue'); while(!$this->response) { $this->channel->wait(null, false, 20); } return intval($this->response); } };
thinkphp 使用 rabbitmq
最新推荐文章于 2024-06-07 16:17:55 发布