Aliyun MNS Queue Driver For Laravel
![Software License][ico-license]
![Total Downloads][ico-downloads], (*1)
Install
Via Composer, (*2)
``` bash
$ composer require chenzi/laravel-mns-driver, (*3)
## Config
Add following service providers into your providers array in `config/app.php`
``` php
Chenzi\LaravelMNS\LaravelMNSServiceProvider::class
Edit your config/queue.php, add mns connection, (*4)
'mns' => [
'sms'=> [
'driver' => 'sms',
'key' => env('QUEUE_MNS_ACCESS_KEY'),
'secret' => env('QUEUE_MNS_SECRET_KEY'),
'endpoint' => env('QUEUE_MNS_ENDPOINT'),
'queue' => env('QUEUE_NAME'),
'wait_seconds' => 30,
],
'email'=> [
'driver' => 'email',
'key' => env('QUEUE_MNS_ACCESS_KEY'),
'secret' => env('QUEUE_MNS_SECRET_KEY'),
'endpoint' => env('QUEUE_MNS_ENDPOINT'),
'queue' => env('QUEUE_NAME'),
'wait_seconds' => 30,
]
]
About wait_seconds, (*5)
Edit your .env file, (*6)
QUEUE_DRIVER=mns
QUEUE_NAME=foobar-local
QUEUE_MNS_ACCESS_KEY=your_acccess_key
QUEUE_MNS_SECRET_KEY=your_secret_key
QUEUE_MNS_ENDPOINT=http://12345678910.mns.cn-hangzhou.aliyuncs.com/
You should update QUEUE_MNS_ENDPOINT to internal endpoint in production mode, (*7)
Usage
First create a queue and get queue endpoint at Aliyun MNS Console, (*8)
Then update MNS_ENDPOINT in .env, (*9)
Push a test message to queue, (*10)
Queue::push(function($job){
/**
* Your statments go here
*/
$job->delete();
});
You also can custom request data format add mns queue with other program, (*11)
return json_encode( [
'displayName' => 'App\Jobs\SendSms',
'job' => 'App\Jobs\SendSms',
'maxTries' => null,
'timeout' => null,
'data' => [],
] );
Create queue work, run command in terminal, (*12)
$ php artisan queue:mns:work sms
Commands
Flush MNS messages on Aliyun, (*13)
$ php artisan queue:mns:flush
Job handle
class SendSms implements ShouldQueue
{
...
public function handle(ReceiveMessageResponse $job){
$messageId = $job->getMessageId();
$messageBody = $job->getMessageBody();
//TODO do some thing...
}
...
}
Security
Create RAM access control at Aliyun RAM Console, (*14)
-
Create a custom policy such as AliyunMNSFullAccessFoobar, (*15)
{
"Version": "1",
"Statement": [
{
"Action": "mns:*",
"Resource": [
"acs:mns:*:*:*/foobar-local",
"acs:mns:*:*:*/foobar-sandbox",
"acs:mns:*:*:*/foobar-production"
],
"Effect": "Allow"
}
]
}
-
Create a user for you app such as foobar, (*16)
-
Assign the policy AliyunMNSFullAccessFoobar to the user foobar, (*17)
-
Create and get the AccessKeyId and AccessKeySecret for user foorbar, (*18)
-
update QUEUE_MNS_ACCESS_KEY and QUEUE_MNS_ACCESS_SECRET in .env, (*19)
Change log
Please see CHANGELOG for more information what has changed recently., (*20)
Testing
bash
$ composer test, (*21)
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*22)
Credits
License
The MIT License (MIT). Please see License File for more information., (*23)