Explorar o código

Merge pull request #1632 from visuddhinanda/laravel

config 加入 mq设置
visuddhinanda %!s(int64=2) %!d(string=hai) anos
pai
achega
bbe9503f42

+ 1 - 1
app/Console/Commands/MqDiscussion.php

@@ -16,7 +16,7 @@ class MqDiscussion extends Command
 {
 {
     /**
     /**
      * The name and signature of the console command.
      * The name and signature of the console command.
-     *
+     * php artisan mq:discussion
      * @var string
      * @var string
      */
      */
     protected $signature = 'mq:discussion';
     protected $signature = 'mq:discussion';

+ 54 - 0
app/Console/Commands/MqTask.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Http\Api\Mq;
+use Illuminate\Support\Facades\Log;
+
+class MqTask extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'mq:task';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'run task';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $this->info('env='.env("RABBITMQ_HOST"));
+        $this->info('config='.config("queue.connections.rabbitmq.host"));
+        $exchange = 'router';
+        $queue = 'task';
+        $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
+        Mq::worker($exchange,$queue,function ($message){
+            $message = json_decode(json_encode($message), true);
+            $this->info('name=',$message['name']);
+            return $this->call($message['name'],$message['param']);
+        });
+        return 0;
+    }
+}

+ 36 - 11
app/Http/Api/Mq.php

@@ -6,16 +6,31 @@ use PhpAmqpLib\Exchange\AMQPExchangeType;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Log;
 
 
 class Mq{
 class Mq{
+
+    private static function connection(){
+        $host = config("queue.connections.rabbitmq.host");
+            $port = config("queue.connections.rabbitmq.port");
+            $user = config("queue.connections.rabbitmq.user");
+            $password = config("queue.connections.rabbitmq.password");
+            $vhost = config("queue.connections.rabbitmq.password");
+            if(empty($host) || empty($port) || empty($user) || empty($password) || empty($vhost)){
+                Log::error('rabbitmq set error');
+                return;
+            }
+            $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
+            return $connection;
+    }
     public static function publish(string $channelName, $message){
     public static function publish(string $channelName, $message){
                 //一对一
                 //一对一
 
 
         try{
         try{
-            $host = config("rabbitmq.host");
-            $port = config("rabbitmq.port");
-            $user = config("rabbitmq.user");
-            $password = config("rabbitmq.password");
-            $vhost = config("rabbitmq.virtual.host");
+            $host = config("queue.connections.rabbitmq.host");
+            $port = config("queue.connections.rabbitmq.port");
+            $user = config("queue.connections.rabbitmq.user");
+            $password = config("queue.connections.rabbitmq.password");
+            $vhost = config("queue.connections.rabbitmq.virtual_host");
             if(empty($host) || empty($port) || empty($user) || empty($password) || empty($vhost)){
             if(empty($host) || empty($port) || empty($user) || empty($password) || empty($vhost)){
+                Log::error('rabbitmq set error');
                 return;
                 return;
             }
             }
             $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
             $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
@@ -43,11 +58,21 @@ class Mq{
 
 
         $consumerTag = 'consumer';
         $consumerTag = 'consumer';
 
 
-        $connection = new AMQPStreamConnection(config("rabbitmq.host"),
-                                        config("rabbitmq.port"),
-                                        config("rabbitmq.user"),
-                                        config("rabbitmq.password"),
-                                        config("rabbitmq.virtual.host"));
+
+        $host = config("queue.connections.rabbitmq.host");
+        $port = config("queue.connections.rabbitmq.port");
+        $user = config("queue.connections.rabbitmq.user");
+        $password = config("queue.connections.rabbitmq.password");
+        $vhost = config("queue.connections.rabbitmq.virtual_host");
+        $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
+
+    /*
+        $connection = new AMQPStreamConnection(env("RABBITMQ_HOST"),
+                                            env("RABBITMQ_PORT"),
+                                            env("RABBITMQ_USER"),
+                                            env("RABBITMQ_PASSWORD"),
+                                            env("RABBITMQ_VIRTUAL_HOST"));
+*/
         $channel = $connection->channel();
         $channel = $connection->channel();
 
 
  /*
  /*
@@ -96,7 +121,7 @@ class Mq{
                     $msg = new AMQPMessage(json_encode(['exchange'=>$exchange,
                     $msg = new AMQPMessage(json_encode(['exchange'=>$exchange,
                                                         'channel'=>$queue,
                                                         'channel'=>$queue,
                                                         'message'=>json_decode($message->body),
                                                         'message'=>json_decode($message->body),
-                                                        'result'=>$result,
+                                                        'result'=>0,
                                                         'error'=>$e,
                                                         'error'=>$e,
                                                         ],JSON_UNESCAPED_UNICODE));
                                                         ],JSON_UNESCAPED_UNICODE));
                     $channelIssues->basic_publish($msg, '', $channelName);
                     $channelIssues->basic_publish($msg, '', $channelName);

+ 8 - 0
config/queue.php

@@ -71,6 +71,14 @@ return [
             'after_commit' => false,
             'after_commit' => false,
         ],
         ],
 
 
+        'rabbitmq' => [
+            'host' => env('RABBITMQ_HOST', '127.0.0.1'),
+            'port' => env('RABBITMQ_PORT', 5672),
+            'user' => env('RABBITMQ_USER', 'guest'),
+            'password' => env('RABBITMQ_PASSWORD', 'guest'),
+            'virtual_host' => env('RABBITMQ_VIRTUAL_HOST', '/'),
+        ],
+
     ],
     ],
 
 
     /*
     /*