本文介绍了Laravel API速度(太慢)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一种API,用于将来自多个应用程序的警报存储到数据库中.我正在使用Laravel开发API.

i'm trying to develop an API to store alarms coming from several applications into a database. I'm developing the api with Laravel.

我已经制作了一个C程序,该程序向API发出多个后期请求,以查看laravel api可以处理多少个请求.

I've made a C program which makes multiple post requests to the API in order to see how many request can the laravel api process.

我在api.php中有以下路线

I have the following route in api.php

Route::post('/alarm', 'Api\v1\AlarmController@store');

在我的控制器中,我实现了存储功能,该功能将发布请求中接收到的警报值存储到数据库中

In my controller i have made the store function which stores the alarm values received in the post request into the database

function store(Request $request)
    {

        $content = $request->getContent();

       $json_content = json_decode($content);

        $id = $this->alarm_model->newAlarm($json_content);

        echo '{ "result": '.$id.', "msg":'.$content.' }';

    }

然后我有一个Alarm模型,该模型将json值存储在数据库警报表中.

Then i have a Alarm model which stores the json values in the database alarm table.

如果我发出1000个请求,则无法处理所有请求.我收到HttpSendRequest错误:12002 Internet超时

If i make 1000 requests it is not able to process all of them. I get HttpSendRequest Error : 12002 Internet Timeout

我做错什么了吗? Laravel框架每秒允许多少个请求?

Am I doing something wrong? How many requests per second allows Laravel framework?

推荐答案

没有限制,据我所知 ,这完全取决于您的环境,如果是小型服务器,我会怀疑每秒能够处理1000个请求.

There's no limit, as far as i know, it all depends on your environment, if it's a small server i doubt it would be able to handle 1000 requests per second.

您可以做的是利用 Laravel队列

我认为您的情况正在发生,您的服务器在尝试一次完成1000个请求时变得太忙了.这导致服务器以我很忙"信号进行响应.

What i think is happening on your end is that your server gets too busy when trying to complete the 1000 requests at once. This results in the server responding with the "I'm Busy" signal.

但是有了队列,您可以限制服务器的压力,并使其在几分钟内完成?

But with a queue you could limit the pressure on the server and make it happen over the course of a few minutes?

方法:

  1. 创建作业php artisan make:job ProcessAlarms

在作业的句柄"方法中,您输入了保存警报的逻辑

In the Job's Handle method you put the logic to save the Alarm

然后在控制器的store方法中使用此dispatch(new ProcessAlarm($data_you_want_to_pass));

Then in your Controller's store method use this dispatch(new ProcessAlarm($data_you_want_to_pass));

这篇关于Laravel API速度(太慢)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:17