IronMQ和Laravel:设置

This two-part article series aims to make a beginner understand using push queues with Laravel. To give a brief overview, the final solution which we will be looking at is a simple form to upload photos and resize them. Resizing images is a time consuming task, so instead of making a user wait until the image is resized we can do it in the background. At the same time, we’ll learn to use a tool called ngrok so that we can use queues in our local system.

这个由两部分组成的文章系列旨在使初学者了解如何将推队列与Laravel结合使用。 为了简要概述,我们将要考虑的最终解决方案是上传照片并调整照片大小的简单表格。 调整图像大小是一项耗时的任务,因此,我们可以在后台进行操作,而不是让用户等到调整图像大小后再进行操作。 同时,我们将学习使用名为ngrok的工具,以便可以在本地系统中使用队列。

The source code is provided at: https://github.com/phpmasterdotcom/laravel-queues

源代码位于: https : //github.com/phpmasterdotcom/laravel-queues

You can also try it in a proper live server.

您也可以在适当的实时服务器中尝试。

排队和铁 (Queues and Iron)

A queue is nothing but a pipeline where we line up our jobs and they are executed in the order they are inserted. Let’s say currently our queue is empty and we put Job A into it, then we put Job B and Job C. We read the queue in the same order. First we take Job A from the queue and finish it, and then work on next one. If you focus on the “read” part, we always have to check our queue to see if any new job is posted. Wouldn’t it be better if the queue itself notifies us when a job is available ? Queues like those are called push queues, and that’s what we’ll be using with IronMQ.

队列不过是一个管道,我们在其中排队作业,并按照插入顺序执行作业。 假设当前队列是空的,然后将作业A放入其中,然后将作业B和作业C放入队列中。我们以相同的顺序读取队列。 首先,我们从队列中取出作业A并完成它,然后再处理下一个作业。 如果您专注于“阅读”部分,我们总是必须检查队列以查看是否发布了任何新作业。 队列本身在有工作时通知我们会更好吗? 像这样的队列称为推送队列,这就是我们将与IronMQ一起使用的队列

Iron MQ is a service which makes our life easier when it comes to using queues. When you create a push queue you also need to mention a subscriber to that queue. A subscriber is nothing but a url which will be called when a job is available in the queue with the data originally provided to the job.

Iron MQ是一项服务,使我们在使用队列时更加轻松。 创建推送队列时,您还需要提及该队列的订阅者。 订阅者不过是一个URL,当队列中有可用的作业且原始提供给该作业的数据将被调用。

To read more about job queues and see comparisons between different solutions, see this article.

要了解有关作业队列的更多信息并查看不同解决方案之间的比较,请参阅本文

安装与安装 (Setup and Installation)

In this step we install Laravel and all required dependencies via composer. We also create an account at Iron and install a tool called ngrok.

在这一步中,我们通过composer安装Laravel和所有必需的依赖项。 我们还在Iron上创建了一个帐户,并安装了一个名为ngrok的工具。

Laravel (Laravel)
  1. Install Composer.

    安装Composer

  2. Install Laravel

    安装Laravel

    composer create-project laravel/laravel --prefer-dist

    composer create-project laravel/laravel --prefer-dist

    After this command you should see the folder “laravel”. Open the laravel folder in your favorite text editor or IDE.

    执行此命令后,您应该会看到文件夹“ laravel”。 在您喜欢的文本编辑器或IDE中打开laravel文件夹。

    Go into the folder via the command line and run:

    通过命令行进入文件夹并运行:

    php artisan serve

    php artisan serve

    At this point if you open http://localhost:8000 you should see the home page of your Laravel installation.

    此时,如果您打开http:// localhost:8000 ,则应该会看到Laravel安装的主页。

  3. Set up the Database : We will use MySQL for our database. Create a database via the command line, phpmyadmin, or whichever tool you are comfortable with. After you created a database, go to app/config/app.php and find the section similar to the following:

    设置数据库 :我们将使用MySQL作为数据库。 通过命令行,phpmyadmin或任何您喜欢的工具创建数据库。 创建数据库后,转到app/config/app.php并找到与以下内容类似的部分:

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )

    Fill the required fields. Assuming you are on a local system, you need to provide the credentials of your MySQL server (username, password) and the name of the database you created.

    填写必填字段。 假设您在本地系统上,则需要提供MySQL服务器的凭据(用户名,密码)和您创建的数据库的名称。

    Run php artisan migrate:install. If you have put your credentials correctly it should say: Migration table created successfully.

    运行php artisan migrate:install 。 如果您正确放置了凭据,则应该说:迁移表已成功创建。

  4. Let’s make a small edit. Open app/views/hello.php and find the following line:

    让我们做一个小的编辑。 打开app/views/hello.php并找到以下行:

    <h1>You have arrived.</h1>

    Edit it to (You can change it to anything!):

    将其编辑为(您可以将其更改为任何内容!):

    <h1>I am learning how to use queues!</h1>

    If you open http://localhost:8000 you should be able to see the Laravel logo and some text under it reading: “I am learning how to use queues!”.

    如果您打开http:// localhost:8000,您应该能够看到Laravel徽标及其下的一些文字:“我正在学习如何使用队列!”。

  5. Open composer.json, add iron (for iron push queues) and intervention (for image manipulation) libraries.

    打开composer.json ,添加铁(用于铁推队列)和干预(用于图像操纵)库。

    "require": {
        "laravel/framework": "4.1.*",
        "iron-io/iron_mq": "1.4.6",
        "intervention/image": "dev-master"
    },

    Update the code

    更新代码

    composer update
  6. Follow instructions below to finish installation of Intervention.

    请按照以下说明完成Intervention的安装。

    Open your Laravel config file config/app.php. In the $providers array add the service providers for this package: 'Intervention\Image\ImageServiceProvider', then add the facade of this package to the $aliases array: 'Image' => 'Intervention\Image\Facades\Image',

    打开您的Laravel配置文件config/app.php 。 在$providers数组中,添加此包的服务提供者: 'Intervention\Image\ImageServiceProvider',然后将该包的外观添加到$aliases数组中: 'Image' => 'Intervention\Image\Facades\Image',

    Reference: http://intervention.olivervogel.net/image/getting_started/laravel

    参考: http : //intervention.olivervogel.net/image/getting_started/laravel

恩格罗克 (ngrok)

In production users access your website via a url like http://example.com. But, you all know that our localhost is not available to the whole internet and as we are going to use Iron for implementing queues, we need to host our code in a server so that our website is accessible to Iron. Doing that means, every time we make a change to our code we have to upload to server and test to see if it is working as we intended. Wouldn’t it be better if we could test our code on a local system? That’s why we use a tool called ngrok which makes our localhost accessible to the whole internet via a special url. We will be installing and using it in a second.

在生产用户中,您可以通过http://example.com之类的URL访问您的网站。 但是,大家都知道我们的本地主机无法用于整个Internet,并且由于我们将使用Iron来实现队列,因此我们需要将代码托管在服务器中,以便Iron可以访问我们的网站。 这样做意味着,每次我们更改代码时,都必须上载到服务器并进行测试以查看其是否按预期工作。 如果可以在本地系统上测试我们的代码会更好吗? 这就是为什么我们使用一个名为ngrok的工具,该工具使我们的本地主机可以通过特殊的URL访问整个互联网。 我们将在一秒钟内安装和使用它。

  1. Go to ngrok.

    ngrok

  2. Download and Install ngrok. Follow – How to install ngrok.

    下载并安装ngrok。 关注– 如何安装ngrok

    The steps are:

    这些步骤是:

    • Download the zip file for your operating system.

      下载适用于您的操作系统的zip文件。
    • Unzip the downloaded zip file

      解压下载的zip文件
    • You should be able to run ./ngrok --help from the command line.

      您应该能够从命令行运行./ngrok --help

  3. Let’s make our localhost visible on the internet.

    让我们在互联网上显示本地主机。

    For this step we assume Laravel is still running at http://localhost:8000. (If it’s not, run php artisan serve from the command line at the root of the Laravel folder)

    对于此步骤,我们假设Laravel仍在http://localhost:8000 。 (如果不是,请从命令行在Laravel文件夹的根目录运行php artisan serve )

    (You need to run the ngrok command through another terminal as in one terminal php artisan serve needs to be running)

    (您需要通过另一终端运行ngrok命令,因为在一个终端中需要运行php artisan serve )

    ./ngrok 8000

    I have given port as 8000 since our laravel is running on 8000. After I run the command I get a reply:

    自从我们的laravel在8000上运行以来,我已将端口指定为8000。运行命令后,我得到了答复:

    Tunnel Status                 online                                            
    Version                       1.6/1.5                                           
    Forwarding                    http://953ffbb.ngrok.com -> 127.0.0.1:8000        
    Forwarding                    https://953ffbb.ngrok.com -> 127.0.0.1:8000       
    Web Interface                 127.0.0.1:4040                                    
    # Conn                        2                                                 
    Avg Conn Time                 167.38ms

    Note: The response you get is different every time you run ngrok. So, the response which you see after running ./ngrok 8000 will be different from what I posted above.

    注意 :每次运行ngrok时,得到的响应都是不同的。 因此,运行./ngrok 8000后看到的响应将与我上面发布的响应不同。

    Observe the url under “Forwarding” : http://953ffbb.ngrok.com. If you open that url (which is a different url for you) you should be able to see your website with the Laravel logo and the text you have written, e.g.: “I am learning how to use queues!”.

    查看“转发”下的网址:http: //953ffbb.ngrok.com 。 如果您打开该URL(这是您使用的其他URL),则应该可以看到带有Laravel徽标和所写文本的网站,例如:“我正在学习如何使用队列!”。

    I suggest you keep ngrok running and note down the Forwarding url as we are going to use it for this tutorial.

    我建议您保持ngrok的运行状态,并记下转发URL,因为我们将在本教程中使用它。

    If you happen to restart ngrok in between, your url will change. It’s not at all a problem! Just make use of the different url whereever needed.

    如果您介于两者之间而重新启动ngrok,则您的网址将会更改。 完全没有问题! 只需在需要的地方使用不同的URL。

铁MQ (Iron MQ)

We will use Iron for implementing queues in our project. To use Iron we first need to create an account.

我们将使用Iron在项目中实现队列。 要使用Iron,我们首先需要创建一个帐户。

  1. Go to http://www.iron.io/ and log in

    转到http://www.iron.io/并登录

  2. Create a new project. I have named my project “Laravel Queues”.

    创建一个新项目。 我将我的项目命名为“ Laravel Queues”。

  3. You can see a “Key” button right next to your project name. Click on it and copy “Token” and “Project ID” to app/config/queue.php.

    您可以在项目名称旁边看到一个“键”按钮。 单击它,然后将“令牌”和“项目ID”复制到app/config/queue.php

    • Change default to ‘iron’

      将默认设置更改为“铁”

      'default' => 'iron',
    • Find the below section:

      找到以下部分:

      'iron' => array(
          'driver'  => 'iron',
          'project' => 'your-project-id',
          'token'   => 'your-token',
          'queue'   => 'your-queue-name',
      )

      Copy your Project ID and Token from the dashboard and paste them here, name your queue “laravel”.

      从仪表板上复制您的项目ID和令牌,然后将其粘贴到此处,将队列命名为“ laravel”。

      'iron' => array(
          'driver'  => 'iron',
          'project' => 'your-project-id',
          'token'   => 'your-token',
          'queue'   => 'laravel',
      )
  4. As discussed above we need to add a subscriber for our queue. You can go to Laravel Push Queues and check the official documentation which we are going to follow now:

    如上所述,我们需要为队列添加一个订户。 您可以转到Laravel Push Queues并检查我们现在将要遵循的官方文档:

    • We have to register a push queue subscriber using the following artisan command (We have to run this command from our project root folder)

      我们必须使用以下artisan命令注册推送队列订户(我们必须在项目根目录中运行此命令)

      From above reference, we will run php artisan queue:subscribe laravel http://953ffbb.ngrok.com/queue/receive

      通过以上参考,我们将运行php artisan queue:subscribe laravel http://953ffbb.ngrok.com/queue/receive

      Note that the url I used was the Forwarding url which ngrok generated. You should see a reply similar to the one below:

      请注意,我使用的网址是ngrok生成的转发网址 。 您应该看到类似于以下内容的回复:

      Queue subscriber added: http://953ffbb.ngrok.com/queue/receive

      If you check your dashboard and click on “MQ” you will go to the project page. Click on “Queues” and find a queue named “laravel”. If you click on a queue name, i.e. laravel, you can see a list of subscribers in the Push Queues tab and find http://953ffbb.ngrok.com/queue/receive under it.

      如果您检查仪表板并单击“ MQ”,将转到项目页面。 单击“队列”,然后找到一个名为“ laravel”的队列。 如果单击队列名称(即laravel),则可以在“推送队列”选项卡中看到订户列表,并在其下找到http://953ffbb.ngrok.com/queue/receive

    • Put this code in app/routes.php

      将此代码放在app/routes.php

      Route::post('queue/receive', function()
      {
          return Queue::marshal();
      });

From the official documentation:

根据官方文档:

The marshal method will take care of firing the correct job handler class. To fire jobs onto the push queue, just use the same Queue::push method used for conventional queues.

编组方法将负责触发正确的作业处理程序类。 要将作业激发到推式队列中,只需使用与常规队列相同的Queue :: push方法即可。

结语 (Wrapping Up)

In this part:

在这一部分:

  1. We have installed Laravel with Iron and Intervention libraries. Our laravel is running at http://localhost:8000

    我们已经在Laravel中安装了Iron和Intervention库。 我们的laravel运行在http://localhost:8000

  2. We installed ngrok and we exposed our localhost to the whole internet via a Forwarding Url that was generated for us (let ngrok run in the terminal).

    我们安装了ngrok,并通过为我们生成的Forwarding Url将localhost暴露给整个Internet(让ngrok在终端中运行)。

  3. We configured Iron with Laravel and created a queue with a subscriber.

    我们使用Laravel配置了Iron,并与订阅者创建了一个队列。

In the next part, we’ll do the heavy lifting and get our hands dirty. We’ll be building our app and implementing the job logic fully. Stay tuned!

在下一部分中,我们将进行繁重的工作并使手变脏。 我们将构建我们的应用程序并完全实现作业逻辑。 敬请关注!

翻译自: https://www.sitepoint.com/ironmq-laravel-setup/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值