摘要:后來(lái)開(kāi)源,現(xiàn)在有大規(guī)模部署和使用,每天處理百萬(wàn)級(jí)任務(wù)。是典型的類(lèi)設(shè)計(jì),協(xié)議和使用方式都是同樣的風(fēng)格,所以使用過(guò)的用戶(hù)會(huì)覺(jué)得似曾相識(shí)。
Beanstalkd介紹 什么是Beanstalkd
Beanstalk,一個(gè)高性能、輕量級(jí)的分布式內(nèi)存隊(duì)列系統(tǒng),最初設(shè)計(jì)的目的是想通過(guò)后臺(tái)異步執(zhí)行耗時(shí)的任務(wù)來(lái)降低高容量Web應(yīng)用系統(tǒng)的頁(yè)面訪問(wèn)延遲,支持過(guò)有9.5 million用戶(hù)的Facebook Causes應(yīng)用。
后來(lái)開(kāi)源,現(xiàn)在有PostRank大規(guī)模部署和使用,每天處理百萬(wàn)級(jí)任務(wù)。Beanstalkd是典型的類(lèi)Memcached設(shè)計(jì),協(xié)議和使用方式都是同樣的風(fēng)格,所以使用過(guò)memcached的用戶(hù)會(huì)覺(jué)得Beanstalkd似曾相識(shí)。
job典型的生命周期
put reserve delete -----> [READY] ---------> [RESERVED] --------> *poof*
job可能的狀態(tài)遷移
put with delay release with delay ----------------> [DELAYED] <------------. | | kick | (time passes) | | | put v reserve | delete -----------------> [READY] ---------> [RESERVED] --------> *poof* ^ ^ | | | release | | | `-------------" | | | | kick | | | | bury | [BURIED] <---------------" | | delete `--------> *poof*Beanstalkd安裝
安裝略過(guò),如需請(qǐng)參考 點(diǎn)擊參考
注:需要安裝Composer,教程鏈接 Composer安裝方法Pheanstalk安裝
用于操作Beanstalkd 的PHP 第三方庫(kù),安裝略過(guò),如需請(qǐng)參考 點(diǎn)擊參考
Pheanstalk操作 連接Beanstalkd Beanstalkd狀態(tài)方法Beanstalkd狀態(tài)
$stats = $pheanstalk->stats();
返回:
PheanstalkResponseArrayResponse Object ( [_name:PheanstalkResponseArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [current-jobs-urgent] => 0 //當(dāng)前存在優(yōu)先級(jí)的任務(wù)數(shù) [current-jobs-ready] => 0 //當(dāng)前準(zhǔn)備就緒的任務(wù)數(shù) [current-jobs-reserved] => 0 //當(dāng)前處于阻塞的任務(wù)數(shù) [current-jobs-delayed] => 0 //當(dāng)前處于延遲狀態(tài)的任務(wù)數(shù) [current-jobs-buried] => 0 //當(dāng)前預(yù)留的任務(wù)數(shù) [cmd-put] => 0 //cmd命令為累計(jì)運(yùn)行次數(shù) [cmd-peek] => 0 [cmd-peek-ready] => 0 [cmd-peek-delayed] => 0 [cmd-peek-buried] => 0 [cmd-reserve] => 0 [cmd-reserve-with-timeout] => 0 [cmd-delete] => 0 [cmd-release] => 0 [cmd-use] => 0 [cmd-watch] => 0 [cmd-ignore] => 0 [cmd-bury] => 0 [cmd-kick] => 0 [cmd-touch] => 0 [cmd-stats] => 1 [cmd-stats-job] => 0 [cmd-stats-tube] => 0 [cmd-list-tubes] => 3 [cmd-list-tube-used] => 0 [cmd-list-tubes-watched] => 0 [cmd-pause-tube] => 0 [job-timeouts] => 0 //超時(shí)的任務(wù) [total-jobs] => 0 //任務(wù)總數(shù) [max-job-size] => 65535 //任務(wù)字符串大小 [current-tubes] => 1 //當(dāng)前的管道數(shù) [current-connections] => 1 //當(dāng)前打開(kāi)的連接數(shù) [current-producers] => 0 //當(dāng)前生產(chǎn)者數(shù)量 [current-workers] => 0 //當(dāng)前消費(fèi)者數(shù)量 [current-waiting] => 0 //發(fā)出reserved指令,但沒(méi)有響應(yīng)的數(shù)量 [total-connections] => 4 //累計(jì)鏈接數(shù)量 [pid] => 530 //Beanstalkd進(jìn)程id [version] => 1.10 [rusage-utime] => 0.003545 [rusage-stime] => 0.007473 [uptime] => 105855 //運(yùn)行時(shí)間(秒) [binlog-oldest-index] => 0 [binlog-current-index] => 0 [binlog-records-migrated] => 0 [binlog-records-written] => 0 [binlog-max-size] => 10485760 [id] => 4199e3eca8bfdea8 [hostname] => lmmlwendeMacBook-Air.local ) )
當(dāng)前的管道列表
$listTubes = $pheanstalk->listTubes();
查看管道的詳細(xì)信息
$stats = $pheanstalk->statsTube();
返回:
PheanstalkResponseArrayResponse Object ( [_name:PheanstalkResponseArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [name] => default //當(dāng)前管道名 [current-jobs-urgent] => 0 //當(dāng)前管道存在優(yōu)先級(jí)的任務(wù)數(shù) [current-jobs-ready] => 0 //當(dāng)前管道準(zhǔn)備就緒的任務(wù)書(shū) [current-jobs-reserved] => 0 //當(dāng)前管道處于阻塞的任務(wù)數(shù) [current-jobs-delayed] => 0 //當(dāng)前管道處于延遲狀態(tài)的任務(wù)數(shù) [current-jobs-buried] => 0 //當(dāng)前管道預(yù)留的任務(wù)數(shù) [total-jobs] => 0 //當(dāng)前管道總?cè)蝿?wù)數(shù) [current-using] => 1 //當(dāng)前管道生產(chǎn)者數(shù)量 [current-watching] => 1 //當(dāng)前管道消費(fèi)者數(shù)量 [current-waiting] => 0 //發(fā)出reserved指令,但沒(méi)有響應(yīng)的數(shù)量 [cmd-delete] => 0 [cmd-pause-tube] => 0 [pause] => 0 [pause-time-left] => 0 ) )
查看任務(wù)的詳細(xì)信息
$job = $pheanstalk->watch("default")->reserve(); $job_stats = $pheanstalk->statsJob($job);
返回:
PheanstalkResponseArrayResponse Object ( [_name:PheanstalkResponseArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [id] => 1 //任務(wù)job ID [tube] => test //所處管道 [state] => reserved //當(dāng)前狀態(tài) [pri] => 1024 //任務(wù)優(yōu)先級(jí)(默認(rèn)1024) [age] => 469 //任務(wù)存活時(shí)間(秒) [delay] => 0 //任務(wù)延遲時(shí)間(秒) [ttr] => 60 //任務(wù)執(zhí)行時(shí)間 [time-left] => 59 //任務(wù)在reserve狀態(tài)維持的秒數(shù) [file] => 0 // binlog-4 默認(rèn)-0 [reserves] => 2 //總共reserve次數(shù) [timeouts] => 0 //任務(wù)超時(shí)次數(shù) [releases] => 0 //重設(shè)任務(wù)次數(shù) [buries] => 0 //預(yù)留次數(shù) [kicks] => 0 //釋放預(yù)留任務(wù)次數(shù) ) )
查看任務(wù)的詳細(xì)信息(通過(guò)ID)
$job = $pheanstalk->peek(1); $job_stats = $pheanstalk->statsJob($job);Beanstalkd生產(chǎn)者方法
指定需要使用的管道
$tube = $pheanstalk->useTube("default");
向管道插入數(shù)據(jù)
$tube = $pheanstalk->useTube("default"); $put = $tube->put( "hello, beanstalk", // 任務(wù)內(nèi)容 1024, // 任務(wù)的優(yōu)先級(jí) 10, // 不等待直接放到ready隊(duì)列中 60 // 處理任務(wù)的時(shí)間 );
或者:
$pheanstalk->putInTube("default", "test1", 1024, 10, 60);Beanstalkd消費(fèi)者方法
監(jiān)聽(tīng)管道
$tube = $pheanstalk->watch("user");
去除不需要監(jiān)聽(tīng)的管道
$tube = $pheanstalk->watch("user")->ignore("default");
以堵塞的方式監(jiān)聽(tīng)管道
$job = $pheanstalk->watch("user")->reserve(4); //堵塞時(shí)間為4秒
列出所有已經(jīng)監(jiān)聽(tīng)的管道
$pheanstalk->listTubesWatched();
watch + reserve 方法
$pheanstalk->reserveFromTube("default")
刪除當(dāng)前任務(wù)
$job = $pheanstalk->watch("default")->reserve(); $pheanstalk->delete($job);
將當(dāng)前任務(wù)重新放入管道
$job = $pheanstalk->watch("default")->reserve(); $pheanstalk->release($job);
為任務(wù)續(xù)命(當(dāng)處理任務(wù)的時(shí)間小于當(dāng)前任務(wù)執(zhí)行時(shí)間時(shí))
$job = $pheanstalk->watch("default")->reserve(); $pheanstalk->touch($job); //TODO
將任務(wù)預(yù)留
$job = $pheanstalk->watch("default")->reserve(); $pheanstalk->bury($job);
將預(yù)留任務(wù)釋放(變?yōu)閞eday狀態(tài))
$job = $pheanstalk->peekBuried("default"); $pheanstalk->kickJob($job);
批量將預(yù)留任務(wù)釋放
$pheanstalk->userTube("default")->kick(999); //將id小于999的預(yù)留任務(wù)全部釋放
讀取當(dāng)前準(zhǔn)備就緒的任務(wù)(ready)
$job = $pheanstalk->peekReady("default");
讀取當(dāng)前處于延遲狀態(tài)的任務(wù)(delayed)
$job = $pheanstalk->peekDelayed("default");
對(duì)管道設(shè)置延遲
$pheanstalk->pauseTube("default", 100); //設(shè)置100秒延遲
取消對(duì)管道的延遲
$pheanstalk->resumeTube("default");
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/28870.html
摘要:的消費(fèi)者,通過(guò)命令來(lái)獲取或改變的狀態(tài)。不再維持這些消息。持久化可以通過(guò)將及其狀態(tài)記錄到文件里面,在下次啟動(dòng)時(shí)可以通過(guò)讀取來(lái)恢復(fù)之前的及狀態(tài)。 一、安裝 1、官網(wǎng) https://kr.github.io/beanstalkd/ 2、安裝 yum install beanstalkd --enablerepo=epel 3、啟動(dòng) /usr/bin/beanstalkd -l 0.0.0....
摘要:說(shuō)明隊(duì)列配置文件存放在。每一種隊(duì)列驅(qū)動(dòng)的配置都可以在該文件中找到,包括數(shù)據(jù)庫(kù),,,,以及同步本地使用驅(qū)動(dòng)。其中還包含了一個(gè)隊(duì)列驅(qū)動(dòng)用于那些放棄隊(duì)列的任務(wù)。 showImg(https://segmentfault.com/img/remote/1460000012384126?w=888&h=280); 說(shuō)明 隊(duì)列配置文件存放在 config/queue.php。 每一種隊(duì)列驅(qū)動(dòng)的配...
摘要:項(xiàng)目地址框架目的基于和實(shí)現(xiàn)多進(jìn)程處理消息隊(duì)列。文件用于配置消息隊(duì)列管道及其處理類(lèi)項(xiàng)目配置文件文件夾下為方式運(yùn)行下產(chǎn)生的日志內(nèi)容是運(yùn)行時(shí)的進(jìn)程信息命令查看的狀態(tài)信息查看的信息啟動(dòng)啟動(dòng)停止重啟其它子進(jìn)程異常退出時(shí)將會(huì)自動(dòng)重啟。 項(xiàng)目地址 SWBT框架 https://github.com/YWNA/SWBT 目的 基于Swoole和beanstalkd實(shí)現(xiàn)多進(jìn)程處理消息隊(duì)列。 安裝 com...
摘要:幾小時(shí)前剛剛發(fā)布的為隊(duì)列功能提供了官方原生的驅(qū)動(dòng)器,完全取代了本軟件包的功能。不過(guò)這也并不意味著就是虛擬主機(jī)沒(méi)戲,必須云主機(jī)起跳的小網(wǎng)站殺手。性能確實(shí)低下受制于,絕對(duì)禁止用于大數(shù)量高密度任務(wù)的場(chǎng)合。虛擬主機(jī)肯定不會(huì)提供。 幾小時(shí)前剛剛發(fā)布的 Larevel 5.0 為隊(duì)列功能提供了官方原生的database驅(qū)動(dòng)器,完全取代了本軟件包的功能。 對(duì)于Laravel 5及以上版本,本文的內(nèi)...
摘要:上一篇文章講了通道建議先去看上篇文章與把隊(duì)列添加到數(shù)據(jù)表中把隊(duì)列添加到內(nèi)存中安裝依賴(lài)安裝到項(xiàng)目根目錄修改訪問(wèn)路由數(shù)據(jù)將寫(xiě)入到內(nèi)存中執(zhí)行命令打開(kāi)日志文件打印的消息完結(jié)下一篇文章講能更直觀的看到數(shù)據(jù)添加到內(nèi)存中 上一篇文章講了 database 通道 建議先去看上篇文章 1.beanstalkd 與 database database:把隊(duì)列添加到數(shù)據(jù)表中beanstalkd:把隊(duì)列添加到...
閱讀 1996·2021-11-22 19:20
閱讀 2641·2021-11-22 13:54
閱讀 1970·2021-09-04 16:40
閱讀 1827·2021-08-13 11:54
閱讀 2672·2019-08-30 15:55
閱讀 3468·2019-08-29 13:51
閱讀 531·2019-08-29 11:09
閱讀 3010·2019-08-26 14:06